首页 > 解决方案 > LatchCountdown 在 Platform.runLater 任务上失败

问题描述

我想使用 JavaFX 可视化我的堆排序算法,每次交换数组中的元素后,我还交换 a 中对应的两个barsbarChart。该算法运行良好,但在更新 GUI 时,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();

锁存器被声明为类变量,并且没有在其他地方修改:

final CountDownLatch latch = new CountDownLatch(1);

这里是可运行的代码片段:

@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);
}

任何想法,为什么不考虑闩锁?

标签: javajavafx

解决方案


推荐阅读