r27128 - trunk/freenet/src/freenet/support

[email protected] Tue, 21 Apr 2009 06:04:44 +0000
Newsgroups gmane.network.freenet.cvs
Message-ID <[email protected]>
Author: j16sdiz
Date: 2009-04-21 06:04:43 +0000 (Tue, 21 Apr 2009)
New Revision: 27128

Modified:
   trunk/freenet/src/freenet/support/PooledExecutor.java
Log:
Pass first job to MyThread with constructor

Modified: trunk/freenet/src/freenet/support/PooledExecutor.java
===================================================================
--- trunk/freenet/src/freenet/support/PooledExecutor.java	2009-04-21 00:23:44 UTC (rev 27127)
+++ trunk/freenet/src/freenet/support/PooledExecutor.java	2009-04-21 06:04:43 UTC (rev 27128)
@@ -63,8 +63,7 @@
 		if(prio < NativeThread.MIN_PRIORITY || prio > NativeThread.MAX_PRIORITY)
 			throw new IllegalArgumentException("Unreconized priority level : " + prio + '!');
 		while(true) {
-			MyThread t;
-			boolean mustStart = false;
+			MyThread t = null;
 			boolean miss = false;
 			synchronized(this) {
 				jobCount++;
@@ -80,37 +79,44 @@
 						ticker.queueTimedJob(job, jobName, 0, true);
 						return;
 					}
+					miss = true;
+				}
+			}
+			
+			// miss
+			if (miss) {
+				synchronized(this) {
 					// Will be coalesced by thread count listings if we use "@" or "for"
-					t = new MyThread("Pooled thread awaiting work @" + (threadCounter[prio - 1]), threadCounter[prio - 1], prio, !fromTicker);
+					t = new MyThread("Pooled thread awaiting work @" + (threadCounter[prio - 1]), job, threadCounter[prio - 1], prio, !fromTicker);
 					threadCounter[prio - 1]++;
+					runningThreads[prio - 1].add(t);
 					t.setDaemon(true);
-					mustStart = true;
-					miss = true;
+					t.setName(jobName + "(" + t.threadNo + ")");
+					jobMisses++;
+					
+					if(logMINOR)
+						Logger.minor(this, "Jobs: " + jobMisses + " misses of " + jobCount + " starting urgently " + jobName);
 				}
+				
+				t.start();
+				return;
 			}
+			
+			// use existing thread
 			synchronized(t) {
 				if(!t.alive)
 					continue;
 				if(t.nextJob != null)
 					continue;
 				t.nextJob = job;
-				if(!mustStart)
+				
 					// It is possible that we could get a wierd race condition with
 					// notify()/wait() signalling on a thread being used by higher
 					// level code. So we'd best use notifyAll().
 					t.notifyAll();
 			}
 			t.setName(jobName + "(" + t.threadNo + ")");
-			if(mustStart) {
-				t.start();
-				synchronized(this) {
-					runningThreads[prio - 1].add(t);
-					if(miss)
-						jobMisses++;
-					if(logMINOR)
-						Logger.minor(this, "Jobs: " + jobMisses + " misses of " + jobCount + " starting urgently " + jobName);
-				}
-			} else
+			
 				if(logMINOR)
 					synchronized(this) {
 						Logger.minor(this, "Not starting: Jobs: " + jobMisses + " misses of " + jobCount + " starting urgently " + jobName);
@@ -144,10 +150,11 @@
 		Runnable nextJob;
 		final long threadNo;
 
-		public MyThread(String defaultName, long threadCounter, int prio, boolean dontCheckRenice) {
+		public MyThread(String defaultName, Runnable firstJob, long threadCounter, int prio, boolean dontCheckRenice) {
 			super(defaultName, prio, dontCheckRenice);
 			this.defaultName = defaultName;
 			threadNo = threadCounter;
+			nextJob = firstJob;
 		}
 
 		@Override