- public synchronized void prependTask(Runnable r) {
- if (idle.size() > 0) ((PooledThread)idle.remove()).setTask(r);
- else if (numThreads < maxThreads) new PooledThread(r);
- else tasks.prepend(r);
+ public synchronized void start(Runnable r) {
+ /* if no idle threads, wait for MINIMUM_DISPATCH_DELAY or until notified */
+ if (numIdleThreads == 0) try {
+ this.wait(MINIMUM_DISPATCH_DELAY);
+ } catch (Exception e) { Log.error(this, e); }
+
+ /* if there are idle threads, use one */
+ if (numIdleThreads > 0) {
+ numIdleThreads--;
+ idleThreads[numIdleThreads].start(r);
+ idleThreads[numIdleThreads] = null;
+ return;
+ }
+
+ /* otherwise, create a new thread */
+ new PooledThread().start(r);