From 54bab7d896a41243548918cc456992bc2e4c689f Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 15 Jul 2007 23:08:51 +0000 Subject: [PATCH] ThreadPool bugfix darcs-hash:20070715230851-5007d-144d7b23a863e9c0866fd32d56a17d32ec934af4.gz --- src/org/ibex/util/ThreadPool.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) 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--; + } } } } -- 1.7.10.4