qflib 0.99

de.qfs.lib.util
Class ThreadPool

java.lang.Object
  extended byde.qfs.lib.util.ThreadPool

public class ThreadPool
extends java.lang.Object

A Thread pool manages a number of Threads to execute asynchronous calls. It helps to avoid Thread creation overhead by keeping the Threads alive and waiting for something to do.

The number of live Threads can be customized with the parameters to its constructor. Threads are assigned first come first serve. The priority of of the calling Thread does not make any difference.

Calls are executed by passing a Runnable to execute. Its run Method will be called from the next available Thread. Additionally, the ThreadPool offers the ability to run "watched" tasks with the executeWatched method. These tasks are run in a background pool thread while the caller is blocked. If the task is cancelled or a timeout is exceeded before the task is done, the call is interupted and control returns to the caller. This is very handy for RMI calls and other non-interruptable tasks.

Author:
Gregor Schmid

Nested Class Summary
static class ThreadPool.CancelledException
          This exception is thrown when a call executed with executeWatched is explicitely cancelled with cancelCall.
static class ThreadPool.TimedOutException
          This exception is thrown when a call executed with executeWatched times out and is interrupted by the WatchDog.
static interface ThreadPool.UnsafeRunnable
          This interface is similar to Runnable, except that the run method may return a value and throw any kind of Exception.
 
Constructor Summary
ThreadPool(int min, int avg, int max)
          Create a new ThreadPool.
ThreadPool(int min, int avg, int max, java.lang.ThreadGroup group)
          Create a new ThreadPool.
 
Method Summary
 void cancelCall(long callid)
          Cancel a call executed with executeWatched.
 void execute(java.lang.Runnable run)
          Execute a runnable in the next available Thread.
 java.lang.Object executeWatched(ThreadPool.UnsafeRunnable run, long timeout, long callid)
          Execute a task in the next available thread and block until the task is finished.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(int min,
                  int avg,
                  int max)
Create a new ThreadPool.

Parameters:
min - Initial number of threads to create.
avg - Don't release threads unless there are more than avg Threads around.
max - Create at most that many Threads.

ThreadPool

public ThreadPool(int min,
                  int avg,
                  int max,
                  java.lang.ThreadGroup group)
Create a new ThreadPool.

Parameters:
min - Initial number of threads to create.
avg - Don't release threads unless there are more than avg Threads around.
max - Create at most that many Threads.
group - Create all threads in this ThreadGroup
Method Detail

execute

public void execute(java.lang.Runnable run)
Execute a runnable in the next available Thread. If there is no Thread currently available and the maximum number of Threads is reached, the calling Thread will be blocked, until a Thread is available.

Parameters:
run - The runnable whose run method will be called.

executeWatched

public java.lang.Object executeWatched(ThreadPool.UnsafeRunnable run,
                                       long timeout,
                                       long callid)
                                throws java.lang.Throwable
Execute a task in the next available thread and block until the task is finished. If the task is cancelled by cancelCall or the timeout is exceeded, interrupt the thread and return execution to the caller.

Parameters:
run - The runnable whose run method will be called.
timeout - Timeout after which the task is interrupted. 0 for no timeout.
callid - An id that uniquely identifies the task. It is the callers responsibility to ensure that ids are unique.
Returns:
The result of the UnsafeRunnable's run method.
Throws:
ThreadPool.CancelledException - If the task is cancelled.
ThreadPool.TimedOutException - If the task is interrupted after the timeout.
java.lang.Throwable - If the runnable throws anything.
Since:
0.99.0

cancelCall

public void cancelCall(long callid)
Cancel a call executed with executeWatched.

Parameters:
callid - The id of the task.
Since:
0.99.0

qflib 0.99