|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.exolab.core.threadPool.ThreadPool
public class ThreadPool
Thread manager that maintains a list of worker threads and allocates them to execute objects asynchronously. This enables execution without the overhead of thread creation.
The thread pool is constructed with a fixed number of threads. The number of threads cannot be increased after the pool is created, but there can be multiple thread pools.
There are two methods to execute threads:
execute(java.lang.Runnable)
- this executes an Runnable
object using
the first available worker thread. If no threads are available,
the call blocks until one is free.queue(java.lang.Runnable)
- this queues an Runnable
object for later
execution by the first available worker thread. It returns immediately
without blocking.
CompletionListener
Constructor Summary | |
---|---|
ThreadPool(int threads)
Create a new thread pool with the requested number of threads. |
|
ThreadPool(int threads,
boolean daemon)
Create a new thread pool with the requested number of threads. |
|
ThreadPool(java.lang.String name,
int threads)
Create a new thread pool with the requested number of threads. |
|
ThreadPool(java.lang.String name,
int threads,
boolean daemon)
Create a new thread pool with the requested number of threads. |
|
ThreadPool(java.lang.ThreadGroup group,
int threads,
boolean daemon)
Create a new thread pool with the requested number of threads. |
Method Summary | |
---|---|
void |
execute(java.lang.Runnable target)
Execute the Runnable object using the next available
worker thread. |
void |
queue(java.lang.Runnable target)
Queue the Runnable object for execution using the next
available worker thread.Unlike execute(java.lang.Runnable) , this method does not block. |
void |
queue(java.lang.Runnable target,
CompletionListener listener)
Queue the Runnable object for execution using the next
available worker thread. |
void |
stopRequestAllWorkers()
Shuts down and removes all threads. |
void |
stopRequestIdleWorkers()
Shuts down and removes any idle threads from the queue. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ThreadPool(int threads)
threads
- the number of threads to allocate to the pool
java.lang.IllegalArgumentException
- if the number of threads is less than 1public ThreadPool(int threads, boolean daemon)
threads
- the number of threads to allocate to the pooldaemon
- if true
all threads will be daemon threads
java.lang.IllegalArgumentException
- if the number of threads is less than 1public ThreadPool(java.lang.String name, int threads)
name
- the name of the thread groupthreads
- the number of threads to allocate to the pool
java.lang.IllegalArgumentException
- if the number of threads is less than 1public ThreadPool(java.lang.String name, int threads, boolean daemon)
name
- the name of the thread groupthreads
- the number of threads to allocate to the pooldaemon
- if true
all threads will be daemon threads
java.lang.IllegalArgumentException
- if the number of threads is less than 1public ThreadPool(java.lang.ThreadGroup group, int threads, boolean daemon)
group
- the thread groupthreads
- the number of threads to allocate to the pooldaemon
- if true
all threads will be daemon threads
java.lang.IllegalArgumentException
- if the number of threads is less than 1Method Detail |
---|
public void execute(java.lang.Runnable target) throws java.lang.InterruptedException
Runnable
object using the next available
worker thread. This method will block until a worker thread is
available for use - once a worker thread becomes available it
will return immediately.
target
- the work to perform
java.lang.InterruptedException
- if this thread is interrupted externallypublic void queue(java.lang.Runnable target)
Runnable
object for execution using the next
available worker thread.execute(java.lang.Runnable)
, this method does not block.
target
- the work to performpublic void queue(java.lang.Runnable target, CompletionListener listener)
Runnable
object for execution using the next
available worker thread. When the object completes execution, the
completion listener will be notified.execute(java.lang.Runnable)
, this method does not block.
target
- the work to performlistener
- the listener to notify when the object has completed
executingpublic void stopRequestIdleWorkers()
public void stopRequestAllWorkers()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |