I use an ExecutorService
to execute a task. This task can recursively create other tasks which are submitted to the same ExecutorService
and those child tasks can do that, too.
I now have the problem that I want to wait until all the tasks are done (that is, all tasks are finished and they did not submit new ones) before I continue.
I cannot call ExecutorService.shutdown()
in the main thread because this prevents new tasks from being accepted by the ExecutorService
.
And Calling ExecutorService.awaitTermination()
seems to do nothing if shutdown
hasn't been called.
So I am kinda stuck here. It can't be that hard for the ExecutorService
to see that all workers are idle, can it? The only inelegant solution I could come up with is to directly use a ThreadPoolExecutor
and query its getPoolSize()
every once in a while. Is there really no better way do do that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…