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:24:29 UTC

[sling-org-apache-sling-commons-threads] 06/33: Use jvm default thread factory and always provide priority and daemon settings.

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 f8c7fb77fc8d5ee384cdfda92254ee0d3597b775
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Feb 18 14:55:27 2008 +0000

    Use jvm default thread factory and always provide priority and daemon settings.
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/sling/threads@628769 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/threads/impl/DefaultThreadFactory.java   | 79 ----------------------
 .../sling/threads/impl/DefaultThreadPool.java      | 27 +++-----
 .../sling/threads/impl/ExtendedThreadFactory.java  | 64 ++++++++++--------
 3 files changed, 42 insertions(+), 128 deletions(-)

diff --git a/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java b/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
deleted file mode 100644
index 2cffa80..0000000
--- a/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.sling.threads.impl;
-
-
-/**
- * This class is responsible to create new Thread instances.
- * It's a very basic implementation.
- *
- * @version $Id$
- */
-public class DefaultThreadFactory
-    implements ExtendedThreadFactory {
-
-    /** The daemon mode */
-    private boolean isDaemon = DefaultThreadPoolManager.DEFAULT_DAEMON_MODE;
-
-    /** The priority of newly created Threads */
-    private int priority = DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY;
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setDaemon(boolean)
-     */
-    public void setDaemon( boolean isDaemon ) {
-        this.isDaemon = isDaemon;
-    }
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#isDaemon()
-     */
-    public boolean isDaemon() {
-        return this.isDaemon;
-    }
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setPriority(int)
-     */
-    public void setPriority( final int priority ) {
-        if( ( Thread.MAX_PRIORITY == priority ) ||
-            ( Thread.MIN_PRIORITY == priority ) ||
-            ( Thread.NORM_PRIORITY == priority ) ) {
-            this.priority = priority;
-        } else {
-            throw new IllegalStateException("Unknown priority " + priority);
-        }
-    }
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#getPriority()
-     */
-    public int getPriority() {
-        return this.priority;
-    }
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#newThread(java.lang.Runnable)
-     */
-    public Thread newThread( final Runnable command ) {
-        final Thread thread = new Thread( command );
-        thread.setPriority( this.priority );
-        thread.setDaemon( this.isDaemon );
-
-        return thread;
-    }
-}
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 f685c15..37289bd 100644
--- a/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
+++ b/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
@@ -17,6 +17,7 @@
 package org.apache.sling.threads.impl;
 
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.RejectedExecutionHandler;
 import java.util.concurrent.SynchronousQueue;
@@ -80,15 +81,14 @@ public class DefaultThreadPool
         }
 
         // factory
-        final ThreadFactory threadFactory;
+        final ThreadFactory delegateThreadFactory;
         if (factory == null) {
-            logger.warn("No ThreadFactory is configured. Will use a "
-                + DefaultThreadFactory.class.getName());
-            threadFactory = new DefaultThreadFactory();
+            logger.warn("No ThreadFactory is configured. Will use JVM default thread factory."
+                + ExtendedThreadFactory.class.getName());
+            delegateThreadFactory = Executors.defaultThreadFactory();
         } else {
-            threadFactory = factory;
+            delegateThreadFactory = factory;
         }
-
         // Min pool size
         // make sure we have enough threads for the default thread pool as we
         // need one for ourself
@@ -102,19 +102,8 @@ public class DefaultThreadPool
         // Max pool size
         maxPoolSize = (maxPoolSize < 0) ? Integer.MAX_VALUE : maxPoolSize;
 
-        // Set priority and daemon if the factory is an extended factory
-        if ( threadFactory instanceof ExtendedThreadFactory ) {
-            final ExtendedThreadFactory extTF = (ExtendedThreadFactory)threadFactory;
-            extTF.setPriority(priority);
-            extTF.setDaemon(isDaemon);
-        } else {
-            if ( priority != Thread.NORM_PRIORITY ) {
-                this.logger.warn("ThreadFactory " + threadFactory + " does not support setting the priority or daemon setting.");
-            }
-            if ( isDaemon != DefaultThreadPoolManager.DEFAULT_DAEMON_MODE ) {
-                this.logger.warn("ThreadFactory " + threadFactory + " does not support setting the daemon mode.");
-            }
-        }
+        // Set priority and daemon flag
+        final ExtendedThreadFactory threadFactory = new ExtendedThreadFactory(delegateThreadFactory, priority, isDaemon);
 
         // Keep alive time
         if (keepAliveTime < 0) {
diff --git a/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java b/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
index 5ad586f..7a030a7 100644
--- a/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
+++ b/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
@@ -18,50 +18,54 @@ package org.apache.sling.threads.impl;
 
 import java.util.concurrent.ThreadFactory;
 
+
 /**
- * This is an extension to the {@link ThreadFactory}.
+ * This class is responsible to create new Thread instances.
+ * It's a very basic implementation.
  *
  * @version $Id$
  */
-public interface ExtendedThreadFactory
-    extends ThreadFactory {
+public final class ExtendedThreadFactory implements ThreadFactory {
 
-    /**
-     * Set the daemon mode of created <code>Thread</code>s should have
-     *
-     * @param isDaemon Whether new {@link Thread}s should run as daemons.
-     */
-    void setDaemon( boolean isDaemon );
+    /** The daemon mode */
+    private final boolean isDaemon;
 
-    /**
-     * Get the daemon mode created <code>Thread</code>s will have
-     *
-     * @return Whether new {@link Thread}s should run as daemons.
-     */
-    boolean isDaemon();
+    /** The priority of newly created Threads */
+    private final int priority;
+
+    /** The real factory. */
+    private final ThreadFactory factory;
 
     /**
-     * Set the priority newly created <code>Thread</code>s should have
+     * Create a new wrapper for a thread factory handling the
      *
      * @param priority One of {@link Thread#MIN_PRIORITY}, {@link
      *        Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
+     * @param isDaemon Whether new {@link Thread}s should run as daemons.
      */
-    void setPriority( int priority );
+    public ExtendedThreadFactory(final ThreadFactory factory,
+                                final int priority,
+                                final boolean isDaemon) {
+        this.isDaemon = isDaemon;
+        if( ( Thread.MAX_PRIORITY == priority ) ||
+                ( Thread.MIN_PRIORITY == priority ) ||
+                ( Thread.NORM_PRIORITY == priority ) ) {
+                this.priority = priority;
+            } else {
+                throw new IllegalStateException("Unknown priority " + priority);
+            }
+        this.factory = factory;
+    }
 
     /**
-     * Get the priority newly created <code>Thread</code>s will have
-     *
-     * @return One of {@link Thread#MIN_PRIORITY}, {@link
-     *         Thread#NORM_PRIORITY}, {@link Thread#MAX_PRIORITY}
+     * Invoke the thread factory and set the daemon flag and priority.
+     * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
      */
-    int getPriority();
+    public Thread newThread( final Runnable command ) {
+        final Thread thread = this.factory.newThread(command);
+        thread.setPriority( this.priority );
+        thread.setDaemon( this.isDaemon );
 
-    /**
-     * Create a new Thread for a {@link Runnable} command
-     *
-     * @param command The <code>Runnable</code>
-     *
-     * @return new <code>Thread</code>
-     */
-    Thread newThread( Runnable command );
+        return thread;
+    }
 }

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