The ThreadPool Component

Commons ThreadPool is a component for working with pools of threads and asynchronously executing tasks.

Often inside application servers and containers the threading model is managed for you. However in some circumstances it can be useful to use your own worker thread pools to dispatch asynchronous work into.

In enterprise class systems typically JMS is used to perform some task asynchronously by another machine. However ThreadPools can be a simple and effective mechanism for asynchronous processing within the same process. This can be particularly useful for performing asynchronous processing in Servlet engines or Swing applications.

Please note that one of the reasons this component is still in the sandbox and has never had a 1.0 release is Doug Lea's excellent util.concurrent library. That library works with many JDKs, is full-featured, well-documented, and well-tested. That library is also a standard part of the JDK starting with JDK 1.5, as the java.util.concurrent library: it is specified in the JSR 166 standard.
All of this is not to say commons-sandbox-threadpool is useless. As with most Apache products, the development and release of this component is driven by user requests and developer interest.

Releases

See the downloads page for information on obtaining releases.

Documentation

The JavaDoc API documents are available online.

How to use a ThreadPool

Once you have a ThreadPool instance you can invoke functionality asynchronously on a ThreadPool via the invokeLater() method.

  // lets start with 5 threads
  ThreadPool threadPool = new DefaultThreadPool(5);    	
	
  // lets use a specific Runnable
  Runnable someTask = ...;
  threadPool.invokeLater( someTask );
	
  // lets just wrap up some code in a Runnable
  threadPool.invokeLater(
    new Runnable() {
      public void run() {
        someObject.doSomeSlowThing();
      }
    }		
  );	

Note that inside the run() method of your Runnable object you must handle Exceptions properly. Any unhandled runtime exception will be caught and logged using Commons Logging.