This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
This class defines a Thread pool. This is because of the overheads involved with creating threads. What this class does is continuously maintain a pool of Threads with which to run a large number of Runnable objects until the ThreadPool is stopped. If there are no available Threads when a Runnable object is received (using addTask( Runnable runnable);), then the following steps are taken 1. create a new Thread with the Runnable parameter object 2. The Thread then runs the Runnable object untils it finishes, when it then becomes available again. 3. increment the number of available threads variable 4. wait for a Runnable object to run 5. When it gets a Runnable object to run(), the waiting thread is notified then the available threads variable is decremented. 6. The notified Thread then gets the next available Runnable object (go back to 2.) If there are available Threads waiting when a Runnable object is received then 1 through to 4 have already happened. To answer your question, availableThread maintains the number of Threads waiting in the ThreadPool's run() method. When a waiting Thread in the ThreadPool is notified and comes out of the waiting state, it is because there is a Runnable object for the Thread to run(), so the Thread will no longer be available, and therefore availableThread must be decremented. Hope this helps dave
[This message has been edited by Dave Turner (edited April 18, 2001).]