KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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
17 package org.apache.axis.components.threadpool;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.i18n.Messages;
21 import org.apache.commons.logging.Log;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * @author James M Snell (jasnell@us.ibm.com)
29  */

30 public class ThreadPool {
31
32     protected static Log log =
33         LogFactory.getLog(ThreadPool.class.getName());
34
35     public static final int DEFAULT_MAX_THREADS = 100;
36     
37     protected Map JavaDoc threads = new Hashtable JavaDoc();
38     protected long threadcount;
39     public boolean _shutdown;
40     private int maxPoolSize = DEFAULT_MAX_THREADS;
41
42     public ThreadPool() {
43     }
44
45     public ThreadPool(int maxPoolSize) {
46         this.maxPoolSize = maxPoolSize;
47     }
48
49     public void cleanup()
50         throws InterruptedException JavaDoc {
51         if (log.isDebugEnabled()) {
52             log.debug("Enter: ThreadPool::cleanup");
53         }
54         if (!isShutdown()) {
55           safeShutdown();
56           awaitShutdown();
57         }
58         synchronized(this) {
59           threads.clear();
60           _shutdown = false;
61         }
62         if (log.isDebugEnabled()) {
63             log.debug("Exit: ThreadPool::cleanup");
64         }
65     }
66
67     /**
68      * Returns true if all workers have been shutdown
69      */

70     public boolean isShutdown() {
71         synchronized (this) {
72             return _shutdown && threadcount == 0;
73         }
74     }
75
76     /**
77      * Returns true if all workers are in the process of shutting down
78      */

79     public boolean isShuttingDown() {
80         synchronized (this) {
81             return _shutdown;
82         }
83     }
84
85     /**
86      * Returns the total number of currently active workers
87      */

88     public long getWorkerCount() {
89         synchronized (this) {
90             return threadcount;
91         }
92     }
93
94     /**
95      * Adds a new worker to the pool
96      */

97     public void addWorker(
98             Runnable JavaDoc worker) {
99         if (log.isDebugEnabled()) {
100             log.debug("Enter: ThreadPool::addWorker");
101         }
102         if (_shutdown || threadcount == maxPoolSize) {
103             throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
104         }
105         Thread JavaDoc thread = new Thread JavaDoc(worker);
106         threads.put(worker, thread);
107         threadcount++;
108         thread.start();
109         if (log.isDebugEnabled()) {
110             log.debug("Exit: ThreadPool::addWorker");
111         }
112     }
113
114     /**
115      * Forcefully interrupt all workers
116      */

117     public void interruptAll() {
118         if (log.isDebugEnabled()) {
119             log.debug("Enter: ThreadPool::interruptAll");
120         }
121         synchronized (threads) {
122             for (Iterator JavaDoc i = threads.values().iterator(); i.hasNext();) {
123                 Thread JavaDoc t = (Thread JavaDoc) i.next();
124                 t.interrupt();
125             }
126         }
127         if (log.isDebugEnabled()) {
128             log.debug("Exit: ThreadPool::interruptAll");
129         }
130     }
131
132     /**
133      * Forcefully shutdown the pool
134      */

135     public void shutdown() {
136         if (log.isDebugEnabled()) {
137             log.debug("Enter: ThreadPool::shutdown");
138         }
139         synchronized (this) {
140             _shutdown = true;
141         }
142         interruptAll();
143         if (log.isDebugEnabled()) {
144             log.debug("Exit: ThreadPool::shutdown");
145         }
146     }
147
148     /**
149      * Forcefully shutdown the pool
150      */

151     public void safeShutdown() {
152         if (log.isDebugEnabled()) {
153             log.debug("Enter: ThreadPool::safeShutdown");
154         }
155         synchronized (this) {
156             _shutdown = true;
157         }
158         if (log.isDebugEnabled()) {
159             log.debug("Exit: ThreadPool::safeShutdown");
160         }
161     }
162
163     /**
164      * Await shutdown of the worker
165      */

166     public synchronized void awaitShutdown()
167             throws InterruptedException JavaDoc {
168         if (log.isDebugEnabled()) {
169             log.debug("Enter: ThreadPool::awaitShutdown");
170         }
171         if (!_shutdown)
172             throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
173         while (threadcount > 0)
174             wait();
175         if (log.isDebugEnabled()) {
176             log.debug("Exit: ThreadPool::awaitShutdown");
177         }
178     }
179
180     /**
181      * Await shutdown of the worker
182      */

183     public synchronized boolean awaitShutdown(long timeout)
184             throws InterruptedException JavaDoc {
185         if (log.isDebugEnabled()) {
186             log.debug("Enter: ThreadPool::awaitShutdown");
187         }
188         if (!_shutdown)
189             throw new IllegalStateException JavaDoc(Messages.getMessage("illegalStateException00"));
190         if (threadcount == 0) {
191             if (log.isDebugEnabled()) {
192                 log.debug("Exit: ThreadPool::awaitShutdown");
193             }
194             return true;
195         }
196         long waittime = timeout;
197         if (waittime <= 0) {
198             if (log.isDebugEnabled()) {
199                 log.debug("Exit: ThreadPool::awaitShutdown");
200             }
201             return false;
202         }
203         for (; ;) {
204             wait(waittime);
205             if (threadcount == 0) {
206                 if (log.isDebugEnabled()) {
207                     log.debug("Exit: ThreadPool::awaitShutdown");
208                 }
209                 return true;
210             }
211             waittime = timeout - System.currentTimeMillis();
212             if (waittime <= 0) {
213                 if (log.isDebugEnabled()) {
214                     log.debug("Exit: ThreadPool::awaitShutdown");
215                 }
216                 return false;
217             }
218         }
219     }
220
221     /**
222      * Used by MessageWorkers to notify the pool that it is done
223      */

224     public void workerDone(
225             Runnable JavaDoc worker,
226             boolean restart) {
227         if (log.isDebugEnabled()) {
228             log.debug("Enter: ThreadPool::workerDone");
229         }
230         synchronized(this) {
231             threads.remove(worker);
232             if (--threadcount == 0 && _shutdown) {
233                 notifyAll();
234             }
235             if (!_shutdown && restart) {
236                 addWorker(worker);
237             }
238         }
239         if (log.isDebugEnabled()) {
240             log.debug("Exit: ThreadPool::workerDone");
241         }
242     }
243 }
244
245
Popular Tags