Tom
Tom

Reputation: 7804

How to block until thread pool is empty... with an unknown number of threads?

Say I have a thread pool of some kind. I submit one thread to it and it goes on its merry way. I want to block the main thread until the pool is empty/all threads have finished, without busy waiting preferably.

But the thread can create and add its own threads to the pool (a quantity that is unknown beforehand)... so it is important that the pool may continue to accept and process threads normally while the main thread blocks. The context, though it doesn't matter: I'm performing a graph search.

I have found so many almost-solutions:

Likewise there are many threads on this topic, but they all involve programs that know how many threads are being created beforehand. I actually find it odd that it's a question not asked more often (or at all)!

Upvotes: 1

Views: 1437

Answers (1)

ewernli
ewernli

Reputation: 38615

As @Tom commented, ForkJoinPool seems adequate: tasks that are added to the pool can spawn additional subtasks in the pool. To block until all tasks have been processed, you use awaitTermination.

Note that tasks and threads aren't the same thing. You could of course have one thread per task, but that would be inefficient, especially if there are many tasks. Internally, the pool allocates a given numbers of threads, that will try to execute the tasks that are pending as quickly as possible.

When a task needs to spawn subtasks, you can use

  • invokeAll( listOfTasks ) -- the method blocks until all subtasks have completed. The subtasks are executed in the pool of the "parent" tasks.
  • getPool().execute( aTask ) -- you can queue subtasks without blocking a well.

The first option is the idiomatic way to use ForkJoinPool. It enables parallel divide and conquer algorithms (see the example in the tutorial). But the second option should work as well if a task spawns subtasks but doesn't care about their results.

Upvotes: 1

Related Questions