org.exolab.core.threadPool
Class ThreadPool

java.lang.Object
  extended byorg.exolab.core.threadPool.ThreadPool

public class ThreadPool
extends java.lang.Object

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:

Version:
$Revision: 1.6 $ $Date: 2002/02/21 09:50:37 $
Author:
Jim Mourikis
See Also:
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

ThreadPool

public ThreadPool(int threads)
Create a new thread pool with the requested number of threads. Each thread will be allocated to a new ThreadGroup named 'ThreadPool-' where id is a number incremented for each new thread pool.

Parameters:
threads - the number of threads to allocate to the pool
Throws:
java.lang.IllegalArgumentException - if the number of threads is less than 1

ThreadPool

public ThreadPool(int threads,
                  boolean daemon)
Create a new thread pool with the requested number of threads. Each thread will be allocated to a new ThreadGroup named 'ThreadPool-' where id is a number incremented for each new thread pool.

Parameters:
threads - the number of threads to allocate to the pool
daemon - if true all threads will be daemon threads
Throws:
java.lang.IllegalArgumentException - if the number of threads is less than 1

ThreadPool

public ThreadPool(java.lang.String name,
                  int threads)
Create a new thread pool with the requested number of threads. Each thread will be allocated to a new ThreadGroup with the specified name.

Parameters:
name - the name of the thread group
threads - the number of threads to allocate to the pool
Throws:
java.lang.IllegalArgumentException - if the number of threads is less than 1

ThreadPool

public ThreadPool(java.lang.String name,
                  int threads,
                  boolean daemon)
Create a new thread pool with the requested number of threads. Each thread will be allocated to a new ThreadGroup with the specified name.

Parameters:
name - the name of the thread group
threads - the number of threads to allocate to the pool
daemon - if true all threads will be daemon threads
Throws:
java.lang.IllegalArgumentException - if the number of threads is less than 1

ThreadPool

public ThreadPool(java.lang.ThreadGroup group,
                  int threads,
                  boolean daemon)
Create a new thread pool with the requested number of threads. Each thread will be associated with the supplied thread group.

Parameters:
group - the thread group
threads - the number of threads to allocate to the pool
daemon - if true all threads will be daemon threads
Throws:
java.lang.IllegalArgumentException - if the number of threads is less than 1
Method Detail

execute

public void execute(java.lang.Runnable target)
             throws java.lang.InterruptedException
Execute the 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.

Parameters:
target - the work to perform
Throws:
java.lang.InterruptedException - if this thread is interrupted externally

queue

public 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.

Parameters:
target - the work to perform

queue

public void queue(java.lang.Runnable target,
                  CompletionListener listener)
Queue the Runnable object for execution using the next available worker thread. When the object completes execution, the completion listener will be notified.
Unlike execute(java.lang.Runnable), this method does not block.

Parameters:
target - the work to perform
listener - the listener to notify when the object has completed executing

stopRequestIdleWorkers

public void stopRequestIdleWorkers()
Shuts down and removes any idle threads from the queue. Working threads will not be shutdown. As working threads finish, and are added back to the idle queue this call needs to be made again to shut them down and remove them.


stopRequestAllWorkers

public void stopRequestAllWorkers()
Shuts down and removes all threads. First all idle threads are removed and shutdown, then all remaining work threads, are shutdown.



Copyright © 1999-2005 The Exolab Group. All Rights Reserved.