incubator-droids-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r734918 - in /incubator/droids/trunk: droids-core/src/main/java/org/apache/droids/api/ droids-core/src/main/java/org/apache/droids/impl/ droids-wicket/ droids-wicket/src/main/java/org/apache/droids/wicket/component/
Date Fri, 16 Jan 2009 06:19:50 GMT
Author: ryan
Date: Thu Jan 15 22:19:50 2009
New Revision: 734918

URL: http://svn.apache.org/viewvc?rev=734918&view=rev
Log:
use the pool for completedTasks and currentTasks rather then keeping a parallel Map

Modified:
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/api/TaskMaster.java
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/MultiThreadedTaskMaster.java
    incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/SequentialTaskMaster.java
    incubator/droids/trunk/droids-wicket/pom.xml
    incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.html
    incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.java

Modified: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/api/TaskMaster.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/api/TaskMaster.java?rev=734918&r1=734917&r2=734918&view=diff
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/api/TaskMaster.java
(original)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/api/TaskMaster.java
Thu Jan 15 22:19:50 2009
@@ -43,7 +43,7 @@
 
   ExecutionState getExecutionState();
 
-  int getCompletedTasks();
+  long getCompletedTasks();
 
   T getLastCompletedTask();
   

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=734918&r1=734917&r2=734918&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
Thu Jan 15 22:19:50 2009
@@ -18,8 +18,8 @@
 
 import java.util.Date;
 import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Condition;
@@ -41,7 +41,6 @@
   private static final long KEEP_ALIVE = 50000L;
   
   private PausableThreadPoolExecutor pool = null;
-  private ConcurrentHashMap<Long, WorkerRunner> runningWorker = null;
   private int maxThreads = 0;
   private TaskQueue<T> queue = null;
   private Droid<T> droid = null;
@@ -49,7 +48,6 @@
 
   private Date startedWorking = null;
   private Date finishedWorking = null;
-  private int completedTask = 0;
   private T lastCompletedTask = null;
   private ExecutionState state = ExecutionState.INITALIZED;
 
@@ -73,7 +71,6 @@
     // start the pool
     this.pool = new PausableThreadPoolExecutor(n, n, KEEP_ALIVE,
         TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>() );
-    this.runningWorker = new ConcurrentHashMap<Long, WorkerRunner>();
     
     startWorkers();
     log.info("Finshed invocation, waiting for workers to finish.");
@@ -83,16 +80,10 @@
   private void finishedWorker(WorkerRunner worker) 
   { 
     synchronized (this) {
-      long id = worker.getId();
-      if( runningWorker.remove(id) == null ) {
-        throw new RuntimeException("should remove something!");
-      }
       lastCompletedTask = worker.task;
-      
-      //int y = worker.getDepth() + 1;
       pool.remove(worker);
       if (log.isDebugEnabled()) {
-        log.debug("Worker '" + id + "' has finished.");
+        log.debug("Worker '" + worker.task.getId() + "' has finished.");
       }
 
       boolean terminate = false;
@@ -114,7 +105,7 @@
         }
       }
       
-      if (terminate || (runningWorker.size() == 0 & !queue.hasNext())) {
+      if (terminate || (pool.getActiveCount() == 0 && !queue.hasNext()) ) {
         shutdownAndAwaitTermination();
         
         long elapsed = System.currentTimeMillis() - startedWorking.getTime();
@@ -127,7 +118,6 @@
       } else if (queue.hasNext()) {
         startWorkers();
       }
-      completedTask++;
     }
   }
   
