File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "threading" Watch "threading" New topic


Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a file here, but I am confused about what int availableThread does. And why that when "this" Thread comes out of waiting state that availableThread has to decrement.import java.util.Vector;
public class ThreadPool implements Runnable {
public final static int DEFAULT_MINIMUM_SIZE = 0;
public final static int DEFAULT_MAXIMUM_SIZE = Integer.MAX_VALUE;
public final static long DEFAULT_RELEASE_DELAY = 10 * 1000;
protected int minimumSize;
protected int maximumSize;
protected int currentSize;
protected int availableThreads;
protected long releaseDelay;
protected Vector taskList;
public ThreadPool(int minSize, int maxSize, long delay) {
minimumSize = minSize;
maximumSize = maxSize;
releaseDelay = delay;
taskList = new Vector(100);
availableThreads = 0;
public ThreadPool() {
public synchronized void setMinimumSize(int minSize) {
minimumSize = minSize;
public synchronized int getMinimumSize() {
return minimumSize;
public synchronized void setMaximumSize(int maxSize) {
maximumSize = maxSize;
public synchronized int getMaximumSize() {
return maximumSize;
public synchronized void setReleaseDelay(long delay) {
releaseDelay = delay;
public synchronized long getReleaseDelay() {
return releaseDelay;
public synchronized void addTask(Runnable runnable) {
if (availableThreads > 0) {
else {
if (currentSize < maximumSize) {<br /> Thread t = new Thread(this);<br /> currentSize++;<br /> t.start();<br /> }<br /> }<br /> }<br /> public void run() {<br /> Runnable task;<br /> while (true) {<br /> synchronized (this) {<br /> if (currentSize > maximumSize) {
task = getNextTask();
if (task == null) {
try {
} catch (InterruptedException ie) {};
task = getNextTask();
if (task == null) {
if (currentSize <= minimumSize) {<br /> continue;<br /> }<br /> currentSize--;<br /> break;<br /> }<br /> }<br /> }<br /> try {<br />;<br /> } catch (Exception e) {<br /> System.err.println("Uncaught exception:");<br /> e.printStackTrace(System.err);<br /> }<br /> }<br /> }<br /> protected synchronized Runnable getNextTask() {<br /> Runnable task = null;<br /> if (taskList.size() > 0) {
task = (Runnable)(taskList.elementAt(0));
return task;
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
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

[This message has been edited by Dave Turner (edited April 18, 2001).]
I agree. Here's the link:
subject: threading