You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:26:04 UTC

[sling-org-apache-sling-commons-threads] 15/27: SLING-2540 : Allow the ThreadPool to process Callable/Runnable and return a Future. Apply patch from Timothee Maret

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.threads-3.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-threads.git

commit 540daa40c5abb485d925f063fab21bbfd0be8858
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Jul 18 14:36:54 2012 +0000

    SLING-2540 : Allow the ThreadPool to process Callable/Runnable and return a Future. Apply patch from Timothee Maret
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/threads@1362967 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../apache/sling/commons/threads/ThreadPool.java   | 19 ++++++++
 .../commons/threads/impl/DefaultThreadPool.java    | 56 +++++++++++++++++-----
 .../commons/threads/impl/ThreadPoolFacade.java     | 18 ++++++-
 4 files changed, 80 insertions(+), 15 deletions(-)

diff --git a/pom.xml b/pom.xml
index b774988..b9e5d2a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,7 +58,7 @@
                             org.apache.sling.commons.threads.impl.Activator
                         </Bundle-Activator>
                         <Export-Package>
-                            org.apache.sling.commons.threads;version=3.1.0
+                            org.apache.sling.commons.threads;version=3.2.0
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.commons.threads.impl
diff --git a/src/main/java/org/apache/sling/commons/threads/ThreadPool.java b/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
index d06d701..746206c 100644
--- a/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
+++ b/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
@@ -16,6 +16,9 @@
  */
 package org.apache.sling.commons.threads;
 
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
 /**
  * The thread pool interface allows to start runnables by
  * getting threads from a managed pool.
@@ -29,6 +32,22 @@ public interface ThreadPool {
     void execute(Runnable runnable);
 
     /**
+     * Submits a callable for execution
+     * @param callable The {@link Callable} to submit
+     * @return A {@link Future} representing pending completion of the {@link Callable}
+     * @since 3.2
+     */
+    <T> Future<T> submit(Callable<T> callable);
+
+    /**
+     * Submits a runnable for execution
+     * @param runnable The {@link Runnable} to submit
+     * @return A {@link Future} representing pending completion of the {@link Runnable}
+     * @since 3.2
+     */
+    Future<?> submit(Runnable runnable);
+
+    /**
      * The name of the thread pool.
      */
     String getName();
diff --git a/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java b/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
index dee1524..81b8740 100644
--- a/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
+++ b/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
@@ -17,7 +17,9 @@
 package org.apache.sling.commons.threads.impl;
 
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.RejectedExecutionHandler;
 import java.util.concurrent.SynchronousQueue;
@@ -152,23 +154,36 @@ public class DefaultThreadPool
     /**
      * @see org.apache.sling.commons.threads.ThreadPool#execute(java.lang.Runnable)
      */
-    public void execute(Runnable runnable) {
-        if ( this.executor == null ) {
-            throw new IllegalStateException("Thread pool " + this.name + " is already shutdown.");
-        }
+    public void execute(final Runnable runnable) {
+        checkExecutor();
         if ( runnable != null ) {
-            if ( this.logger.isDebugEnabled() ) {
-                this.logger.debug("Executing runnable: {}, pool={}, active={}, corePoolSize={}, maxPoolSize={}, queueSize={}",
-                        new Object[] {runnable,
-                                      this.name,
-                                      this.executor.getActiveCount(),
-                                      this.executor.getCorePoolSize(),
-                                      this.executor.getMaximumPoolSize(),
-                                      this.executor.getQueue().size()});
+            if ( logger.isDebugEnabled() ) {
+                logOperation("Executing runnable: ", runnable);
             }
+            executor.execute(runnable);
+        }
+    }
 
-        	this.executor.execute(runnable);
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#submit(java.util.concurrent.Callable)
+     */
+    public <T> Future<T> submit(final Callable<T> callable) {
+        checkExecutor();
+        if ( logger.isDebugEnabled() ) {
+            logOperation("Submitting callable: ", callable);
         }
+        return executor.submit(callable);
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#submit(java.lang.Runnable)
+     */
+    public Future<?> submit(final Runnable runnable) {
+        checkExecutor();
+        if ( logger.isDebugEnabled() ) {
+            logOperation("Submitting runnable: ", runnable);
+        }
+        return executor.submit(runnable);
     }
 
     /**
@@ -203,4 +218,19 @@ public class DefaultThreadPool
     public ThreadPoolExecutor getExecutor() {
         return this.executor;
     }
+
+    private void checkExecutor() {
+        if ( this.executor == null ) {
+            throw new IllegalStateException("Thread pool " + this.name + " is already shutdown.");
+        }
+    }
+
+    private void logOperation(final String msg, final Object obj) {
+        logger.debug("{} {}, pool={}, active={}, corePoolSize={}, maxPoolSize={}, queueSize={}",
+                new Object[] {msg, obj, name,
+                        executor.getActiveCount(),
+                        executor.getCorePoolSize(),
+                        executor.getMaximumPoolSize(),
+                        executor.getQueue().size()});
+    }
 }
diff --git a/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java b/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
index 0135e3c..94c8198 100644
--- a/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
+++ b/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.commons.threads.impl;
 
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
 import java.util.concurrent.ThreadPoolExecutor;
 
 import org.apache.sling.commons.threads.ThreadPool;
@@ -43,11 +45,25 @@ public final class ThreadPoolFacade implements ThreadPool {
     /**
      * @see org.apache.sling.commons.threads.ThreadPool#execute(java.lang.Runnable)
      */
-    public void execute(Runnable runnable) {
+    public void execute(final Runnable runnable) {
         this.delegatee.execute(runnable);
     }
 
     /**
+     * @see org.apache.sling.commons.threads.ThreadPool#submit(java.util.concurrent.Callable)
+     */
+    public <T> Future<T> submit(final Callable<T> callable) {
+        return this.delegatee.submit(callable);
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#submit(java.lang.Runnable)
+     */
+    public Future<?> submit(final Runnable runnable) {
+        return this.delegatee.submit(runnable);
+    }
+
+    /**
      * @see org.apache.sling.commons.threads.ThreadPool#getConfiguration()
      */
     public ThreadPoolConfig getConfiguration() {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.