Are We Done Yet?
Knowing when the thread pool has completed its task can be complex. There are several things that must be checked to determine whether the thread pool is completely done. First, the assignments array must be empty. However, an empty assignments array does not mean that the thread pool is done. There may still be threads inside of the pool that are executing tasks that were previously assigned.
To determine whether the thread pool is done or not, I have provided the Done class (shown in Listing 2), which is used internally by the thread pool. All that you have to do to make use of it is assign your tasks to the ThreadPool and call the complete method to wait for the ThreadPool to complete. The Done class is used to determine when no threads are still running.
The Done class has two methods that are called by the worker threads to track their progress. When a worker thread begins, it calls the Done class' workerBegin method. Similarly, when a worker thread completes, it calls the Done class's workerEnd method. These two methods allow the Done class to determine when no thread is currently running.
Most likely, you will not directly interact with the Done class. You will simply assign your tasks to the ThreadPool and wait for the ThreadPool to complete. In the next section, I will show you an example of how the whole thread pool fits together.