|
qflib 0.99 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.qfs.lib.util.ThreadPool
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.
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 |
public ThreadPool(int min, int avg, int max)
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.public ThreadPool(int min, int avg, int max, java.lang.ThreadGroup group)
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 ThreadGroupMethod Detail |
public void execute(java.lang.Runnable run)
run
- The runnable whose run method will be called.public java.lang.Object executeWatched(ThreadPool.UnsafeRunnable run, long timeout, long callid) throws java.lang.Throwable
cancelCall
or
the timeout is exceeded, interrupt the thread and return execution to
the caller.
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.
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.public void cancelCall(long callid)
executeWatched
.
callid
- The id of the task.
|
qflib 0.99 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |