我对多线程很陌生,这就是我想要做的事情:
如何检查所有已启动的线程是否已完成?
我通过这样做启动它们
for (Auftrag auftrag: auftragsliste) {
RunnableFS thread = new RunnableFS(auftrag, optionen, elmafs);
thread.start();
}
// I want to do something here after all my above started threads have finished
我知道thread.join()
我可以实现一个主线程等待直到另一个完成的点。但是,如果我在for循环中执行此操作,我将回到单线程:(
答案 0 :(得分:3)
你可以维护一个RunnableFS
的列表,在启动所有线程之后,你可以遍历它们并执行join()
(顺便说一句,我不知道什么是RunnableFS
)
List<RunnableFS> threads = new ArrayList<>();
for (Auftrag auftrag: auftragsliste) {
RunnableFS thread = new RunnableFS(auftrag, optionen, elmafs);
thread.start();
threads.add(thread);
}
// Later
for(RunnableFS thread: threads){
thread.join();
}
答案 1 :(得分:2)
CountDownLatch或CyclicBarrier是实现类似用例的其他替代方法。