我的应用程序通过调用Platform.runlater方法在FX线程上添加了多个Runnable,然后,我只想在FX平台队列中没有额外的Runnable时进行一些计算。但是我不知道正确的方式,有什么事件或回调机制来获取正确的时间吗?
目前,我强制应用程序线程随机休眠毫秒。
发布于 2016-09-23 17:41:31
这从一开始就不是一个好主意,因为您不知道其他代码使用Platform.runLater的是什么。此外,您不应该依赖这样的实现细节;就您所知,队列永远不会为空。
但是,您可以使用一个自定义类来发布这些Runnable,该类跟踪Runnable的数量,并在所有操作完成时通知您:
public class UpdateHandler {
     private final AtomicInteger count;
     private final Runnable completionHandler;
     public UpdateHandler(Runnable completionHandler, Runnable... initialTasks) {
         if (completionHandler == null || Stream.of(initialTasks).anyMatch(Objects::isNull)) {
             throw new IllegalArgumentException();
         }
         count = new AtomicInteger(initialTasks.length);
         this.completionHandler = completionHandler;
         for (Runnable r : initialTasks) {
             startTask(r);
         }
     }
     private void startTask(Runnable runnable) {
         Platform.runLater(() -> {
             runnable.run();
             if (count.decrementAndGet() == 0) {
                 completionHandler.run();
             }
         });
     }
     public void Runnable runLater(Runnable runnable) {
         if (runnable == null) {
             throw new IllegalArgumentException();
         }
         count.incrementAndGet();
         startTask(runnable);
     }
}https://stackoverflow.com/questions/39653295
复制相似问题