KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > components > threadpool > ThreadPool


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.components.threadpool;
57
58 import org.jboss.axis.i18n.Messages;
59 import org.jboss.logging.Logger;
60
61 import java.util.Hashtable JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Map JavaDoc;
64
65 /**
66  * @author James M Snell (jasnell@us.ibm.com)
67  */

68 public class ThreadPool
69 {
70
71    private static Logger log = Logger.getLogger(ThreadPool.class.getName());
72
73    public static final long MAX_THREADS = 100;
74
75    protected Map JavaDoc threads = new Hashtable JavaDoc();
76    protected long threadcount;
77    public boolean _shutdown;
78
79    public void cleanup()
80            throws InterruptedException JavaDoc
81    {
82       if (log.isDebugEnabled())
83       {
84          log.debug("Enter: ThreadPool::cleanup");
85       }
86       if (!isShutdown())
87       {
88          safeShutdown();
89          awaitShutdown();
90       }
91       synchronized (this)
92       {
93          threads.clear();
94          _shutdown = false;
95       }
96       if (log.isDebugEnabled())
97       {
98          log.debug("Exit: ThreadPool::cleanup");
99       }
100    }
101
102    /**
103     * Returns true if all workers have been shutdown
104     */

105    public boolean isShutdown()
106    {
107       synchronized (this)
108       {
109          return _shutdown && threadcount == 0;
110       }
111    }
112
113    /**
114     * Returns true if all workers are in the process of shutting down
115     */

116    public boolean isShuttingDown()
117    {
118       synchronized (this)
119       {
120          return _shutdown;
121       }
122    }
123
124    /**
125     * Returns the total number of currently active workers
126     */

127    public long getWorkerCount()
128    {
129       synchronized (this)
130       {
131          return threadcount;
132       }
133    }
134
135    /**
136     * Adds a new worker to the pool
137     */

138    public void addWorker(Runnable JavaDoc worker)
139    {
140       if (log.isDebugEnabled())
141       {
142          log.debug("Enter: ThreadPool::addWorker");
143       }
144       if (_shutdown ||
145               threadcount == MAX_THREADS)
146          throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
147       Thread JavaDoc thread = new Thread JavaDoc(worker);
148       threads.put(worker, thread);
149       threadcount++;
150       thread.start();
151       if (log.isDebugEnabled())
152       {
153          log.debug("Exit: ThreadPool::addWorker");
154       }
155    }
156
157    /**
158     * Forcefully interrupt all workers
159     */

160    public void interruptAll()
161    {
162       if (log.isDebugEnabled())
163       {
164          log.debug("Enter: ThreadPool::interruptAll");
165       }
166       synchronized (threads)
167       {
168          for (Iterator JavaDoc i = threads.values().iterator(); i.hasNext();)
169          {
170             Thread JavaDoc t = (Thread JavaDoc)i.next();
171             t.interrupt();
172          }
173       }
174       if (log.isDebugEnabled())
175       {
176          log.debug("Exit: ThreadPool::interruptAll");
177       }
178    }
179
180    /**
181     * Forcefully shutdown the pool
182     */

183    public void shutdown()
184    {
185       if (log.isDebugEnabled())
186       {
187          log.debug("Enter: ThreadPool::shutdown");
188       }
189       synchronized (this)
190       {
191          _shutdown = true;
192       }
193       interruptAll();
194       if (log.isDebugEnabled())
195       {
196          log.debug("Exit: ThreadPool::shutdown");
197       }
198    }
199
200    /**
201     * Forcefully shutdown the pool
202     */

203    public void safeShutdown()
204    {
205       if (log.isDebugEnabled())
206       {
207          log.debug("Enter: ThreadPool::safeShutdown");
208       }
209       synchronized (this)
210       {
211          _shutdown = true;
212       }
213       if (log.isDebugEnabled())
214       {
215          log.debug("Exit: ThreadPool::safeShutdown");
216       }
217    }
218
219    /**
220     * Await shutdown of the worker
221     */

222    public synchronized void awaitShutdown()
223            throws InterruptedException JavaDoc
224    {
225       if (log.isDebugEnabled())
226       {
227          log.debug("Enter: ThreadPool::awaitShutdown");
228       }
229       if (!_shutdown)
230          throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
231       while (threadcount > 0)
232          wait();
233       if (log.isDebugEnabled())
234       {
235          log.debug("Exit: ThreadPool::awaitShutdown");
236       }
237    }
238
239    /**
240     * Await shutdown of the worker
241     */

242    public synchronized boolean awaitShutdown(long timeout)
243            throws InterruptedException JavaDoc
244    {
245       if (log.isDebugEnabled())
246       {
247          log.debug("Enter: ThreadPool::awaitShutdown");
248       }
249       if (!_shutdown)
250          throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
251       if (threadcount == 0)
252       {
253          if (log.isDebugEnabled())
254          {
255             log.debug("Exit: ThreadPool::awaitShutdown");
256          }
257          return true;
258       }
259       long waittime = timeout;
260       if (waittime <= 0)
261       {
262          if (log.isDebugEnabled())
263          {
264             log.debug("Exit: ThreadPool::awaitShutdown");
265          }
266          return false;
267       }
268       long start = System.currentTimeMillis();
269       for (; ;)
270       {
271          wait(waittime);
272          if (threadcount == 0)
273          {
274             if (log.isDebugEnabled())
275             {
276                log.debug("Exit: ThreadPool::awaitShutdown");
277             }
278             return true;
279          }
280          waittime = timeout - System.currentTimeMillis();
281          if (waittime <= 0)
282          {
283             if (log.isDebugEnabled())
284             {
285                log.debug("Exit: ThreadPool::awaitShutdown");
286             }
287             return false;
288          }
289       }
290    }
291
292    /**
293     * Used by MessageWorkers to notify the pool that it is done
294     */

295    public void workerDone(Runnable JavaDoc worker,
296                           boolean restart)
297    {
298       if (log.isDebugEnabled())
299       {
300          log.debug("Enter: ThreadPool::workerDone");
301       }
302       synchronized (this)
303       {
304          threads.remove(worker);
305          if (--threadcount == 0 && _shutdown)
306          {
307             notifyAll();
308          }
309          if (!_shutdown && restart)
310          {
311             addWorker(worker);
312          }
313       }
314       if (log.isDebugEnabled())
315       {
316          log.debug("Exit: ThreadPool::workerDone");
317       }
318    }
319 }
320
321
Popular Tags