You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2014/05/01 21:35:23 UTC

svn commit: r1591752 - in /commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs: auxiliary/disk/jdbc/ auxiliary/lateral/socket/tcp/ auxiliary/remote/server/ utils/discovery/ utils/threadpool/

Author: tv
Date: Thu May  1 19:35:22 2014
New Revision: 1591752

URL: http://svn.apache.org/r1591752
Log:
Centralize thread factories

Added:
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java   (with props)
Modified:
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCacheManagerAbstractTemplate.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCacheManagerAbstractTemplate.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCacheManagerAbstractTemplate.java?rev=1591752&r1=1591751&r2=1591752&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCacheManagerAbstractTemplate.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCacheManagerAbstractTemplate.java Thu May  1 19:35:22 2014
@@ -25,11 +25,11 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
 import org.apache.commons.jcs.auxiliary.disk.AbstractDiskCacheManager;
+import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -138,7 +138,8 @@ public abstract class JDBCDiskCacheManag
         {
             if ( shrinkerDaemon == null )
             {
-                shrinkerDaemon = Executors.newScheduledThreadPool(2, new MyThreadFactory());
+                shrinkerDaemon = Executors.newScheduledThreadPool(2,
+                        new DaemonThreadFactory("JCS-JDBCDiskCacheManager-", Thread.MIN_PRIORITY));
             }
 
             ShrinkerThread shrinkerThread = shrinkerThreadMap.get( cattr.getTableName() );
@@ -204,28 +205,4 @@ public abstract class JDBCDiskCacheManag
             }
         }
     }
-
-    /**
-     * Allows us to set the daemon status on the clock-daemon
-     */
-    protected static class MyThreadFactory
-        implements ThreadFactory
-    {
-        /**
-         * Set the priority to min and daemon to true.
-         * <p>
-         * @param runner
-         * @return the daemon thread.
-         */
-        @Override
-        public Thread newThread( Runnable runner )
-        {
-            Thread t = new Thread( runner );
-            String oldName = t.getName();
-            t.setName( "JCS-JDBCDiskCacheManager-" + oldName );
-            t.setDaemon( true );
-            t.setPriority( Thread.MIN_PRIORITY );
-            return t;
-        }
-    }
 }

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java?rev=1591752&r1=1591751&r2=1591752&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java Thu May  1 19:35:22 2014
@@ -32,7 +32,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
 
 import org.apache.commons.jcs.access.exception.CacheException;
 import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor;
@@ -45,6 +44,7 @@ import org.apache.commons.jcs.engine.beh
 import org.apache.commons.jcs.engine.control.CompositeCache;
 import org.apache.commons.jcs.engine.control.CompositeCacheManager;
 import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
+import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -153,7 +153,8 @@ public class LateralTCPListener<K extend
         {
             this.port = getTcpLateralCacheAttributes().getTcpListenerPort();
 
-            pooledExecutor = Executors.newCachedThreadPool(new MyThreadFactory());
+            pooledExecutor = Executors.newCachedThreadPool(
+                    new DaemonThreadFactory("JCS-LateralTCPListener-"));
             terminated = false;
             shutdown = false;
 
@@ -747,28 +748,6 @@ public class LateralTCPListener<K extend
     }
 
     /**
-     * Allows us to set the daemon status on the executor threads
-     * <p>
-     * @author Aaron Smuts
-     */
-    protected static class MyThreadFactory
-        implements ThreadFactory
-    {
-        /**
-         * @param runner
-         * @return daemon thread
-         */
-        @Override
-        public Thread newThread( Runnable runner )
-        {
-            Thread t = new Thread( runner );
-            t.setDaemon( true );
-            t.setPriority( Thread.MIN_PRIORITY );
-            return t;
-        }
-    }
-
-    /**
      * Shuts down the receiver.
      */
     @Override

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java?rev=1591752&r1=1591751&r2=1591752&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServerFactory.java Thu May  1 19:35:22 2014
@@ -31,7 +31,6 @@ import java.rmi.server.UnicastRemoteObje
 import java.util.Properties;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheConfigurator;
@@ -41,6 +40,7 @@ import org.apache.commons.jcs.engine.beh
 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
 import org.apache.commons.jcs.utils.config.OptionConverter;
 import org.apache.commons.jcs.utils.config.PropertySetter;
+import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -196,7 +196,8 @@ public class RemoteCacheServerFactory
             {
                 if ( keepAliveDaemon == null )
                 {
-                    keepAliveDaemon = Executors.newScheduledThreadPool(1, new MyThreadFactory());
+                    keepAliveDaemon = Executors.newScheduledThreadPool(1,
+                            new DaemonThreadFactory("JCS-RemoteCacheServerFactory-"));
                 }
                 RegistryKeepAliveRunner runner = new RegistryKeepAliveRunner( host, port, serviceName );
                 runner.setCacheEventLogger( cacheEventLogger );
@@ -533,29 +534,4 @@ public class RemoteCacheServerFactory
     {
         return serviceName;
     }
-
-    /**
-     * Allows us to set the daemon status on the clockdaemon
-     */
-    protected static class MyThreadFactory
-        implements ThreadFactory
-    {
-        /**
-         * @param runner
-         * @return a new thread for the given Runnable
-         */
-        @Override
-        public Thread newThread( Runnable runner )
-        {
-            Thread t = new Thread( runner );
-            String oldName = t.getName();
-            t.setName( "JCS-RemoteCacheServerFactory-" + oldName );
-            t.setDaemon( true );
-            t.setPriority( Thread.MIN_PRIORITY );
-            if (log.isDebugEnabled()){
-                log.debug("Created thread: " + t);
-            }
-            return t;
-        }
-    }
 }

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java?rev=1591752&r1=1591751&r2=1591752&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java Thu May  1 19:35:22 2014
@@ -26,13 +26,13 @@ import java.net.DatagramPacket;
 import java.net.InetAddress;
 import java.net.MulticastSocket;
 import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
 
 import org.apache.commons.jcs.engine.CacheInfo;
 import org.apache.commons.jcs.engine.behavior.IShutdownObserver;
 import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
 import org.apache.commons.jcs.utils.discovery.UDPDiscoveryMessage.BroadcastType;
