package org.jboss.axis.components.threadpool;
import org.jboss.axis.i18n.Messages;
import org.jboss.logging.Logger;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public class ThreadPool
{
private static Logger log = Logger.getLogger(ThreadPool.class.getName());
public static final long MAX_THREADS = 100;
protected Map threads = new Hashtable();
protected long threadcount;
public boolean _shutdown;
public void cleanup()
throws InterruptedException
{
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 boolean isShutdown()
{
synchronized (this)
{
return _shutdown && threadcount == 0;
}
}
public boolean isShuttingDown()
{
synchronized (this)
{
return _shutdown;
}
}
public long getWorkerCount()
{
synchronized (this)
{
return threadcount;
}
}
public void addWorker(Runnable worker)
{
if (log.isDebugEnabled())
{
log.debug("Enter: ThreadPool::addWorker");
}
if (_shutdown ||
threadcount == MAX_THREADS)
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 void interruptAll()
{
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 void shutdown()
{
if (log.isDebugEnabled())
{
log.debug("Enter: ThreadPool::shutdown");
}
synchronized (this)
{
_shutdown = true;
}
interruptAll();
if (log.isDebugEnabled())
{
log.debug("Exit: ThreadPool::shutdown");
}
}
public void safeShutdown()
{
if (log.isDebugEnabled())
{
log.debug("Enter: ThreadPool::safeShutdown");
}
synchronized (this)
{
_shutdown = true;
}
if (log.isDebugEnabled())
{
log.debug("Exit: ThreadPool::safeShutdown");
}
}
public synchronized void awaitShutdown()
throws InterruptedException
{
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 boolean awaitShutdown(long timeout)
throws InterruptedException
{
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;
}
long start = System.currentTimeMillis();
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 void workerDone(Runnable worker,
boolean restart)
{
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");
}
}
}