所以我在一个方法中有这个:
Runnable task = new PostLoadRunnable(tool, archive);
SwingUtilities.invokeLater(task);
但我想让当前方法在任务线程完成之前不会继续。我想做一些类似 join()
的事情,但我不知道该怎么做。 task.join()
和 Thread.task.join()
不起作用,而 Thread.currentThread().join()
不起作用不要给我任何选项来将其加入到我想要的线程中。
如何停止该方法直到任务完成?
最佳答案
您可以使用CountDownLatch
来完成此操作。在 PostLoadRunnable
run 方法中,您可以在 finally 部分包含以下代码。
public void run() {
try {
...
} finally {
latch.countDown();
}
}
CountDownLatch latch = CountDownLatch(1);
Runnable task = new PostLoadRunnable(tool, archive, latch);
SwingUtilities.invokeLater(task);
latch.await();
关于java - 在继续当前线程之前等待线程完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227491/