X-Git-Url: http://git.megacz.com/?p=org.ibex.util.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FThreadPool.java;fp=src%2Forg%2Fibex%2Futil%2FThreadPool.java;h=0fb6d0761637932f1d4010a832f9d79b2734dd53;hp=16eeb83cec32acc9ccb77f30a3b55aaef992aad0;hb=54bab7d896a41243548918cc456992bc2e4c689f;hpb=95f48d5332312955e2fe0a2dc337769b1bd558ae diff --git a/src/org/ibex/util/ThreadPool.java b/src/org/ibex/util/ThreadPool.java index 16eeb83..0fb6d07 100644 --- a/src/org/ibex/util/ThreadPool.java +++ b/src/org/ibex/util/ThreadPool.java @@ -35,7 +35,7 @@ public final class ThreadPool { this.numIdleThreads = 0; this.idleThreads = new PooledThread[minThreads]; for(int i=0; i= idleThreads.length) - throw new Error("this should not happen"); - numThreads++; - idleThreads[numIdleThreads++] = this; - } + synchronized(ThreadPool.this) { numThreads++; } start(); } public synchronized void start(Runnable runnable) { @@ -76,21 +71,26 @@ public final class ThreadPool { 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--; + } } } }