【发布时间】:2018-05-07 19:30:06
【问题描述】:
我目前正在尝试从我的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()
}
如果你们需要其他东西、类或函数来帮助我,请在评论中告诉我,我会尽快添加。
谢谢
【问题讨论】:
标签: java multithreading function javafx