You are viewing a plain text version of this content. The canonical link for it is here.
Posted to droids-commits@incubator.apache.org by rf...@apache.org on 2011/12/02 05:06:28 UTC

svn commit: r1209360 - /incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java

Author: rfrovarp
Date: Fri Dec  2 05:06:27 2011
New Revision: 1209360

URL: http://svn.apache.org/viewvc?rev=1209360&view=rev
Log:
Fixing DROIDS-155
Only shut everything down if no threads are executing. This alone will allow threads to die too quickly, but at least the one doing work can continue.

Add in these two and threads wait a while longer:
Stagger startup of threads in case the pool is small. This will help provide time for the first thread to add more items.
If the pool is empty, wait a little while and check again. This too is useful on startup.
Increase the wait time from 250 ms to 1000 ms. 
All of this combined allows me to use a large thread count, but yet only start with one URL.

Cleaned up a couple of warnings Eclipse was throwing.

Modified:
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java

Modified: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java?rev=1209360&r1=1209359&r2=1209360&view=diff
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java (original)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java Fri Dec  2 05:06:27 2011
@@ -29,7 +29,6 @@ import org.apache.droids.api.Task;
 import org.apache.droids.api.TaskExceptionHandler;
 import org.apache.droids.api.TaskExceptionResult;
 import org.apache.droids.api.TaskMaster;
-import org.apache.droids.api.TaskMaster.ExecutionState;
 import org.apache.droids.api.WorkMonitor;
 import org.apache.droids.api.Worker;
 import org.slf4j.Logger;
@@ -42,7 +41,8 @@ public class MultiThreadedTaskMaster<T e
 {
 
   protected final Logger log = LoggerFactory.getLogger(AbstractDroid.class);
-  private final long TICKLE_TIME = 250L;
+  private final long TICKLE_TIME = 1000L;
+  
   /**
    * The execution state
    */
@@ -99,7 +99,13 @@ public class MultiThreadedTaskMaster<T e
       this.pool.setCorePoolSize(this.poolSize);
     }
 
+    // Stagger startup
     for (int i = 0; i < poolSize; i++) {
+      try {
+        Thread.sleep(TICKLE_TIME);
+      } catch(InterruptedException ignored) {
+        log.error("", ignored);
+      }
       pool.execute(new TaskExecutor(droid));
     }
   }
@@ -263,10 +269,14 @@ public class MultiThreadedTaskMaster<T e
     {
       super.afterExecute(r, thrwbl);
 
+      
+      
       // try to reexecute the task runner while
       // the task queue is not empty and while the pool
       // is still completing the execution of tasks.
+      @SuppressWarnings("unchecked")
       TaskExecutor taskExecutor = (TaskExecutor) r;
+      
       while (taskExecutor.getQueue().size() > 0 || getQueue().size() > 0) {
         if (taskExecutor.getQueue().size() > 0) {
           execute(r);
@@ -278,10 +288,13 @@ public class MultiThreadedTaskMaster<T e
           log.error("", e);
         }
       }
-
+      
       taskExecutor.getDroid().finished();
       state = ExecutionState.COMPLETED;
-      shutdownNow();
+      // If this point is reached, a count of one means this completed thread
+      if(this.getActiveCount() == 1) {
+        shutdown();
+      }
 
     }
   }
@@ -310,6 +323,7 @@ public class MultiThreadedTaskMaster<T e
       return queue;
     }
 
+    @SuppressWarnings("unused")
     public Worker getWorker()
     {
       return worker;
@@ -320,6 +334,15 @@ public class MultiThreadedTaskMaster<T e
     {
       // poll the last task
       T task = queue.poll();
+      
+      if(task == null) {
+        try {
+          Thread.sleep(TICKLE_TIME);
+        } catch (InterruptedException e) {
+          log.error("", e);
+        }
+        task = queue.poll();
+      }
 
       // execute the task
       if (task != null) {