@@ -138,23 +128,27 @@
    */
   private void startWorkers(){
     
-    while( queue.hasNext() ) {
-      // The getActiveCount is often wrong for only a single thread!
-      // the javadocs say the number is approxiate, so in the case we only 
-      // see one active worker, we will assume there are none...
-      int activeWorkers = pool.getActiveCount();
-      if( activeWorkers == 1 && runningWorker.isEmpty() ) {
-        activeWorkers = 0;
-      }
-      if( activeWorkers >= getMaxThreads() ) {
-        return;  // don't make a new runner...
-      }
-      WorkerRunner runner = new WorkerRunner();
-      runningWorker.put(runner.getId(), runner);
-      if (log.isDebugEnabled()) {
-        log.debug("Starting worker '" + runner.getId() + "'");
+    try {
+      while( queue.hasNext() ) {
+        // The getActiveCount is often wrong for only a single thread!
+        // the javadocs say the number is approxiate, so in the case we only 
+        // see one active worker, we will assume there are none...
+        int activeWorkers = pool.getActiveCount();
+        if( activeWorkers == 1 ) { //&& runningWorker.isEmpty() ) {
+          activeWorkers = 0;
+        }
+        if( activeWorkers >= getMaxThreads() ) {
+          return;  // don't make a new runner...
+        }
+        WorkerRunner runner = new WorkerRunner();
+        if (log.isDebugEnabled()) {
+          log.debug("Starting worker '" + runner + "'");
+        }
+        pool.execute(runner);
       }
-      pool.execute(runner);
+    }
+    catch( RejectedExecutionException ex ) {
+      log.info( "humm", ex );
     }
   }
 
@@ -179,8 +173,8 @@
    */
   public  void setMaxThreads(int maxThreads) {
     if( pool != null && maxThreads != this.maxThreads ) {
+      pool.setMaximumPoolSize( maxThreads );
       pool.setCorePoolSize( maxThreads );
-      
       if( state == ExecutionState.RUNNING ) {
         startWorkers(); // fill up the queue with new workers...
       }
@@ -231,7 +225,7 @@
   /**
    * Wrapper to call finishedWorker() after 
    */
-  class WorkerRunner extends Thread {
+  class WorkerRunner implements Runnable {
     
     Date startTime;
     T task;
@@ -278,8 +272,8 @@
     }
   }
 
-  public int getCompletedTasks() {
-    return completedTask;
+  public long getCompletedTasks() {
+    return (pool==null) ? 0 : pool.getCompletedTaskCount();
   }
 
   public Date getFinishedWorking() {
@@ -298,7 +292,7 @@
     pool.awaitTermination(timeout, unit);
   }
   
-
+  
   /**
    * "pause" support
    */
@@ -315,6 +309,7 @@
       super( corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue );
     }
 
+    @Override
     protected void beforeExecute(Thread t, Runnable r) {
       super.beforeExecute(t, r);
       pauseLock.lock();

Modified: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/SequentialTaskMaster.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/SequentialTaskMaster.java?rev=734918&r1=734917&r2=734918&view=diff
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/SequentialTaskMaster.java
(original)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/impl/SequentialTaskMaster.java
Thu Jan 15 22:19:50 2009
@@ -130,7 +130,7 @@
   }
 
 
-  public int getCompletedTasks() {
+  public long getCompletedTasks() {
     return completedTask;
   }
 

Modified: incubator/droids/trunk/droids-wicket/pom.xml
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-wicket/pom.xml?rev=734918&r1=734917&r2=734918&view=diff
==============================================================================
--- incubator/droids/trunk/droids-wicket/pom.xml (original)
+++ incubator/droids/trunk/droids-wicket/pom.xml Thu Jan 15 22:19:50 2009
@@ -51,6 +51,12 @@
 		
     <dependency>
       <groupId>org.apache.wicket</groupId>
+      <artifactId>wicket-extensions</artifactId>
+      <version>${wicket.version}</version>
+    </dependency>
+		
+    <dependency>
+      <groupId>org.apache.wicket</groupId>
       <artifactId>wicket-spring</artifactId>
       <version>${wicket.version}</version>
       <type>jar</type>

Modified: incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.html
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.html?rev=734918&r1=734917&r2=734918&view=diff
==============================================================================
--- incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.html
(original)
+++ incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.html
Thu Jan 15 22:19:50 2009
@@ -18,6 +18,22 @@
 </ul>
 
 
+<div wicket:id="taskMaster">
+ <h3>Workers:</h3>
+ <!--
+ <ul wicket:id="workers">
+   <li wicket:id="task">
+     [<span wicket:id="time">00,00,10.xx</span>] 
+     <span wicket:id="text">Task 1 todo</span>
+     <a href="#">[watch]</a>
+   </li>
+ </ul>
+ -->
+ <p wicket:id="threads">
+     Concurrent threads: <span wicket:id="count">count</span>
+ </p>
+</div>
+
 
  <h3>Queue:</h3>
  <ul wicket:id="queue">

Modified: incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.java?rev=734918&r1=734917&r2=734918&view=diff
==============================================================================
--- incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.java
(original)
+++ incubator/droids/trunk/droids-wicket/src/main/java/org/apache/droids/wicket/component/DroidPanel.java
Thu Jan 15 22:19:50 2009
@@ -18,6 +18,7 @@
 
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
@@ -27,8 +28,10 @@
 import org.apache.droids.api.Task;
 import org.apache.droids.api.TaskMaster;
 import org.apache.droids.api.TaskQueue;
+import org.apache.droids.impl.MultiThreadedTaskMaster;
 import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
 import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableChoiceLabel;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.panel.Panel;
@@ -36,6 +39,7 @@
 import org.apache.wicket.markup.repeater.RefreshingView;
 import org.apache.wicket.model.AbstractReadOnlyModel;
 import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.PropertyModel;
 import org.apache.wicket.spring.injection.annot.SpringBean;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.time.Duration;
@@ -51,7 +55,7 @@
   TaskQueue taskQueue;
   
   @SpringBean( name="taskMaster" )
-  TaskMaster taskMaster;
+  MultiThreadedTaskMaster taskMaster;
 
   public DroidPanel(String id) {
     super(id);
@@ -86,9 +90,9 @@
       }
     }));
 
-    info.add( new Label( "completedTasks", new AbstractReadOnlyModel<Integer>() {
+    info.add( new Label( "completedTasks", new AbstractReadOnlyModel<Long>() {
       @Override
-      public Integer getObject() {
+      public Long getObject() {
         return droid.getTaskMaster().getCompletedTasks();
       }
     }));
@@ -122,9 +126,18 @@
     classinfo.add( new Label( "taskMaster", taskMaster.toString() ) );
     add( classinfo );
     
-    // QUEUE
+    // TASK MASTER
+    WebMarkupContainer tmDIV = new WebMarkupContainer( "taskMaster" );
+    WebMarkupContainer threads = new WebMarkupContainer( "threads" );
+    threads.add( new AjaxEditableChoiceLabel<Integer>("count", 
+        new PropertyModel<Integer>( taskMaster, "maxThreads" )
+    , Arrays.asList( 1,2,3,4,5,6,7,8 ) ) );  
+    tmDIV.add( threads );
+    add( tmDIV );
     
-
+    
+    
+    // QUEUE
     final WebMarkupContainer queueDIV = new WebMarkupContainer( "queue" );
     queueDIV.add( new RefreshingView<Task>("task")
     {



Mime
View raw message