You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/02/18 15:55:29 UTC

svn commit: r628769 - in /incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl: DefaultThreadFactory.java DefaultThreadPool.java ExtendedThreadFactory.java

Author: cziegeler
Date: Mon Feb 18 06:55:27 2008
New Revision: 628769

URL: http://svn.apache.org/viewvc?rev=628769&view=rev
Log:
Use jvm default thread factory and always provide priority and daemon settings.

Added:
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java
      - copied, changed from r628678, incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
Removed:
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java
Modified:
    incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java

Modified: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java?rev=628769&r1=628768&r2=628769&view=diff
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java (original)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadPool.java Mon Feb 18 06:55:27 2008
@@ -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 @@
         }
 
         // 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 @@
         // 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) {

Copied: incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java (from r628678, incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java?p2=incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java&p1=incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java&r1=628678&r2=628769&rev=628769&view=diff
==============================================================================
--- incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/DefaultThreadFactory.java (original)
+++ incubator/sling/trunk/sling/threads/src/main/java/org/apache/sling/threads/impl/ExtendedThreadFactory.java Mon Feb 18 06:55:27 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.threads.impl;
 
+import java.util.concurrent.ThreadFactory;
+
 
 /**
  * This class is responsible to create new Thread instances.
@@ -23,54 +25,44 @@
  *
  * @version $Id$
  */
-public class DefaultThreadFactory
-    implements ExtendedThreadFactory {
+public final class ExtendedThreadFactory implements ThreadFactory {
 
     /** The daemon mode */
-    private boolean isDaemon = DefaultThreadPoolManager.DEFAULT_DAEMON_MODE;
+    private final boolean isDaemon;
 
     /** The priority of newly created Threads */
-    private int priority = DefaultThreadPoolManager.DEFAULT_THREAD_PRIORITY;
+    private final int 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;
-    }
+    /** The real factory. */
+    private final ThreadFactory factory;
 
     /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#setPriority(int)
+     * 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.
      */
-    public void setPriority( final 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);
-        }
-    }
-
-    /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#getPriority()
-     */
-    public int getPriority() {
-        return this.priority;
+                ( Thread.MIN_PRIORITY == priority ) ||
+                ( Thread.NORM_PRIORITY == priority ) ) {
+                this.priority = priority;
+            } else {
+                throw new IllegalStateException("Unknown priority " + priority);
+            }
+        this.factory = factory;
     }
 
     /**
-     * @see org.apache.sling.threads.impl.ExtendedThreadFactory#newThread(java.lang.Runnable)
+     * Invoke the thread factory and set the daemon flag and priority.
+     * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
      */
     public Thread newThread( final Runnable command ) {
-        final Thread thread = new Thread( command );
+        final Thread thread = this.factory.newThread(command);
         thread.setPriority( this.priority );
         thread.setDaemon( this.isDaemon );