Method from org.apache.tomcat.util.threads.ThreadPool Detail: |
public void addThread(Thread t,
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(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() {
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 --;
}
}
}
Called by the monitor thread to harvest idle threads. |
public static ThreadPool createThreadPool(boolean jmx) {
return new ThreadPool();
}
Create a ThreadPool instance. |
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 MonitorRunnable getMonitor() {
return monitor;
}
|
public String getName() {
return name;
}
|
public int getSequence() {
return sequence;
}
|
public String[] getThreadParam() {
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;
}
Return an array with the current "param" ( XXX better name ? )
of each thread. This is typically the last request. |
public int getThreadPriority() {
return threadPriority;
}
Returns the priority level of current and
future threads in this pool. |
public String[] getThreadStatus() {
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;
}
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 ) |
public Enumeration getThreads() {
return threads.keys();
}
|
public int incSequence() {
return sequence++;
}
|
public boolean isDaemon() {
return isDaemon;
}
|
void log(String s) {
log.info(s);
//loghelper.flush();
} Deprecated!
|
protected synchronized void notifyThreadEnd(ControlRunnable c) {
currentThreadsBusy--;
currentThreadCount --;
notify();
}
Inform the pool that the specific thread finish.
Called by the ControlRunnable.run() when the runnable
throws an exception. |
protected void openThreads(int toOpen) {
if(toOpen > maxThreads) {
toOpen = maxThreads;
}
for(int i = currentThreadCount ; i < toOpen ; i++) {
pool[i - currentThreadsBusy] = new ControlRunnable(this);
}
currentThreadCount = toOpen;
}
|
public void removeThread(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(ControlRunnable c) {
if(0 == currentThreadCount || stopThePool) {
c.terminate();
return;
}
// atomic
currentThreadsBusy--;
pool[currentThreadCount - currentThreadsBusy - 1] = c;
notify();
}
Returns the thread to the pool.
Called by threads as they are becoming idel. |
public void run(Runnable r) {
ControlRunnable c = findControlRunnable();
c.runIt(r);
}
|
public void runIt(ThreadPoolRunnable r) {
if(null == r) {
throw new NullPointerException();
}
ControlRunnable c = findControlRunnable();
c.runIt(r);
}
Executes a given Runnable on a thread in the pool, block if needed. |
public void setDaemon(boolean b) {
isDaemon=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. |
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(String name) {
this.name = name;
}
|
public synchronized void setThreadPriority(int threadPriority) {
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);
}
}
Sets the thread priority for current
and future threads in this pool. |
public synchronized void shutdown() {
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 String threadStatusString() {
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();
}
Debug display of the stage of each thread. The return is html style,
for display in the console ( it can be easily parsed too ). |