Methods Summary |
---|
public void | addThread(java.lang.Thread t, org.apache.tomcat.util.threads.ThreadPool$ControlRunnable cr)
threads.put( t, cr );
for( int i=0; i<listeners.size(); i++ ) {
ThreadPoolListener tpl=(ThreadPoolListener)listeners.elementAt(i);
tpl.threadStart(this, t);
}
|
public void | addThreadPoolListener(org.apache.tomcat.util.threads.ThreadPool$ThreadPoolListener tpl)
listeners.addElement( tpl );
|
protected void | adjustLimits()
if(maxThreads <= 0) {
maxThreads = MAX_THREADS;
} else if (maxThreads < MAX_THREADS_MIN) {
log.warn(sm.getString("threadpool.max_threads_too_low",
new Integer(maxThreads),
new Integer(MAX_THREADS_MIN)));
maxThreads = MAX_THREADS_MIN;
}
if(maxSpareThreads >= maxThreads) {
maxSpareThreads = maxThreads;
}
if(maxSpareThreads <= 0) {
if(1 == maxThreads) {
maxSpareThreads = 1;
} else {
maxSpareThreads = maxThreads/2;
}
}
if(minSpareThreads > maxSpareThreads) {
minSpareThreads = maxSpareThreads;
}
if(minSpareThreads <= 0) {
if(1 == maxSpareThreads) {
minSpareThreads = 1;
} else {
minSpareThreads = maxSpareThreads/2;
}
}
|
protected synchronized void | checkSpareControllers()Called by the monitor thread to harvest idle threads.
if(stopThePool) {
return;
}
if((currentThreadCount - currentThreadsBusy) > maxSpareThreads) {
int toFree = currentThreadCount -
currentThreadsBusy -
maxSpareThreads;
for(int i = 0 ; i < toFree ; i++) {
ControlRunnable c = pool[currentThreadCount - currentThreadsBusy - 1];
c.terminate();
pool[currentThreadCount - currentThreadsBusy - 1] = null;
currentThreadCount --;
}
}
|
public static org.apache.tomcat.util.threads.ThreadPool | createThreadPool(boolean jmx)Create a ThreadPool instance.
return new ThreadPool();
|
private org.apache.tomcat.util.threads.ThreadPool$ControlRunnable | findControlRunnable()
ControlRunnable c=null;
if ( stopThePool ) {
throw new IllegalStateException();
}
// Obtain a free thread from the pool.
synchronized(this) {
while (currentThreadsBusy == currentThreadCount) {
// All threads are busy
if (currentThreadCount < maxThreads) {
// Not all threads were open,
// Open new threads up to the max number of idel threads
int toOpen = currentThreadCount + minSpareThreads;
openThreads(toOpen);
} else {
logFull(log, currentThreadCount, maxThreads);
// Wait for a thread to become idel.
try {
this.wait();
}
// was just catch Throwable -- but no other
// exceptions can be thrown by wait, right?
// So we catch and ignore this one, since
// it'll never actually happen, since nowhere
// do we say pool.interrupt().
catch(InterruptedException e) {
log.error("Unexpected exception", e);
}
if( log.isDebugEnabled() ) {
log.debug("Finished waiting: CTC="+currentThreadCount +
", CTB=" + currentThreadsBusy);
}
// Pool was stopped. Get away of the pool.
if( stopThePool) {
break;
}
}
}
// Pool was stopped. Get away of the pool.
if(0 == currentThreadCount || stopThePool) {
throw new IllegalStateException();
}
// If we are here it means that there is a free thread. Take it.
int pos = currentThreadCount - currentThreadsBusy - 1;
c = pool[pos];
pool[pos] = null;
currentThreadsBusy++;
}
return c;
|
public int | getCurrentThreadCount()
return currentThreadCount;
|
public int | getCurrentThreadsBusy()
return currentThreadsBusy;
|
public boolean | getDaemon()
return isDaemon;
|
public static int | getDebug()
return 0;
|
public int | getMaxSpareThreads()
return maxSpareThreads;
|
public int | getMaxThreads()
return maxThreads;
|
public int | getMinSpareThreads()
return minSpareThreads;
|
public org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable | getMonitor()
return monitor;
|
public java.lang.String | getName()
return name;
|
public int | getSequence()
return sequence;
|
public java.lang.String[] | getThreadParam()Return an array with the current "param" ( XXX better name ? )
of each thread. This is typically the last request.
String status[]=new String[ threads.size()];
Iterator it=threads.keySet().iterator();
for( int i=0; ( i<status.length && it.hasNext()); i++ ) {
ThreadWithAttributes twa=(ThreadWithAttributes)
it.next();
Object o=twa.getParam(this);
status[i]=(o==null)? null : o.toString();
}
return status;
|
public int | getThreadPriority()Returns the priority level of current and
future threads in this pool.
return threadPriority;
|
public java.lang.String[] | getThreadStatus()Return an array with the status of each thread. The status
indicates the current request processing stage ( for tomcat ) or
whatever the thread is doing ( if the application using TP provide
this info )
String status[]=new String[ threads.size()];
Iterator it=threads.keySet().iterator();
for( int i=0; ( i<status.length && it.hasNext()); i++ ) {
ThreadWithAttributes twa=(ThreadWithAttributes)
it.next();
status[i]=twa.getCurrentStage(this);
}
return status;
|
public java.util.Enumeration | getThreads()
return threads.keys();
|
public int | incSequence()
return sequence++;
|
public boolean | isDaemon()
return isDaemon;
|
void | log(java.lang.String s)
log.info(s);
//loghelper.flush();
|
private static void | logFull(org.apache.juli.logging.Log loghelper, int currentThreadCount, int maxThreads)
if( logfull ) {
log.error(sm.getString("threadpool.busy",
new Integer(currentThreadCount),
new Integer(maxThreads)));
logfull=false;
} else if( log.isDebugEnabled() ) {
log.debug("All threads are busy " + currentThreadCount + " " +
maxThreads );
}
|
protected synchronized void | notifyThreadEnd(org.apache.tomcat.util.threads.ThreadPool$ControlRunnable c)Inform the pool that the specific thread finish.
Called by the ControlRunnable.run() when the runnable
throws an exception.
currentThreadsBusy--;
currentThreadCount --;
notify();
|
protected void | openThreads(int toOpen)Create missing threads.
if(toOpen > maxThreads) {
toOpen = maxThreads;
}
for(int i = currentThreadCount ; i < toOpen ; i++) {
pool[i - currentThreadsBusy] = new ControlRunnable(this);
}
currentThreadCount = toOpen;
|
public void | removeThread(java.lang.Thread t)
threads.remove(t);
for( int i=0; i<listeners.size(); i++ ) {
ThreadPoolListener tpl=(ThreadPoolListener)listeners.elementAt(i);
tpl.threadEnd(this, t);
}
|
protected synchronized void | returnController(org.apache.tomcat.util.threads.ThreadPool$ControlRunnable c)Returns the thread to the pool.
Called by threads as they are becoming idel.
if(0 == currentThreadCount || stopThePool) {
c.terminate();
return;
}
// atomic
currentThreadsBusy--;
pool[currentThreadCount - currentThreadsBusy - 1] = c;
notify();
|
public void | run(java.lang.Runnable r)
ControlRunnable c = findControlRunnable();
c.runIt(r);
|
public void | runIt(ThreadPoolRunnable r)Executes a given Runnable on a thread in the pool, block if needed.
if(null == r) {
throw new NullPointerException();
}
ControlRunnable c = findControlRunnable();
c.runIt(r);
|
public void | setDaemon(boolean b)The default is true - the created threads will be
in daemon mode. If set to false, the control thread
will not be daemon - and will keep the process alive.
isDaemon=b;
|
public void | setMaxSpareThreads(int maxSpareThreads)
this.maxSpareThreads = maxSpareThreads;
|
public void | setMaxThreads(int maxThreads)
this.maxThreads = maxThreads;
|
public void | setMinSpareThreads(int minSpareThreads)
this.minSpareThreads = minSpareThreads;
|
public void | setName(java.lang.String name)
this.name = name;
|
public synchronized void | setThreadPriority(int threadPriority)Sets the thread priority for current
and future threads in this pool.
if(log.isDebugEnabled())
log.debug(getClass().getName() +
": setPriority(" + threadPriority + "): here.");
if (threadPriority < Thread.MIN_PRIORITY) {
throw new IllegalArgumentException("new priority < MIN_PRIORITY");
} else if (threadPriority > Thread.MAX_PRIORITY) {
throw new IllegalArgumentException("new priority > MAX_PRIORITY");
}
// Set for future threads
this.threadPriority = threadPriority;
Enumeration currentThreads = getThreads();
Thread t = null;
while(currentThreads.hasMoreElements()) {
t = (Thread) currentThreads.nextElement();
t.setPriority(threadPriority);
}
|
public synchronized void | shutdown()Stop the thread pool
if(!stopThePool) {
stopThePool = true;
if (monitor != null) {
monitor.terminate();
monitor = null;
}
for(int i = 0; i < currentThreadCount - currentThreadsBusy; i++) {
try {
pool[i].terminate();
} catch(Throwable t) {
/*
* Do nothing... The show must go on, we are shutting
* down the pool and nothing should stop that.
*/
log.error("Ignored exception while shutting down thread pool", t);
}
}
currentThreadsBusy = currentThreadCount = 0;
pool = null;
notifyAll();
}
|
public synchronized void | start()
stopThePool=false;
currentThreadCount = 0;
currentThreadsBusy = 0;
adjustLimits();
pool = new ControlRunnable[maxThreads];
openThreads(minSpareThreads);
if (maxSpareThreads < maxThreads) {
monitor = new MonitorRunnable(this);
}
|
public java.lang.String | threadStatusString()Debug display of the stage of each thread. The return is html style,
for display in the console ( it can be easily parsed too ).
StringBuffer sb=new StringBuffer();
Iterator it=threads.keySet().iterator();
sb.append("<ul>");
while( it.hasNext()) {
sb.append("<li>");
ThreadWithAttributes twa=(ThreadWithAttributes)
it.next();
sb.append(twa.getCurrentStage(this) ).append(" ");
sb.append( twa.getParam(this));
sb.append( "</li>\n");
}
sb.append("</ul>");
return sb.toString();
|