FileDocCategorySizeDatePackage
ThreadPool.javaAPI DocApache Axis 1.46789Sat Apr 22 18:57:26 BST 2006org.apache.axis.components.threadpool

ThreadPool

public class ThreadPool extends Object
author
James M Snell (jasnell@us.ibm.com)

Fields Summary
protected static Log
log
public static final int
DEFAULT_MAX_THREADS
protected Map
threads
protected long
threadcount
public boolean
_shutdown
private int
maxPoolSize
Constructors Summary
public ThreadPool()


      
    
public ThreadPool(int maxPoolSize)

        this.maxPoolSize = maxPoolSize;
    
Methods Summary
public voidaddWorker(java.lang.Runnable worker)
Adds a new worker to the pool

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::addWorker");
        }
        if (_shutdown || threadcount == maxPoolSize) {
            throw new IllegalStateException(Messages.getMessage("illegalStateException00"));
        }
        Thread thread = new Thread(worker);
        threads.put(worker, thread);
        threadcount++;
        thread.start();
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::addWorker");
        }
    
public synchronized voidawaitShutdown()
Await shutdown of the worker

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::awaitShutdown");
        }
        if (!_shutdown)
            throw new IllegalStateException(Messages.getMessage("illegalStateException00"));
        while (threadcount > 0)
            wait();
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::awaitShutdown");
        }
    
public synchronized booleanawaitShutdown(long timeout)
Await shutdown of the worker

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::awaitShutdown");
        }
        if (!_shutdown)
            throw new IllegalStateException(Messages.getMessage("illegalStateException00"));
        if (threadcount == 0) {
            if (log.isDebugEnabled()) {
                log.debug("Exit: ThreadPool::awaitShutdown");
            }
            return true;
        }
        long waittime = timeout;
        if (waittime <= 0) {
            if (log.isDebugEnabled()) {
                log.debug("Exit: ThreadPool::awaitShutdown");
            }
            return false;
        }
        for (; ;) {
            wait(waittime);
            if (threadcount == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("Exit: ThreadPool::awaitShutdown");
                }
                return true;
            }
            waittime = timeout - System.currentTimeMillis();
            if (waittime <= 0) {
                if (log.isDebugEnabled()) {
                    log.debug("Exit: ThreadPool::awaitShutdown");
                }
                return false;
            }
        }
    
public voidcleanup()

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::cleanup");
        }
        if (!isShutdown()) {
          safeShutdown();
          awaitShutdown();
        }
        synchronized(this) {
          threads.clear();
          _shutdown = false;
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::cleanup");
        }
    
public longgetWorkerCount()
Returns the total number of currently active workers

        synchronized (this) {
            return threadcount;
        }
    
public voidinterruptAll()
Forcefully interrupt all workers

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::interruptAll");
        }
        synchronized (threads) {
            for (Iterator i = threads.values().iterator(); i.hasNext();) {
                Thread t = (Thread) i.next();
                t.interrupt();
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::interruptAll");
        }
    
public booleanisShutdown()
Returns true if all workers have been shutdown

        synchronized (this) {
            return _shutdown && threadcount == 0;
        }
    
public booleanisShuttingDown()
Returns true if all workers are in the process of shutting down

        synchronized (this) {
            return _shutdown;
        }
    
public voidsafeShutdown()
Forcefully shutdown the pool

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::safeShutdown");
        }
        synchronized (this) {
            _shutdown = true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::safeShutdown");
        }
    
public voidshutdown()
Forcefully shutdown the pool

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::shutdown");
        }
        synchronized (this) {
            _shutdown = true;
        }
        interruptAll();
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::shutdown");
        }
    
public voidworkerDone(java.lang.Runnable worker, boolean restart)
Used by MessageWorkers to notify the pool that it is done

        if (log.isDebugEnabled()) {
            log.debug("Enter: ThreadPool::workerDone");
        }
        synchronized(this) {
            threads.remove(worker);
            if (--threadcount == 0 && _shutdown) {
                notifyAll();
            }
            if (!_shutdown && restart) {
                addWorker(worker);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Exit: ThreadPool::workerDone");
        }