> >Opinions, Comments?
>
> The extensive documentation here may serve as a basis to start with
>
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/PooledE
xecutor.html
>
> Wolfgang.
>

I think this is really excellent and probably this should just be adapted to
use jakarta's class such as the FIFOQueue class.

In general the pool may have a preallocated amount of threads and accept
tasks with a

dispatchTask(Runnable runnable)

method which will run the threads it. The Pool will be configurable with
respect to the amount of threads, threads idle life. It will also contain
the same blocked execution policy which could always run the task right now
(by spawning an extra thread), wait until one is free if the pool is full or
throw an exception if the pool is full. We could also include a parameter
for creation steps to indicate how many threads be built when the maximum is
reached.

Finally an interrupt all should be also useful

I also like the idea of a ThreadFactory, and that could give a default
initial priority and name. Normal users just use an internal defaults
factory and advanced users could implement more advanced or customized
factories.

In the ObjectPool spirit I propose a ThreadPool and a ThreadPoolFactory
interfaces with their respective implementations. Something like:

public interface ThreadPool {
    public static final int RUNS = 0;
    public static final int BLOCKS = 1;
    public static final int FAIL = 2;

    /**
     * Executes a task
     * Depending on the policy the behavior varies bocking or spawning a new
thread
     * Should this throw an exception when the policy says that it's full?
     * Should we have an ThreadPoolException which encapsulates three
possible
     * situations:
     * - The Task was interrupted
     * - The Pool is full and no more threads are allowed
     * - An exception was thrown by the task
     */
    public dispatchTask(Runnable runnable) throws InterruptedException;

    /**
     * Return the number of Threads currently idle in the pool
     */
    public abstract int numIdle();

    /**
     * Return the number of Threads currently borrowed from the pool
     */
    public abstract int numActive() throws UnsupportedOperationException;

    /**
     * Interrupts all tasks
     *
     */
    public abstract void interruptsAll()

    /**
     * Changes the blocking policy to either RUN, BLOCKS, FAILS
     */
    public void setBlockedPolicy(int policy);
}

Reply via email to