FileDocCategorySizeDatePackage
RxTaskPool.javaAPI DocApache Tomcat 6.0.145005Fri Jul 20 04:20:36 BST 2007org.apache.catalina.tribes.transport

RxTaskPool

public class RxTaskPool extends Object
author
not attributable
version
1.0

Fields Summary
List
idle
A very simple thread pool class. The pool size is set at construction time and remains fixed. Threads are cycled through a FIFO idle queue.
List
used
Object
mutex
boolean
running
private static int
counter
private int
maxTasks
private int
minTasks
private TaskCreator
creator
Constructors Summary
public RxTaskPool(int maxTasks, int minTasks, TaskCreator creator)

        // fill up the pool with worker threads
        this.maxTasks = maxTasks;
        this.minTasks = minTasks;
        this.creator = creator;
    
Methods Summary
public intavailable()

        return idle.size();
    
protected voidconfigureTask(AbstractRxTask task)

        synchronized (task) {
            task.setTaskPool(this);
//            task.setName(task.getClass().getName() + "[" + inc() + "]");
//            task.setDaemon(true);
//            task.setPriority(Thread.MAX_PRIORITY);
//            task.start();
        }
    
public intgetMaxThreads()

        return maxTasks;
    
public intgetMinThreads()

        return minTasks;
    
public AbstractRxTaskgetRxTask()
Find an idle worker thread, if any. Could return null.

        AbstractRxTask worker = null;
        synchronized (mutex) {
            while ( worker == null && running ) {
                if (idle.size() > 0) {
                    try {
                        worker = (AbstractRxTask) idle.remove(0);
                    } catch (java.util.NoSuchElementException x) {
                        //this means that there are no available workers
                        worker = null;
                    }
                } else if ( used.size() < this.maxTasks && creator != null) {
                    worker = creator.createRxTask();
                    configureTask(worker);
                } else {
                    try { mutex.wait(); } catch ( java.lang.InterruptedException x ) {Thread.currentThread().interrupted();}
                }
            }//while
            if ( worker != null ) used.add(worker);
        }
        return (worker);
    
public org.apache.catalina.tribes.transport.RxTaskPool$TaskCreatorgetTaskCreator()

        return this.creator;
    
private static synchronized intinc()


         
        return counter++;
    
public voidreturnWorker(AbstractRxTask worker)
Called by the worker thread to return itself to the idle pool.

        if ( running ) {
            synchronized (mutex) {
                used.remove(worker);
                //if ( idle.size() < minThreads && !idle.contains(worker)) idle.add(worker);
                if ( idle.size() < maxTasks && !idle.contains(worker)) idle.add(worker); //let max be the upper limit
                else {
                    worker.setDoRun(false);
                    synchronized (worker){worker.notify();}
                }
                mutex.notify();
            }
        }else {
            worker.setDoRun(false);
            synchronized (worker){worker.notify();}
        }
    
public voidsetMaxTasks(int maxThreads)

        this.maxTasks = maxThreads;
    
public voidsetMinTasks(int minThreads)

        this.minTasks = minThreads;
    
public voidstop()

        running = false;
        synchronized (mutex) {
            Iterator i = idle.iterator();
            while ( i.hasNext() ) {
                AbstractRxTask worker = (AbstractRxTask)i.next();
                returnWorker(worker);
                i.remove();
            }
        }