ialize to use a maximum of 8 threads. static PooledExecutor pool = new PooledExecutor(8); } Here are some sample variants in initialization:
- Using a bounded buffer of 10 tasks, at least 4 threads (started only when needed due to incoming requests), but allowing up to 100 threads if the buffer gets full.
pool = new PooledExecutor(new BoundedBuffer(10), 100); pool.setMinimumPoolSize(4);
- Same as (1), except pre-start 9 threads, allowing them to die if they are not used for five minutes.
pool = new PooledExecutor(new BoundedBuffer(10), 100); pool.setMinimumPoolSize(4); pool.setKeepAliveTime(1000 * 60 * 5); pool.createThreads(9);
- Same as (2) except clients block if both the buffer is full and all 100 threads are busy:
pool = new PooledExecutor(new BoundedBuffer(10), 100); pool.setMinimumPoolSize(4); pool.setKeepAliveTime(1000 * 60 * 5); pool.waitWhenBlocked(); pool.createThreads(9);
- An unbounded queue serviced by exactly 5 threads:
pool = new PooledExecutor(new LinkedQueue()); pool.setKeepAliveTime(-1); // live forever pool.createThreads(5);
Usage notes.
Pools do not mesh well with using thread-specific storage via java.lang.ThreadLocal. ThreadLocal relies on the identity of a thread executing a particular task. Pools use the same thread to perform different tasks.
If you need a policy not handled by the parameters in this class consider writing a subclass.
Version note: Previous versions of this class relied on ThreadGroups for aggregate control. This has been removed, and the method interruptAll added, to avoid differences in behavior across JVMs.
[ Introduction to this package. ]