[jboss-cvs] JBossAS SVN: r100220 - projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 1 15:10:57 EST 2010


Author: david.lloyd at jboss.com
Date: 2010-02-01 15:10:57 -0500 (Mon, 01 Feb 2010)
New Revision: 100220

Added:
   projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/DelegatingDirectBlockingExecutor.java
Modified:
   projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/JBossExecutors.java
Log:
JBTHR-11: Add blocking variations of direct executors

Added: projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/DelegatingDirectBlockingExecutor.java
===================================================================
--- projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/DelegatingDirectBlockingExecutor.java	                        (rev 0)
+++ projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/DelegatingDirectBlockingExecutor.java	2010-02-01 20:10:57 UTC (rev 100220)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * 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.threads;
+
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+
+final class DelegatingDirectBlockingExecutor implements BlockingExecutor, DirectExecutor {
+    private final DirectExecutor delegate;
+
+    DelegatingDirectBlockingExecutor(final DirectExecutor delegate) {
+        this.delegate = delegate;
+    }
+
+    public void execute(final Runnable task) {
+        delegate.execute(task);
+    }
+
+    public void executeNonBlocking(final Runnable task) throws RejectedExecutionException {
+        delegate.execute(task);
+    }
+
+    public void executeBlocking(final Runnable task, final long timeout, final TimeUnit unit) throws RejectedExecutionException, InterruptedException {
+        try {
+            delegate.execute(task);
+        } catch (ExecutionInterruptedException e) {
+            if (Thread.interrupted()) {
+                throw new InterruptedException();
+            } else {
+                throw e;
+            }
+        }
+    }
+
+    public void executeBlocking(final Runnable task) throws RejectedExecutionException, InterruptedException {
+        try {
+            delegate.execute(task);
+        } catch (ExecutionInterruptedException e) {
+            if (Thread.interrupted()) {
+                throw new InterruptedException();
+            } else {
+                throw e;
+            }
+        }
+    }
+}

Modified: projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/JBossExecutors.java
===================================================================
--- projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/JBossExecutors.java	2010-02-01 19:41:16 UTC (rev 100219)
+++ projects/jboss-threads/trunk/jboss-threads/src/main/java/org/jboss/threads/JBossExecutors.java	2010-02-01 20:10:57 UTC (rev 100220)
@@ -52,6 +52,10 @@
     private static final DirectExecutorService REJECTING_EXECUTOR_SERVICE = new DelegatingDirectExecutorService(RejectingExecutor.INSTANCE);
     private static final DirectExecutorService DISCARDING_EXECUTOR_SERVICE = new DelegatingDirectExecutorService(DiscardingExecutor.INSTANCE);
 
+    private static final BlockingExecutor BLOCKING_DIRECT_EXECUTOR = new DelegatingDirectBlockingExecutor(SimpleDirectExecutor.INSTANCE);
+    private static final BlockingExecutor BLOCKING_REJECTING_EXECUTOR = new DelegatingDirectBlockingExecutor(RejectingExecutor.INSTANCE);
+    private static final BlockingExecutor BLOCKING_DISCARDING_EXECUTOR = new DelegatingDirectBlockingExecutor(DiscardingExecutor.INSTANCE);
+
     // ==================================================
     // DIRECT EXECUTORS
     // ==================================================
@@ -282,6 +286,37 @@
     }
 
     // ==================================================
+    // DIRECT BLOCKING EXECUTORS
+    // ==================================================
+
+    /**
+     * Get an executor which executes tasks in the current thread, which implements {@code BlockingExecutor}.
+     *
+     * @return the blocking direct executor
+     */
+    public static BlockingExecutor blockingDirectExecutor() {
+        return BLOCKING_DIRECT_EXECUTOR;
+    }
+
+    /**
+     * Get an executor which discards all tasks, which implements {@code BlockingExecutor}.
+     *
+     * @return the executor
+     */
+    public static BlockingExecutor blockingDiscardingExecutor() {
+        return BLOCKING_DISCARDING_EXECUTOR;
+    }
+
+    /**
+     * Get an executor which rejects all tasks, which implements {@code BlockingExecutor}.
+     *
+     * @return the executor
+     */
+    public static BlockingExecutor blockingRejectingExecutor() {
+        return BLOCKING_REJECTING_EXECUTOR;
+    }
+
+    // ==================================================
     // EXECUTORS
     // ==================================================
 
@@ -297,6 +332,18 @@
     }
 
     /**
+     * An executor which delegates to the given direct executor, but implements the blocking executor interface.
+     * Since direct executors always execute tasks in the current thread, no blocking is possible; therefore the
+     * methods of BlockingExecutors always succeed or fail instantly.
+     *
+     * @param delegate the delegate direct executor
+     * @return the blocking executor
+     */
+    public static BlockingExecutor directBlockingExecutor(final DirectExecutor delegate) {
+        return new DelegatingDirectBlockingExecutor(delegate);
+    }
+
+    /**
      * Create a wrapping executor for a delegate executor which creates an {@link #executorTask(DirectExecutor, Runnable)} for
      * each task.
      *




More information about the jboss-cvs-commits mailing list