KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > util > threadpool > ThreadPool


1 package org.apache.axis2.util.threadpool;
2
3 import org.apache.axis2.engine.AxisFault;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12  * This the thread pool for axis2. This class will be used a singleton
13  * across axis2 engine. <code>ThreadPool</code> is accepts <code>AxisWorkers</code> which has
14  * doWork method on them and execute this method, using one of the threads
15  * in the thread pool.
16  */

17
18 public class ThreadPool {
19
20     protected static Log log = LogFactory.getLog(ThreadPool.class.getName());
21
22     private static int MAX_THREAD_COUNT = 10;
23     protected static long SLEEP_INTERVAL = 1000;
24     private static List JavaDoc threads;
25     private static List JavaDoc tasks;
26     private static boolean shoutDown;
27
28     public ThreadPool() {
29         threads = new ArrayList JavaDoc();
30         tasks = new ArrayList JavaDoc();
31
32         for (int i = 0; i < MAX_THREAD_COUNT; i++) {
33             ThreadWorker threadWorker = new ThreadWorker();
34             threadWorker.setPool(this);
35             threads.add(threadWorker);
36             threadWorker.start();
37         }
38
39     }
40
41     public void addWorker(AxisWorker worker) throws AxisFault {
42         if (shoutDown)
43             throw new AxisFault("Thread Pool is Shutting Down");
44         tasks.add(worker);
45     }
46
47     public synchronized AxisWorker getWorker() {
48         if (!tasks.isEmpty()) {
49             AxisWorker worker = (AxisWorker) tasks.get(0);
50             tasks.remove(worker);
51             return worker;
52         } else
53             return null;
54     }
55
56     /**
57      * This is the recommended shutdown method for the thread pool
58      * This will wait till all the workers that are already handed over to the
59      * thread pool get executed.
60      *
61      * @throws AxisFault
62      */

63     public void safeShutDown() throws AxisFault {
64         synchronized (this) {
65             shoutDown = true;
66         }
67         while (!tasks.isEmpty()) {
68             try {
69                 Thread.sleep(SLEEP_INTERVAL);
70             } catch (InterruptedException JavaDoc e) {
71                 throw new AxisFault("Error while safeShutDown", e);
72             }
73         }
74         forceShutDown();
75
76     }
77
78     /**
79      * A forceful shutdown mechanism for thread pool.
80      */

81     public void forceShutDown() {
82         if (log.isDebugEnabled())
83             log.debug("forceShutDown called. Thread workers will be stopped");
84         Iterator JavaDoc ite = threads.iterator();
85         while (ite.hasNext()) {
86             ThreadWorker worker = (ThreadWorker) ite.next();
87             worker.setStop(true);
88         }
89     }
90 }
91
Popular Tags