+import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -91,7 +91,8 @@ public class UDPDiscoveryReceiver
         this.multicastPort = multicastPort;
 
         // create a small thread pool to handle a barrage
-        pooledExecutor = (ThreadPoolExecutor)Executors.newFixedThreadPool(maxPoolSize, new MyThreadFactory());
+        pooledExecutor = (ThreadPoolExecutor)Executors.newFixedThreadPool(maxPoolSize,
+                new DaemonThreadFactory("JCS-UDPDiscoveryReceiver-", Thread.MIN_PRIORITY));
         pooledExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
         //pooledExecutor.setMinimumPoolSize(1);
 
@@ -360,30 +361,6 @@ public class UDPDiscoveryReceiver
         }
     }
 
-    /**
-     * Allows us to set the daemon status on the executor threads
-     */
-    protected static class MyThreadFactory
-        implements ThreadFactory
-    {
-        /**
-         * Sets the thread to daemon.
-         * <p>
-         * @param runner
-         * @return a daemon thread
-         */
-        @Override
-        public Thread newThread( Runnable runner )
-        {
-            Thread t = new Thread( runner );
-            String oldName = t.getName();
-            t.setName( "JCS-UDPDiscoveryReceiver-" + oldName );
-            t.setDaemon( true );
-            t.setPriority( Thread.MIN_PRIORITY );
-            return t;
-        }
-    }
-
     /** Shuts down the socket. */
     @Override
     public void shutdown()

Added: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java?rev=1591752&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java Thu May  1 19:35:22 2014
@@ -0,0 +1,74 @@
+package org.apache.commons.jcs.utils.threadpool;
+
+/*
+ * 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.
+ */
+
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * Allows us to set the daemon status on the threads.
+ * <p>
+ * @author aaronsm
+ */
+public class DaemonThreadFactory
+    implements ThreadFactory
+{
+    private String prefix;
+    private boolean threadIsDaemon = true;
+    private int threadPriority = Thread.NORM_PRIORITY;
+
+    /**
+     * Constructor
+     *
+     * @param prefix thread name prefix
+     */
+    public DaemonThreadFactory(String prefix)
+    {
+        this(prefix, Thread.NORM_PRIORITY);
+    }
+
+    /**
+     * Constructor
+     *
+     * @param prefix thread name prefix
+     * @param threadPriority set thread priority
+     */
+    public DaemonThreadFactory(String prefix, int threadPriority)
+    {
+        this.prefix = prefix;
+        this.threadPriority = threadPriority;
+    }
+
+    /**
+     * Sets the thread to daemon.
+     * <p>
+     * @param runner
+     * @return a daemon thread
+     */
+    @Override
+    public Thread newThread( Runnable runner )
+    {
+        Thread t = new Thread( runner );
+        String oldName = t.getName();
+        t.setName( prefix + oldName );
+        t.setDaemon(threadIsDaemon);
+        t.setPriority(threadPriority);
+        return t;
+    }
+}
\ No newline at end of file

Propchange: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/DaemonThreadFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java?rev=1591752&r1=1591751&r2=1591752&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java Thu May  1 19:35:22 2014
@@ -24,7 +24,6 @@ import java.util.HashMap;
 import java.util.Properties;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
@@ -162,7 +161,7 @@ public class ThreadPoolManager
 
         pool = new ThreadPoolExecutor(config.getStartUpSize(), config.getMaximumPoolSize(),
                 config.getKeepAliveTime(), TimeUnit.MILLISECONDS,
-                queue, new MyThreadFactory());
+                queue, new DaemonThreadFactory("JCS-ThreadPoolManager-"));
 
         // when blocked policy
         switch (config.getWhenBlockedPolicy())
@@ -443,29 +442,4 @@ public class ThreadPoolManager
 
         return config;
     }
-
-    /**
-     * Allows us to set the daemon status on the threads.
-     * <p>
-     * @author aaronsm
-     */
-    public static class MyThreadFactory
-        implements ThreadFactory
-    {
-        /**
-         * Sets the thread to daemon.
-         * <p>
-         * @param runner
-         * @return a daemon thread
-         */
-        @Override
-        public Thread newThread( Runnable runner )
-        {
-            Thread t = new Thread( runner );
-            String oldName = t.getName();
-            t.setName( "JCS-ThreadPoolManager-" + oldName );
-            t.setDaemon( true );
-            return t;
-        }
-    }
 }