directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r220235 - in /directory/network: branches/0.7/src/java/org/apache/mina/io/filter/ branches/0.7/src/java/org/apache/mina/protocol/filter/ branches/0.7/src/java/org/apache/mina/util/ trunk/src/java/org/apache/mina/filter/
Date Fri, 22 Jul 2005 04:27:50 GMT
Author: trustin
Date: Thu Jul 21 21:27:48 2005
New Revision: 220235

URL: http://svn.apache.org/viewcvs?rev=220235&view=rev
Log:
Fix for DIRMINA-75 and DIRMINA-77
* Added threadIdReuseQueue to reuse threadIds
* Added acquireThreadId and releaseThreadId methods to manage threadIds.
* Added threadNamePrefix property and its getter.


Modified:
    directory/network/branches/0.7/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
    directory/network/branches/0.7/src/java/org/apache/mina/protocol/filter/ProtocolThreadPoolFilter.java
    directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java
    directory/network/trunk/src/java/org/apache/mina/filter/ThreadPoolFilter.java

Modified: directory/network/branches/0.7/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java?rev=220235&r1=220234&r2=220235&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
(original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
Thu Jul 21 21:27:48 2005
@@ -41,11 +41,23 @@
 public class IoThreadPoolFilter extends BaseThreadPool implements ThreadPool, IoFilter
 {
     /**
-     * Creates a new instanceof this filter with default thread pool settings.
+     * Creates a new instance of this filter with default thread pool settings.
      * You'll have to invoke {@link #start()} method to start threads actually.
      */
     public IoThreadPoolFilter()
     {
+        this( "IoThreadPool" );
+    }
+    
+    /**
+     * Creates a new instance of this filter with default thread pool settings.
+     * You'll have to invoke {@link #start()} method to start threads actually.
+     * 
+     * @param threadNamePrefix the prefix of the thread names this pool will create.
+     */
+    public IoThreadPoolFilter( String threadNamePrefix )
+    {
+        super( threadNamePrefix );
     }
 
     public void sessionOpened( NextFilter nextFilter, IoSession session )

Modified: directory/network/branches/0.7/src/java/org/apache/mina/protocol/filter/ProtocolThreadPoolFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/protocol/filter/ProtocolThreadPoolFilter.java?rev=220235&r1=220234&r2=220235&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/protocol/filter/ProtocolThreadPoolFilter.java
(original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/protocol/filter/ProtocolThreadPoolFilter.java
Thu Jul 21 21:27:48 2005
@@ -46,6 +46,18 @@
      */
     public ProtocolThreadPoolFilter()
     {
+        this( "ProtocolThreadPool" );
+    }
+
+    /**
+     * Creates a new instance of this filter with default thread pool settings.
+     * You'll have to invoke {@link #start()} method to start threads actually.
+     * 
+     * @param threadNamePrefix the prefix of the thread names this pool will create.
+     */
+    public ProtocolThreadPoolFilter( String threadNamePrefix )
+    {
+        super( threadNamePrefix );
     }
 
     public void sessionOpened( NextFilter nextFilter,

Modified: directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java?rev=220235&r1=220234&r2=220235&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/util/BaseThreadPool.java Thu Jul
21 21:27:48 2005
@@ -47,36 +47,78 @@
      */
     public static final int DEFAULT_KEEP_ALIVE_TIME = 60 * 1000;
 
-    private static volatile int threadId = 0;
+    /**
+     * A queue which contains {@link Integer}s which represents reusable
+     * thread IDs.  {@link Worker} first checks this queue and then
+     * uses {@link #threadId} when no reusable thread ID is available.
+     */
+    private static final Queue threadIdReuseQueue = new Queue();
+    private static int threadId = 0;
+    
+    private static int acquireThreadId()
+    {
+        synchronized( threadIdReuseQueue )
+        {
+            Integer id = ( Integer ) threadIdReuseQueue.pop();
+            if( id == null )
+            {
+                return ++ threadId;
+            }
+            else
+            {
+                return id.intValue();
+            }
+        }
+    }
+    
+    private static void releaseThreadId( int id )
+    {
+        synchronized( threadIdReuseQueue )
+        {
+            threadIdReuseQueue.push( new Integer( id ) );
+        }
+    }
 
+    private final String threadNamePrefix;
     private final Map buffers = new IdentityHashMap();
-
     private final Stack followers = new Stack();
-
     private final BlockingSet readySessionBuffers = new BlockingSet();
-
     private final Set busySessionBuffers = new HashSet();
 
     private Worker leader;
 
     private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
-
     private int keepAliveTime = DEFAULT_KEEP_ALIVE_TIME;
 
     private boolean started;
-
     private boolean shuttingDown;
 
     private int poolSize;
-
     private final Object poolSizeLock = new Object();
 
     /**
      * Creates a new instance with default thread pool settings.
      * You'll have to invoke {@link #start()} method to start threads actually.
+     *
+     * @param threadNamePrefix the prefix of the thread names this pool will create.
      */
-    protected BaseThreadPool()
+    public BaseThreadPool( String threadNamePrefix )
+    {
+        if( threadNamePrefix == null )
+        {
+            throw new NullPointerException( "threadNamePrefix" );
+        }
+        threadNamePrefix = threadNamePrefix.trim();
+        if( threadNamePrefix.length() == 0 )
+        {
+            throw new IllegalArgumentException( "threadNamePrefix is empty." );
+        }
+        this.threadNamePrefix = threadNamePrefix;
+    }
+    
+    public String getThreadNamePrefix()
     {
+        return threadNamePrefix;
     }
 
     public int getPoolSize()
@@ -244,11 +286,14 @@
 
     private class Worker extends Thread
     {
+        private final int id;
         private final Object promotionLock = new Object();
 
         private Worker()
         {
-            super( "IoThreadPool-" + ( threadId++ ) );
+            int id = acquireThreadId();
+            this.id = id;
+            this.setName( threadNamePrefix + '-' + id );
             increasePoolSize();
         }
 
@@ -283,6 +328,7 @@
             }
 
             decreasePoolSize();
+            releaseThreadId( id );
         }
 
         private SessionBuffer fetchBuffer()

Modified: directory/network/trunk/src/java/org/apache/mina/filter/ThreadPoolFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/ThreadPoolFilter.java?rev=220235&r1=220234&r2=220235&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/ThreadPoolFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/ThreadPoolFilter.java Thu Jul
21 21:27:48 2005
@@ -57,8 +57,39 @@
      */
     public static final int DEFAULT_KEEP_ALIVE_TIME = 60 * 1000;
 
-    private static volatile int threadId = 0;
+    /**
+     * A queue which contains {@link Integer}s which represents reusable
+     * thread IDs.  {@link Worker} first checks this queue and then
+     * uses {@link #threadId} when no reusable thread ID is available.
+     */
+    private static final Queue threadIdReuseQueue = new Queue();
+    private static int threadId = 0;
+    
+    private static int acquireThreadId()
+    {
+        synchronized( threadIdReuseQueue )
+        {
+            Integer id = ( Integer ) threadIdReuseQueue.pop();
+            if( id == null )
+            {
+                return ++ threadId;
+            }
+            else
+            {
+                return id.intValue();
+            }
+        }
+    }
+    
+    private static void releaseThreadId( int id )
+    {
+        synchronized( threadIdReuseQueue )
+        {
+            threadIdReuseQueue.push( new Integer( id ) );
+        }
+    }
 
+    private final String threadNamePrefix;
     private final Map parents = new IdentityHashMap(); 
     private final Map buffers = new IdentityHashMap();
     private final Stack followers = new Stack();
@@ -76,10 +107,35 @@
     private final Object poolSizeLock = new Object();
 
     /**
-     * Creates a new instanceof this filter with default thread pool settings.
+     * Creates a new instance of this filter with default thread pool settings.
      */
     public ThreadPoolFilter()
     {
+        this( "IoThreadPool" );
+    }
+    
+    /**
+     * Creates a new instance of this filter with the specified thread name prefix
+     * and other default settings.
+     * @param threadNamePrefix the prefix of the thread names this pool will create.
+     */
+    public ThreadPoolFilter( String threadNamePrefix )
+    {
+        if( threadNamePrefix == null )
+        {
+            throw new NullPointerException( "threadNamePrefix" );
+        }
+        threadNamePrefix = threadNamePrefix.trim();
+        if( threadNamePrefix.length() == 0 )
+        {
+            throw new IllegalArgumentException( "threadNamePrefix is empty." );
+        }
+        this.threadNamePrefix = threadNamePrefix;
+    }
+    
+    public String getThreadNamePrefix()
+    {
+        return threadNamePrefix;
     }
 
     public int getPoolSize()
@@ -196,11 +252,14 @@
 
     private class Worker extends Thread
     {
+        private final int id;
         private final Object promotionLock = new Object();
 
         private Worker()
         {
-            super( "IoThreadPool-" + ( threadId++ ) );
+            int id = acquireThreadId();
+            this.id = id;
+            this.setName( threadNamePrefix + '-' + id );
             increasePoolSize();
         }
 
@@ -235,6 +294,7 @@
             }
 
             decreasePoolSize();
+            releaseThreadId( id );
         }
 
         private SessionBuffer fetchBuffer()



Mime
View raw message