this.numIdleThreads = 0;
this.idleThreads = new PooledThread[minThreads];
for(int i=0; i<this.idleThreads.length; i++)
- this.idleThreads[i] = new PooledThread();
+ new PooledThread();
}
public synchronized void start(Runnable r) {
private class PooledThread extends Thread {
private Runnable runnable = null;
public PooledThread() {
- synchronized(ThreadPool.this) {
- if (numIdleThreads >= idleThreads.length)
- throw new Error("this should not happen");
- numThreads++;
- idleThreads[numIdleThreads++] = this;
- }
+ synchronized(ThreadPool.this) { numThreads++; }
start();
}
public synchronized void start(Runnable runnable) {
public void run() {
try {
while(true) {
+ synchronized(this) {
+ if (runnable==null)
+ synchronized(ThreadPool.this) {
+ /* if the idle array is full, just let ourselves die */
+ if (numIdleThreads > minIdleThreads && numThreads > minThreads) return;
+ /* otherwise put ourselves back in the pool and release anybody who is waiting */
+ idleThreads[numIdleThreads++] = this;
+ ThreadPool.this.notifyAll();
+ }
+ }
try {
synchronized(this) { while (runnable==null) wait(); }
runnable.run();
} catch (Exception e) { Log.error(this, e); }
runnable = null;
- synchronized(ThreadPool.this) {
- /* if the idle array is full, just let ourselves die */
- if (numIdleThreads > minIdleThreads && numThreads > minThreads) return;
- /* otherwise put ourselves back in the pool and release anybody who is waiting */
- idleThreads[numIdleThreads++] = this;
- ThreadPool.this.notifyAll();
- }
}
} finally {
- numThreads--;
+ synchronized(ThreadPool.this) {
+ numThreads--;
+ }
}
}
}