sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-commons-threads] 04/33: Create own thread pool for eventing.
Date Tue, 07 Nov 2017 09:24:27 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit a2160409e97928deefe30aa0032b84373411420a
Author: Carsten Ziegeler <cziegeler@apache.org>
AuthorDate: Mon Feb 18 09:51:26 2008 +0000

    Create own thread pool for eventing.
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads@628671
13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/threads/ThreadPoolManager.java    | 42 +++++++++++-
 .../sling/threads/impl/DefaultThreadPool.java      | 55 +++++-----------
 .../threads/impl/DefaultThreadPoolManager.java     | 75 +++++++++++++++-------
 3 files changed, 106 insertions(+), 66 deletions(-)

diff --git a/src/main/java/org/apache/sling/threads/ThreadPoolManager.java b/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
index 603aa46..62610b4 100644
--- a/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
+++ b/src/main/java/org/apache/sling/threads/ThreadPoolManager.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.threads;
 
+import java.util.concurrent.ThreadFactory;
+
 /**
  * The <cod>ThreadPoolManager</code> manages thread pools.
  *
@@ -23,18 +25,54 @@ package org.apache.sling.threads;
  */
 public interface ThreadPoolManager {
 
+    /** The default thread pool name */
+    String DEFAULT_THREADPOOL_NAME = "default";
+
+    /** The thread pool policies. */
+    enum ThreadPoolPolicy {
+        ABORT,
+        DISCARD,
+        DISCARDOLDEST,
+        RUN
+    };
+
+    /** The default policy */
+    ThreadPoolPolicy DEFAULT_BLOCK_POLICY = ThreadPoolPolicy.RUN;
+
     /**
      * Add a new pool.
      * If a pool with the same name already exists, the new pool is not added
      * and false is returned.
-     * @param pool The pool
+     * @param pool The pool.
      * @return True if the pool could be added, false otherwise.
      */
     boolean add(ThreadPool pool);
 
     /**
-     * Get a thread pool
+     * Get a thread pool.
+     * If there is no thread pool with the given name, the default thread
+     * pool is returned.
      * @param name The name of the thread pool or null for the default pool.
      */
     ThreadPool get(String name);
+
+    /**
+     * Create a new thread pool.
+     * If a pool with the same name already exists, no new pool is created
+     * and <code>null</code> is returned.
+     * @param name Name must not be null.
+     * @param blockPolicy The thread pool policy or null for the default.
+     * @param factory A thread factory or null for the default favtory.
+     */
+    ThreadPool create(String name,
+                     int   minPoolSize,
+                     int   maxPoolSize,
+                     final int queueSize,
+                     long  keepAliveTime,
+                     ThreadPoolPolicy blockPolicy,
+                     final boolean shutdownGraceful,
+                     final int shutdownWaitTimeMs,
+                     final ThreadFactory factory,
+                     final int   priority,
+                     final boolean isDaemon);
 }
diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
index c3fb849..1682abf 100644
--- a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
+++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
@@ -59,31 +59,12 @@ public class DefaultThreadPool
      * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME}
      *               is used
      */
-    public DefaultThreadPool(final String name) {
-        this(DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME,
-                DefaultThreadPoolManager.DEFAULT_MIN_POOL_SIZE,
-                DefaultThreadPoolManager.DEFAULT_MAX_POOL_SIZE,
-                DefaultThreadPoolManager.DEFAULT_QUEUE_SIZE,
-                DefaultThreadPoolManager.DEFAULT_KEEP_ALIVE_TIME,
-                DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY,
-                DefaultThreadPoolManager.DEFAULT_SHUTDOWN_GRACEFUL,
-                DefaultThreadPoolManager.DEFAULT_SHUTDOWN_WAIT_TIME,
-             null,
-             DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY,
-             DefaultThreadPoolManager.DEFAULT_DAEMON_MODE);
-    }
-
-    /**
-     * Create a new thread pool.
-     * @param name - The name of the thread pool. If null {@link DefaultThreadPoolManager#DEFAULT_THREADPOOL_NAME}
-     *               is used
-     */
     public DefaultThreadPool(final String name,
                              int   minPoolSize,
                              int   maxPoolSize,
                              final int queueSize,
                              long  keepAliveTime,
-                             String blockPolicy,
+                             ThreadPoolManager.ThreadPoolPolicy blockPolicy,
                              final boolean shutdownGraceful,
                              final int shutdownWaitTimeMs,
                              final ThreadFactory factory,
@@ -156,26 +137,20 @@ public class DefaultThreadPool
         if ( blockPolicy == null ) {
             blockPolicy = DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY;
         }
-        final RejectedExecutionHandler handler;
-        if (DefaultThreadPoolManager.POLICY_ABORT.equalsIgnoreCase(blockPolicy)) {
-            handler = new ThreadPoolExecutor.AbortPolicy();
-        } else if (DefaultThreadPoolManager.POLICY_DISCARD.equalsIgnoreCase(blockPolicy))
{
-            handler = new ThreadPoolExecutor.AbortPolicy();
-        } else if (DefaultThreadPoolManager.POLICY_DISCARD_OLDEST.equalsIgnoreCase(blockPolicy))
{
-            handler = new ThreadPoolExecutor.AbortPolicy();
-        } else if (DefaultThreadPoolManager.POLICY_RUN.equalsIgnoreCase(blockPolicy)) {
-            handler = new ThreadPoolExecutor.AbortPolicy();
-        } else {
-            final StringBuffer msg = new StringBuffer();
-            msg.append("WARNING: Unknown block-policy configuration \"")
-                .append(blockPolicy);
-            msg.append("\". Should be one of \"").append(DefaultThreadPoolManager.POLICY_ABORT);
-            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD);
-            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_DISCARD_OLDEST);
-            msg.append("\",\"").append(DefaultThreadPoolManager.POLICY_RUN);
-            msg.append("\". Will use \"").append(DefaultThreadPoolManager.DEFAULT_BLOCK_POLICY).append("\"");
-            logger.warn(msg.toString());
-            handler = new ThreadPoolExecutor.CallerRunsPolicy();
+        RejectedExecutionHandler handler = null;
+        switch (blockPolicy) {
+            case ABORT :
+                handler = new ThreadPoolExecutor.AbortPolicy();
+                break;
+            case DISCARD :
+                handler = new ThreadPoolExecutor.AbortPolicy();
+                break;
+            case DISCARDOLDEST :
+                handler = new ThreadPoolExecutor.AbortPolicy();
+                break;
+            case RUN :
+                handler = new ThreadPoolExecutor.AbortPolicy();
+                break;
         }
         this.shutdownGraceful = shutdownGraceful;
         this.shutdownWaitTimeMs = shutdownWaitTimeMs;
diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
index c2c6183..fcaa35e 100644
--- a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
+++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPoolManager.java
@@ -18,6 +18,7 @@ package org.apache.sling.threads.impl;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ThreadFactory;
 
 import org.apache.sling.threads.ThreadPool;
 import org.apache.sling.threads.ThreadPoolManager;
@@ -60,24 +61,6 @@ public class DefaultThreadPoolManager implements ThreadPoolManager {
     /** The default shutdown waittime time */
     protected final static int DEFAULT_SHUTDOWN_WAIT_TIME = -1;
 
-    /** The default shutdown waittime time */
-    protected final static String DEFAULT_THREADPOOL_NAME = "default";
-
-    /** ThreadPool block policy ABORT */
-    protected final static String POLICY_ABORT = "ABORT";
-
-    /** ThreadPool block policy DISCARD */
-    protected final static String POLICY_DISCARD = "DISCARD";
-
-    /** ThreadPool block policy DISCARD-OLDEST */
-    protected final static String POLICY_DISCARD_OLDEST = "DISCARDOLDEST";
-
-    /** ThreadPool block policy RUN */
-    protected final static String POLICY_RUN = "RUN";
-
-    /** The default shutdown waittime time */
-    protected final static String DEFAULT_BLOCK_POLICY = POLICY_RUN;
-
     /** By default we use the logger for this class. */
     protected Logger logger = LoggerFactory.getLogger(getClass());
 
@@ -101,7 +84,9 @@ public class DefaultThreadPoolManager implements ThreadPoolManager {
                     null,
                     DEFAULT_THREAD_PRIORITY,
                     DEFAULT_DAEMON_MODE);
-        this.pools.put(defaultPool.getName(), defaultPool);
+        synchronized ( this.pools ) {
+            this.pools.put(defaultPool.getName(), defaultPool);
+        }
         this.logger.info("Thread pool manager startet with default pool.");
     }
 
@@ -112,14 +97,16 @@ public class DefaultThreadPoolManager implements ThreadPoolManager {
         this.logger.info("Stopping thread pool manager.");
         this.logger.debug("Disposing all thread pools");
 
-        for (ThreadPool pool : this.pools.values()) {
-            this.logger.debug("Shutting down thread pool {}", pool.getName());
+        synchronized ( this.pools ) {
+            for (ThreadPool pool : this.pools.values()) {
+                this.logger.debug("Shutting down thread pool {}", pool.getName());
 
-            pool.shutdown();
+                pool.shutdown();
 
-            this.logger.debug("Thread pool " + pool.getName() + " is shut down.");
+                this.logger.debug("Thread pool " + pool.getName() + " is shut down.");
+            }
+            this.pools.clear();
         }
-        this.pools.clear();
         this.logger.info("Thread pool manager stopped.");
     }
 
@@ -152,4 +139,44 @@ public class DefaultThreadPoolManager implements ThreadPoolManager {
             return pool;
         }
     }
+
+    /**
+     * @see org.apache.sling.threads.ThreadPoolManager#create(java.lang.String, int, int,
int, long, org.apache.sling.threads.ThreadPoolManager.ThreadPoolPolicy, boolean, int, java.util.concurrent.ThreadFactory,
int, boolean)
+     */
+    public ThreadPool create(String name,
+                             int minPoolSize,
+                             int maxPoolSize,
+                             int queueSize,
+                             long keepAliveTime,
+                             ThreadPoolPolicy blockPolicy,
+                             boolean shutdownGraceful,
+                             int shutdownWaitTimeMs,
+                             ThreadFactory factory,
+                             int priority,
+                             boolean isDaemon) {
+        if ( name == null ) {
+            throw new IllegalArgumentException("Name must not be null.");
+        }
+        synchronized ( this.pools ) {
+            ThreadPool pool = this.pools.get(name);
+            if ( pool != null ) {
+                // pool already exists
+                return null;
+            }
+            pool = new DefaultThreadPool(name,
+                                         minPoolSize,
+                                         maxPoolSize,
+                                         queueSize,
+                                         keepAliveTime,
+                                         blockPolicy,
+                                         shutdownGraceful,
+                                         shutdownWaitTimeMs,
+                                         factory,
+                                         priority,
+                                         isDaemon);
+            this.pools.put(name, pool);
+            return pool;
+        }
+    }
+
 }

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

Mime
View raw message