Java Platform.runLater任务上的闭锁倒计时失败
我想使用JavaFX可视化我的堆排序算法,在数组中的元素每次交换之后,我还交换Java Platform.runLater任务上的闭锁倒计时失败,java,javafx,Java,Javafx,我想使用JavaFX可视化我的堆排序算法,在数组中的元素每次交换之后,我还交换条形图中两个相应的条形图的值。该算法工作正常,但在更新GUI时,runnable似乎不会阻止下一个runnable的执行 这就是为什么我的图表混乱(值没有按顺序交换)。请参见下图: 这是我的可运行代码片段: new Thread(new Runnable() { @Override public void run() { System.o
条形图中两个相应的条形图的值。该算法工作正常,但在更新GUI时,runnable
似乎不会阻止下一个runnable的执行
这就是为什么我的图表混乱(值没有按顺序交换)。请参见下图:
这是我的可运行代码片段:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Await");
try {
latch.await();
} catch (InterruptedException ex) {
}
// queuing the done notification into the javafx thread
Platform.runLater(new Runnable() {
@Override
public void run() {
controller.updateChart(parentIndex, childIndex);
}
});
}
}).start();
@Override
public void updateChart(int index1, int index2) {
Object tempObject = series.getData().get(index2);
series.getData().set(index2, series.getData().get(index1));
series.getData().set(index1, tempObject);
barChartCopy.getData().setAll(series);
}
闩锁声明为类变量,不在其他地方修改:
final CountDownLatch latch = new CountDownLatch(1);
这里是runnable正在执行的代码片段:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Await");
try {
latch.await();
} catch (InterruptedException ex) {
}
// queuing the done notification into the javafx thread
Platform.runLater(new Runnable() {
@Override
public void run() {
controller.updateChart(parentIndex, childIndex);
}
});
}
}).start();
@Override
public void updateChart(int index1, int index2) {
Object tempObject = series.getData().get(index2);
series.getData().set(index2, series.getData().get(index1));
series.getData().set(index1, tempObject);
barChartCopy.getData().setAll(series);
}
知道为什么不考虑闩锁吗?我们无法知道,除非看到您在其他地方如何修改闩锁。我将闩锁声明为类变量(请参阅更新的代码),请提供一个示例来演示此问题。您从未调用倒计时()
形成您显示的代码。此外,即使传递给runLater
的Runnable
中的代码调用了CountDownLatch
上的方法,线程也将永远等待,因为触发CountDownLatch
调用的逻辑不会安排在您开始等待时。。。假设这些可运行项中的多个减少了同一个锁存,这也可能是有问题的…那么我应该在哪里添加倒计时@fabian