org.ws4d.java.concurrency
Class ThreadPool

java.lang.Object
  extended by org.ws4d.java.concurrency.ThreadPool

public class ThreadPool
extends java.lang.Object

Implements a simple thread pool which allows dynamic creation of worker threads and automatic disposal of unused worker threads after the given period of time.

Usage example:

ThreadPool myThreadPool = new ThreadPool (3, 5000);
myThreadPool.executeOrAbort(runnable1); // essential tasks, which must be started immediately or aborted
myThreadPool.execute(runnable2); ... // common tasks, which will be started as soon as possible
myThreadPool.execute(runnableN);
myThreadPool.shutdown();


Constructor Summary
ThreadPool()
          The constructor of the ThreadPool class, creating a thread pool with default size and default timeout.
ThreadPool(int size)
          The constructor of the ThreadPool class with the default life duration of idle threads
ThreadPool(int size, long timeout)
          The constructor of the ThreadPool class.
 
Method Summary
 void execute(java.lang.Runnable task)
          Assigns tasks to the thread pool for execution.
 boolean executeOrAbort(java.lang.Runnable task)
          Assigns tasks to the thread pool for execution.
 void shutdown()
          Disposes of the thread pool.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool()
The constructor of the ThreadPool class, creating a thread pool with default size and default timeout.


ThreadPool

public ThreadPool(int size,
                  long timeout)
The constructor of the ThreadPool class.

Parameters:
size - maximal number of threads in the pool
timeout - life duration of idle thread pool worker

ThreadPool

public ThreadPool(int size)
The constructor of the ThreadPool class with the default life duration of idle threads

Parameters:
size - maximal number of threads in the pool
Method Detail

execute

public void execute(java.lang.Runnable task)
Assigns tasks to the thread pool for execution. Use this method for common non-critical tasks. The tasks will be started as soon as possible.

Parameters:
task - runnable which is assigned to the thread pool

executeOrAbort

public boolean executeOrAbort(java.lang.Runnable task)
Assigns tasks to the thread pool for execution. Use this method for essential tasks, which have to be immediately started or else aborted.

Parameters:
task - runnable which is assigned to the thread pool
Returns:
boolean returns true only if the runnable was started immediately, false if the runnable could not be started immediately and was aborted

shutdown

public void shutdown()
Disposes of the thread pool. New tasks will be ignored. The currently running threads will be executed until the end.