5

How can I check whether all tasks submitted to ThreadPoolExecutor are completed?

New tasks can be added from previously submitted tasks, so I can't call shutdown followed by awaitTermination nor invokeAll, as it will ignore these new tasks.

Sebastian Nowak
  • 5,507
  • 8
  • 65
  • 106
  • Related but different problem so not quite a dupe - http://stackoverflow.com/questions/3929361/how-to-wait-for-all-tasks-in-an-threadpoolexecutor-to-finish-without-shutting-do – djechlin May 14 '13 at 21:38

2 Answers2

7

yourThreadPoolExecutor.getActiveCount(); will return 0 when all tasks have completed.

assylias
  • 310,138
  • 72
  • 642
  • 762
  • I have a flacky test that may be related to using this. Reading the documenttion states that this number is an aproximation: "Returns the approximate number of threads that are actively executing tasks". – borjab Oct 26 '21 at 17:54
1

You can use ThreadPoolExecutor.getCompletedTaskCount()

Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266