Coverage Report - org.apache.commons.threadpool.DefaultThreadPool
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultThreadPool
0%
0/46
0%
0/10
1.538
 
 1  
 /*
 2  
  * Copyright 1999-2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.threadpool;
 17  
 
 18  
 /**
 19  
  * A default implementation of a ThreadPool
 20  
  * which is constructed with a given number of threads.
 21  
  *
 22  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 23  
  * @version $Revision: 155472 $
 24  
  */
 25  
 public class DefaultThreadPool implements Runnable, ThreadPool {
 26  
 
 27  0
     private MTQueue queue = new MTQueue();
 28  0
     private boolean stopped = false;
 29  
     private final ThreadPoolMonitor monitor;
 30  
     private ThreadGroup threadGroup;
 31  
 
 32  
     public DefaultThreadPool(ThreadPoolMonitor monitor,
 33  0
                  int numberOfThreads, int threadPriority) {
 34  0
         this.monitor = monitor;
 35  0
         for ( int i = 0; i < numberOfThreads; i++ ) {
 36  0
             startThread(threadPriority);
 37  
         }
 38  0
     }
 39  
 
 40  
     public DefaultThreadPool(ThreadPoolMonitor monitor,
 41  0
                  int numberOfThreads) {
 42  0
         this.monitor = monitor;
 43  0
         for ( int i = 0; i < numberOfThreads; i++ ) {
 44  0
             startThread();
 45  
         }
 46  0
     }
 47  
 
 48  0
     public DefaultThreadPool() {
 49  0
         this.monitor = new CommonsLoggingThreadPoolMonitor();
 50  
         // typically a thread pool should have at least 1 thread
 51  0
         startThread();
 52  0
     }
 53  
 
 54  
     public DefaultThreadPool(int numberOfThreads) {
 55  0
         this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads);
 56  0
     }
 57  
 
 58  
     public DefaultThreadPool(int numberOfThreads, int threadPriority) {
 59  0
         this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads, threadPriority);
 60  0
     }
 61  
 
 62  
     public void setThreadGroup(ThreadGroup threadGroup) {
 63  0
         this.threadGroup = threadGroup;
 64  0
     }
 65  
     
 66  
     /** Start a new thread running */
 67  
     public Thread startThread() {
 68  0
         Thread thread = createThread();
 69  0
         thread.start();
 70  0
         return thread;
 71  
     }
 72  
 
 73  
     public Thread startThread(int priority) {
 74  0
         Thread thread = createThread();
 75  0
         thread.setPriority(priority);
 76  0
         thread.start();
 77  0
         return thread;
 78  
     }
 79  
 
 80  
     private Thread createThread() {
 81  0
         if(this.threadGroup != null) {
 82  0
             return new Thread(this.threadGroup, this);
 83  
         } else {
 84  0
             return new Thread(this);
 85  
         }
 86  
     }
 87  
 
 88  
     public void stop() {
 89  0
         stopped = true;
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Returns number of runnable object in the queue.
 94  
      */
 95  
     public int getRunnableCount() {
 96  0
        return queue.size();
 97  
     }
 98  
 
 99  
 
 100  
     // ThreadPool interface
 101  
     //-------------------------------------------------------------------------
 102  
     
 103  
     /** 
 104  
      * Dispatch a new task onto this pool 
 105  
      * to be invoked asynchronously later
 106  
      */
 107  
     public void invokeLater(Runnable task) {
 108  0
         queue.add( task );
 109  0
     }
 110  
 
 111  
     // Runnable interface
 112  
     //-------------------------------------------------------------------------
 113  
     
 114  
     /** The method ran by the pool of background threads
 115  
      */
 116  
     public void run() {
 117  0
         while ( ! stopped ) {
 118  0
             Runnable task = (Runnable) queue.remove();
 119  0
             if ( task != null ) {
 120  
                 try {
 121  0
                     task.run();
 122  
                 }
 123  0
                 catch (Throwable t) {
 124  0
                     monitor.handleThrowable(this.getClass(), task, t);
 125  0
                 }
 126  
             }
 127  0
         }
 128  0
     }
 129  
 }