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,607
  • 8
  • 67
  • 107
  • 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
  • 321,522
  • 82
  • 660
  • 783
  • 1
    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
  • 133,369
  • 30
  • 199
  • 275