/** * Returns the thread to the pool. * Called by threads as they are becoming idel. */ protected synchronized void returnController(ControlRunnable c) { if(0 == currentThreadCount || stopThePool) { c.terminate(); return; } // atomic currentThreadsBusy--; pool[currentThreadCount - currentThreadsBusy - 1] = c; notify(); }
/** * Stop the thread 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(); } }
/** * Called by the monitor thread to harvest idle threads. */ 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 --; } } }
public void stop() { this.terminate(); }