[jboss-cvs] JBoss Messaging SVN: r5045 - trunk/src/main/org/jboss/messaging/util.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Sep 30 06:52:12 EDT 2008


Author: timfox
Date: 2008-09-30 06:52:12 -0400 (Tue, 30 Sep 2008)
New Revision: 5045

Modified:
   trunk/src/main/org/jboss/messaging/util/OrderedExecutorFactory.java
Log:
https://jira.jboss.org/jira/browse/JBMESSAGING-1333


Modified: trunk/src/main/org/jboss/messaging/util/OrderedExecutorFactory.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/OrderedExecutorFactory.java	2008-09-30 02:00:10 UTC (rev 5044)
+++ trunk/src/main/org/jboss/messaging/util/OrderedExecutorFactory.java	2008-09-30 10:52:12 UTC (rev 5045)
@@ -18,72 +18,109 @@
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */ 
+ */
 
 package org.jboss.messaging.util;
 
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.LinkedList;
-import java.util.Set;
 import java.util.concurrent.Executor;
 
 /**
- * This factory creates a hierarchy of Executor which shares the threads of the
- * parent Executor (typically, the root parent is a Thread pool).
+ * A OrderedExecutorFactory2
+ *
+ * @author <a href="mailto:david.lloyd at jboss.com">David LLoyd</a>
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
  * 
- * @author <a href="david.lloyd at jboss.com">David Lloyd</a>
- * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- * 
- * @version <tt>$Revision$</tt>
- * 
  */
 public final class OrderedExecutorFactory implements ExecutorFactory
 {
    private final Executor parent;
-   private final Set<ChildExecutor> runningChildren = Collections.synchronizedSet(new HashSet<ChildExecutor>());
 
+   /**
+    * Construct a new instance delegating to the given parent executor.
+    *
+    * @param parent the parent executor
+    */
    public OrderedExecutorFactory(final Executor parent)
    {
       this.parent = parent;
    }
 
+   /**
+    * Get an executor that always executes tasks in order.
+    *
+    * @return an ordered executor
+    */
    public Executor getExecutor()
    {
-      return new ChildExecutor();
+      return new OrderedExecutor(parent);
    }
 
-   private final class ChildExecutor implements Executor, Runnable
+   private static final class OrderedExecutor implements Executor
    {
+      // @protectedby tasks
       private final LinkedList<Runnable> tasks = new LinkedList<Runnable>();
 
-      public void execute(Runnable command)
+      // @protectedby tasks
+      private boolean running;
+
+      private final Executor parent;
+
+      private final Runnable runner;
+
+      /**
+       * Construct a new instance.
+       *
+       * @param parent the parent executor
+       */
+      public OrderedExecutor(final Executor parent)
       {
-         synchronized (tasks)
+         this.parent = parent;
+         
+         runner = new Runnable()
          {
-            tasks.add(command);
-            if (tasks.size() == 1 && runningChildren.add(this))
+            public void run()
             {
-               parent.execute(this);
+               for (;;)
+               {
+                  final Runnable task;
+                  synchronized (tasks)
+                  {
+                     task = tasks.poll();
+                     if (task == null)
+                     {
+                        running = false;
+                        return;
+                     }
+                  }
+                  try
+                  {
+                     task.run();
+                  }
+                  catch (Throwable t)
+                  {
+                     // eat it!
+                  }
+               }
             }
-         }
+         };
       }
 
-      public void run()
+      /**
+       * Run a task.
+       *
+       * @param command the task to run.
+       */
+      public void execute(Runnable command)
       {
-         for (;;)
+         synchronized (tasks)
          {
-            final Runnable task;
-            synchronized (tasks)
+            tasks.add(command);
+            if (!running)
             {
-               task = tasks.poll();
-               if (task == null)
-               {
-                  runningChildren.remove(this);
-                  return;
-               }
+               running = true;
+               parent.execute(runner);
             }
-            task.run();
          }
       }
    }




More information about the jboss-cvs-commits mailing list