Source code: org/mule/util/NamedThreadPool.java
1 /*
2 * $Header: /cvsroot/mule/mule/src/java/org/mule/util/NamedThreadPool.java,v 1.3 2003/10/20 21:44:38 rossmason Exp $
3 * $Revision: 1.3 $
4 * $Date: 2003/10/20 21:44:38 $
5 * ------------------------------------------------------------------------------------------------------
6 *
7 * Copyright (c) Cubis Limited. All rights reserved.
8 * http://www.cubis.co.uk
9 *
10 * The software in this package is published under the terms of the BSD
11 * style license a copy of which has been included with this distribution in
12 * the LICENSE.txt file.
13 *
14 */
15 package org.mule.util;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.commons.threadpool.MTQueue;
20 import org.apache.commons.threadpool.ThreadPool;
21
22 /**
23 * <p><code>ThreadPool</code> A pool of threads for dispatching work
24 * @author <a href="mailto:ross.mason@cubis.co.uk">Ross Mason</a>
25 * @version $Revision: 1.3 $
26 */
27
28 public class NamedThreadPool implements Runnable, ThreadPool
29 {
30
31 /** The logger for this class */
32 private static final transient Log log = LogFactory.getLog(NamedThreadPool.class);
33
34 private MTQueue queue = new MTQueue();
35 private boolean stopped = false;
36 private ExceptionListener exceptionListener = null;
37 private int threadCount = 0;
38
39 public NamedThreadPool()
40 {
41 startThread();
42 }
43
44 public NamedThreadPool(int numberOfThreads)
45 {
46 for (int i = 0; i < numberOfThreads; i++)
47 {
48 threadCount++;
49 startThread();
50 }
51 }
52
53 public NamedThreadPool(int numberOfThreads, int threadPriority)
54 {
55 for (int i = 0; i < numberOfThreads; i++)
56 {
57 threadCount++;
58 startThread(threadPriority);
59 }
60 }
61
62 public NamedThreadPool(int numberOfThreads, int threadPriority, String name)
63 {
64 for (int i = 0; i < numberOfThreads; i++)
65 {
66 threadCount++;
67 startThread(threadPriority, name);
68 }
69 }
70
71 /** Dispatch a new task onto this pool */
72
73 public void invokeLater(Runnable task)
74 {
75 queue.add(task);
76 }
77
78 /** The method ran by the pool of background threads
79 */
80 public void run()
81 {
82 while (!stopped)
83 {
84 Runnable task = (Runnable) queue.remove();
85 if (task != null)
86 {
87 try
88 {
89 task.run();
90 }
91 catch (Throwable t)
92 {
93 if (exceptionListener != null)
94 {
95 exceptionListener.onException(t);
96 }
97 else
98 {
99 handleException(t);
100 }
101
102 }
103 }
104 }
105 }
106
107 /** Start a new thread running */
108 public Thread startThread()
109 {
110 Thread thread = new Thread(this);
111 thread.start();
112 return thread;
113 }
114
115 public Thread startThread(int priority)
116 {
117 Thread thread = new Thread(this);
118 thread.setPriority(priority);
119 thread.start();
120 return thread;
121 }
122
123 public Thread startThread(int priority, String name)
124 {
125 Thread thread = new Thread(this, name + "-" + threadCount);
126 thread.setPriority(priority);
127 thread.start();
128 return thread;
129 }
130
131 public void stop()
132 {
133 stopped = true;
134 }
135
136 /**
137 * Returns number of runnable object in the queue.
138 */
139 public int size()
140 {
141 return queue.size();
142 }
143
144 protected void handleException(Throwable t)
145 {
146 if (exceptionListener != null)
147 {
148 exceptionListener.onException(t);
149 }
150 else
151 {
152 log.error("ThreadPool caught: " + t, t);
153 }
154 }
155 /**
156 * @return
157 */
158 public ExceptionListener getExceptionListener()
159 {
160 return exceptionListener;
161 }
162
163 /**
164 * @param listener
165 */
166 public void setExceptionListener(ExceptionListener listener)
167 {
168 exceptionListener = listener;
169 }
170
171 }