jut.util.concurrent
Class BlockingThreadPool

java.lang.Object
  extended by jut.util.concurrent.BlockingThreadPool
All Implemented Interfaces:
Executor

public class BlockingThreadPool
extends Object
implements Executor

executor running tasks in a thread pool. other than any ThreadPoolExecutor, this executor's execute(Runnable) method blocks if the all threads are busy and the task queue is full. additionally, this executor can be configured to run tasks in the current thread instead of in a thread pool.

this class solves the problem that in the standard JDK there is no blocking Executor. this class is especially suited if thousands or millions of small tasks are created which could lead - with the standard ThreadPoolExecutor - either to high memory requirements or to RejectedExecutionExceptions.

Author:
Georg Dietrich

Constructor Summary
BlockingThreadPool(int poolSize, int queueSize)
          create a blocking executor running tasks either in a thread pool or in the current thread.
 
Method Summary
 void execute(Runnable command)
          execute the given command at some time in the future. the command is either executed in a pooled thread or in the calling thread, depending on the BlockingThreadPool configuration.
 void shutdown()
          initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. this method returns immediately. it does not wait for the task execution to finish. invocation has no additional effect if already shut down.
 void shutdownWait()
          initiates an orderly shutdown (see shutdown() and waits for the task execution to finish. to avoid deadlocks, this method must not be called from one of the thread pool's threads.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingThreadPool

public BlockingThreadPool(int poolSize,
                          int queueSize)
create a blocking executor running tasks either in a thread pool or in the current thread.

Parameters:
poolSize - - the number of threads to keep in the pool. if set to 0 or below, tasks are run in the current thread.
queueSize - - the number of tasks to accept in queue before blocking when calling execute(Runnable), i.e., initially a BlockingThreadPool will accept [poolSize] + [queueSize] tasks, blocking when more are added until other tasks are finished. if set to 0 or below, a 1-element queue is used.
Method Detail

execute

public void execute(Runnable command)
execute the given command at some time in the future. the command is either executed in a pooled thread or in the calling thread, depending on the BlockingThreadPool configuration.

Specified by:
execute in interface Executor
Parameters:
command - - the task to execute

shutdown

public void shutdown()
initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. this method returns immediately. it does not wait for the task execution to finish. invocation has no additional effect if already shut down.


shutdownWait

public void shutdownWait()
initiates an orderly shutdown (see shutdown() and waits for the task execution to finish. to avoid deadlocks, this method must not be called from one of the thread pool's threads.