java - JAVAFX 在线程中调用函数
问题描述
我目前正在尝试从我的Thread
(这是一个简单的计数器)调用一个函数。没有该函数,线程运行良好,但是当我尝试调用它时,出现此错误:
Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
at window.GamePane.destroyCardPane(GamePane.java:716)
at window.GamePane.timeIsUp(GamePane.java:810)
at window.GamePane$1.run(GamePane.java:74)
at java.lang.Thread.run(Unknown Source)
所以有我的线程:
public Thread getCounter() {
if(counter == null) {
counter = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 5; i >= 0; i--) {
try {
System.out.println(i);
if(i == 0) {
System.out.println("Time is up");
timeIsUp();
System.out.println("test");
break;
}
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
});
counter.setDaemon(true);
}
return counter;
}
出现从 5 到 0 的数字和“时间到了”,但随后出现错误。
这是我在计数器达到 0 时尝试调用的函数:
public void timeIsUp() {
getCounter().interrupt(); // stopping the thread? I tried without this and it's still the same error
if (turn+1 < nbPlayers) {
turn++;
} else {
turn = 0;
}
getBtnDes().setDisable(false); // Disable a button
destroyCardPane(); // remove an AnchorPane from this.children()
}
如果你们需要其他东西、类或函数来帮助我,请在评论中告诉我,我会尽快添加。
谢谢
解决方案
所有 UI 操作都必须在 FX 应用程序线程上执行。您可以Platform.runLater(...)
从后台线程调用并将其传递给Runnable
将在 FX 应用程序线程上执行的。
这里真的不需要中断现有的线程。如果你重新排序代码以便你先睡觉,然后检查,那么线程无论如何都会退出:
public Thread getCounter() {
if(counter == null) {
counter = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 5; i >= 0; i--) {
try {
Thread.sleep(1000);
System.out.println(i);
if(i == 0) {
System.out.println("Time is up");
Platform.runLater(this::timeIsUp);
System.out.println("test");
break;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
});
counter.setDaemon(true);
}
return counter;
}
现在您可以从方法中删除中断调用timeIsUp()
。
public void timeIsUp() {
if (turn+1 < nbPlayers) {
turn++;
} else {
turn = 0;
}
getBtnDes().setDisable(false); // Disable a button
destroyCardPane(); // remove an AnchorPane from this.children()
}
请注意,您可以在没有线程的情况下完成所有这些操作,使用Timeline
:
Timeline timeline = new Timeline();
for (int i = 5 ; i >= 0 ; i=--) {
final int count = i ;
KeyFrame frame = new KeyFrame(Duration.seconds(5-i), e -> System.out.println(count));
timeline.getKeyFrames().add(frame);
}
timeline.setOnFinished(e -> {
System.out.println("Time is up");
timeIsUp();
});
timeline.play();
这里没有阻塞(因此不需要线程),关键帧的事件处理程序和onFinished
处理程序都在 FX Application Thread 上执行,因此在那里更新 UI 是安全的。
推荐阅读
- r - R data.table:如何使用作为向量提供的列名通过引用更新行?
- c# - 我的 do while 循环无法中断。我不知道怎么了?当键入 && 时它会中断,但如果我键入 || 则不会
- c# - 单词数组中单词的结束位置不正确
- python - 如何创建一个显示每个 bin 概率的 seaborn 图?
- sql - Stripe Sigma 查询返回已完成交易的汇率
- javascript - TypeError 无法读取 TypeScript、NextJS 项目上未定义的属性“cwd”
- python - python mac/windows计算问题
- typescript - 打字稿:除非使用关键字“as”,否则传播运算符不起作用
- c# - 我可以依赖为较旧的 .NET 标准构建的项目吗?
- pnpm - 为什么 pnpm 在顶层链接某些依赖项?