Commits

Anonymous committed 9493b53

QUARTZ-491 - fix synchronization of availCount

git-svn-id: http://svn.opensymphony.com/svn/quartz/trunk@60369f7d36a-ea1c-0410-88ea-9fd03e4c9665

  • Participants
  • Parent commits 90de400

Comments (0)

Files changed (1)

src/java/org/quartz/simpl/SimpleThreadPool.java

                 workers[i].setContextClassLoader(Thread.currentThread()
                         .getContextClassLoader());
             }
-            reportThreadReady();
+            availCount++;
             workers[i].start();
         }
     }
     }
 
     public int blockForAvailableThreads() {
-        if(availCount > 0) {
-            return availCount;
-        }
-
         synchronized(nextRunnableLock) {
+            if(availCount > 0) {
+                return availCount;
+            }
+
             while(availCount < 1  && !isShutdown) {
                 try {
                     nextRunnableLock.wait(1000);
      * is, the time used for waiting need not be short.
      * </p>
      */
-    private Runnable getNextRunnable() throws InterruptedException {
+    private Runnable getNextRunnable(boolean ranLastTime) throws InterruptedException {
         Runnable toRun = null;
 
         // Wait for new Runnable (see runInThread()) and notify runInThread()
         // in case the next Runnable is already waiting.
         synchronized (nextRunnableLock) {
+
+            if(ranLastTime) {
+                availCount++;
+                nextRunnableLock.notifyAll();
+            }
+
             if (nextRunnable == null) {
                 nextRunnableLock.wait(1000);
             }
         return toRun;
     }
 
-    private void reportThreadReady() {
-        synchronized(nextRunnableLock) {
-            availCount++;
-            nextRunnableLock.notifyAll();
-        }
-    }
-
     /*
      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      * 
         void shutdown() {
             run = false;
 
-            // @todo I'm not really sure if we should interrupt the thread.
             // Javadoc mentions that it interrupts blocked I/O operations as
             // well. Hence the job will most likely fail. I think we should
             // shut the work thread gracefully, by letting the job finish
         public void run() {
             boolean runOnce = (runnable != null);
 
+            boolean ran = false;
             while (run) {
-                boolean ran = false;
                 try {
                     if (runnable == null) {
-                        runnable = tp.getNextRunnable();
+                        runnable = tp.getNextRunnable(ran);
                     }
 
                     if (runnable != null) {
                         ran = true;
                         runnable.run();
                     }
+                    else {
+                        ran = false;
+                    }
                 } catch (InterruptedException unblock) {
                     // do nothing (loop will terminate if shutdown() was called
                     try {
                     if (runOnce) {
                         run = false;
                     }
-                    if (ran) {
-                        tp.reportThreadReady();
-                    }
                     runnable = null;
 
                     // repair the thread in case the runnable mucked it up...