James,
 
    I have made some changes to allow the additon of ThreadGroups and better naming of threads to DefaultThreadPool.  These changes make it much easier to identify threads that are in the thread pool while using a profiler or examining a thread dump.
 
Please find the attached path file (patch.txt) and apply if you feel it is warranted.
 
Thanks!
 
James Birchfield
Index: DefaultThreadPool.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/DefaultThreadPool.java,v
retrieving revision 1.1
diff -u -r1.1 DefaultThreadPool.java
--- DefaultThreadPool.java      10 Oct 2002 20:05:20 -0000      1.1
+++ DefaultThreadPool.java      27 Mar 2003 03:30:56 -0000
@@ -76,35 +76,107 @@
     /** The Log to which logging calls will be made. */
     private Log log = LogFactory.getLog(DefaultThreadPool.class);
 
+    public static final String DEFAULT_THREAD_GROUP_NAME = "DefaultThreadPool";
+
     private MTQueue queue = new MTQueue();
     private boolean stopped = false;
 
+    /**
+     * The thread group in which all threads will be assigned
+     */
+    private ThreadGroup threadGroup;
+
+    /**
+     * The name for this thread group
+     */
+    private String threadGroupName;
+
+    /**
+     * The number of threads in the pool.
+     */
+    private int threadCount;
+
+       /**
+        * Default constructor.  Will create one thread with normal priority
+        * using the default thread group name
+        *
+        */
     public DefaultThreadPool() {
-        // typically a thread pool should have at least 1 thread
-        startThread();
+        this(DEFAULT_THREAD_GROUP_NAME, 1, Thread.NORM_PRIORITY);
     }
 
+       /**
+        * Will create one thread with normal priority and the given
+        * thread group name
+        * @param threadGroupName the name of the thread group
+        */
+    public DefaultThreadPool(String threadGroupName) {
+        this(threadGroupName, 1, Thread.NORM_PRIORITY);
+    }
+
+       /**
+        * Will create the given number of threads wiith normal priority
+        * using the default thread group name
+        * @param numberOfThreads the number of threads to create
+        */
     public DefaultThreadPool(int numberOfThreads) {
-        for ( int i = 0; i < numberOfThreads; i++ ) {
-            startThread();
-        }
+        this(DEFAULT_THREAD_GROUP_NAME, numberOfThreads, Thread.NORM_PRIORITY);
     }
 
-    public DefaultThreadPool(int numberOfThreads, int threadPriority) {
-        for ( int i = 0; i < numberOfThreads; i++ ) {
+       /**
+        * Will create the given number of threads with the given priority
+        * using the default thread group name
+        * @param numberOfThreads the number of threads to start
+        * @param priority the priority for each thread
+        */
+    public DefaultThreadPool(int numberOfThreads, int priority) {
+        this(DEFAULT_THREAD_GROUP_NAME, numberOfThreads, priority);
+    }
+
+       /**
+        * Will create the givennumber of threads with normal priority
+        * using the given thread group name
+        * @param threadGroupName the thread group name
+        * @param numberOfThreads the number of threads to start
+        */
+    public DefaultThreadPool(String threadGroupName, int numberOfThreads) {
+        this(threadGroupName, numberOfThreads, Thread.NORM_PRIORITY);
+    }
+
+       /**
+        * Will create the given number of threads with the given priority
+        * using the given thread group name
+        * @param threadGroupName the thread group name
+        * @param numberOfThreads the number of threads to start
+        * @param threadPriority the priority for each thread
+        */
+    public DefaultThreadPool(
+        String threadGroupName,
+        int numberOfThreads,
+        int threadPriority) {
+        this.threadGroupName = threadGroupName;
+        threadGroup = new ThreadGroup(threadGroupName);
+        for (int i = 0; i < numberOfThreads; i++) {
             startThread(threadPriority);
         }
     }
-    
-    /** Start a new thread running */
+
+    /**
+     * Will create a thread with normal priority
+     * @return the newly created thread
+     */
     public Thread startThread() {
-        Thread thread = new Thread( this );
-        thread.start();
-        return thread;
+        return startThread(Thread.NORM_PRIORITY);
     }
 
+       /**
+        * Will create a thread with the given priority
+        * @param priority the priority for the thread
+        * @return the newly created thread
+        */
     public Thread startThread(int priority) {
-        Thread thread = new Thread( this );
+        String threadName = threadGroupName + "-" + ++threadCount;
+        Thread thread = new Thread(threadGroup, this, threadName);
         thread.setPriority(priority);
         thread.start();
         return thread;
@@ -118,34 +190,32 @@
      * Returns number of runnable object in the queue.
      */
     public int getRunnableCount() {
-       return queue.size();
+        return queue.size();
     }
 
-
     // ThreadPool interface
     //-------------------------------------------------------------------------
-    
+
     /** 
      * Dispatch a new task onto this pool 
      * to be invoked asynchronously later
      */
     public void invokeLater(Runnable task) {
-        queue.add( task );
+        queue.add(task);
     }
 
     // Runnable interface
     //-------------------------------------------------------------------------
-    
+
     /** The method ran by the pool of background threads
      */
     public void run() {
-        while ( ! stopped ) {
+        while (!stopped) {
             Runnable task = (Runnable) queue.remove();
-            if ( task != null ) {
+            if (task != null) {
                 try {
                     task.run();
-                }
-                catch (Throwable t) {
+                } catch (Throwable t) {
                     handleException(t);
                 }
             }
@@ -155,6 +225,6 @@
     // Implementation methods
     //-------------------------------------------------------------------------
     protected void handleException(Throwable t) {
-        log.error( "Caught: " + t, t );
+        log.error("Caught: " + t, t);
     }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to