KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > util > ThreadPool


1 /*
2  * javainpractice.net 1999-2002
3  *
4  * This software is intended to be used for educational purposes only.
5  *
6  * We make no representations or warranties about the
7  * suitability of the software.
8  *
9  * Any feedback relating to this software can be sent to
10  * info@javainpractice.net
11  *
12  */

13 package net.sourceforge.cvsgrab.util;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import net.sourceforge.cvsgrab.CVSGrab;
19
20
21 /**
22  * Simple fized sized Thread Pool
23  */

24 public class ThreadPool {
25     private static ThreadPool _instance;
26     private final boolean _debugging = false;
27     private List JavaDoc _allThreads = new ArrayList JavaDoc();
28     private List JavaDoc _pool = new ArrayList JavaDoc();
29     private boolean _stopped = false;
30     
31     /**
32      * Initialise the thread pool
33      * @param maxThreads The max number of threads
34      */

35     public static void init(int maxThreads) {
36         CVSGrab.getLog().info("Using up to " + maxThreads + " simultaneous connections to load files");
37         _instance = new ThreadPool(maxThreads);
38     }
39     
40     /**
41      * @return the singleton instance of the thread pool
42      */

43     public static ThreadPool getInstance() {
44         return _instance;
45     }
46
47     /**
48      * Create a thread pool with max number of threads
49      */

50     public ThreadPool(int max) {
51         for (int i = 0; i < max; i++) {
52             WorkerThread worker = new WorkerThread(i);
53
54             _pool.add(worker);
55             _allThreads.add(worker);
56             worker.start();
57         }
58     }
59
60     /**
61      * Interrupt all threads and clear the pool
62      * This method should only be used to tidy up.
63      * The ThreadPool should not be used after invoking this method
64      */

65     public void destroy() {
66         _stopped = true;
67
68         for (int i = 0; i < _allThreads.size(); i++) {
69             WorkerThread wt = (WorkerThread) _allThreads.get(i);
70
71             if (_debugging) {
72                 System.err.println(Thread.currentThread().getName() + ".destroy: Killing " + wt.getName());
73             }
74
75             wt.kill();
76         }
77     }
78
79     /**
80      * Execute a Runnable task
81      */

82     public void doTask(Runnable JavaDoc task) {
83         WorkerThread worker = null;
84
85         synchronized (getPool()) {
86             while (getPool().isEmpty()) {
87                 if (isStopped()) {
88                     return;
89                 }
90
91                 try {
92                     getPool().wait();
93                 } catch (InterruptedException JavaDoc ex) {
94                     System.err.println(ex.getMessage());
95
96                     return;
97                 }
98             }
99
100             worker = (WorkerThread) getPool().remove(0);
101         }
102
103         worker.runTask(task);
104     }
105
106     /**
107      * Gets the pool.
108      * @return the pool.
109      */

110     protected List JavaDoc getPool() {
111         return _pool;
112     }
113
114     /**
115      * Gets the stopped.
116      * @return the stopped.
117      */

118     protected boolean isStopped() {
119         return _stopped;
120     }
121
122     private class WorkerThread extends Thread JavaDoc {
123         private boolean stop = false;
124         private Runnable JavaDoc task;
125
126         public WorkerThread(int i) {
127             setName("Worker" + i);
128         }
129
130         /**
131          * Interupt the WorkerThread
132          */

133         public synchronized void kill() {
134             stop = true;
135             notify();
136         }
137
138         public void runTask(Runnable JavaDoc runnable) {
139             task = runnable;
140
141             synchronized (this) {
142                 notifyAll();
143             }
144         }
145
146         public void run() {
147             if (_debugging) {
148                 System.err.println(getName() + ".run: ->");
149             }
150
151             while (!stop) {
152                 if (_debugging) {
153                     System.err.println(getName() + ".run: Loop, task = " + task);
154                 }
155
156                 if (task == null) {
157                     if (_debugging) {
158                         System.err.println(getName() + ".run: Waiting for task ...");
159                     }
160
161                     try {
162                         synchronized (this) {
163                             wait();
164                         }
165                     } catch (InterruptedException JavaDoc ex) {
166                         // ignore
167
}
168
169                     if (_debugging) {
170                         System.err.println(getName() + ".run: Done waiting");
171                     }
172                 } else {
173                     if (_debugging) {
174                         System.err.println(getName() + ".run: Task = " + task);
175                     }
176
177                     try {
178
179                         task.run();
180
181                         if (_debugging) {
182                             System.err.println(getName() + ".run: Done " + task);
183                         }
184                     } catch (Throwable JavaDoc t) {
185                         t.printStackTrace(System.err);
186                     }
187
188                     task = null;
189
190                     if (isStopped()) {
191                         if (_debugging) {
192                             System.err.println(getName() + ".run: <- (Stopped)");
193                         }
194
195                         return;
196                     }
197
198                     synchronized (getPool()) {
199                         getPool().add(this);
200                         getPool().notifyAll();
201                     }
202                 }
203             }
204
205             if (_debugging) {
206                 System.err.println(getName() + ".run: <-");
207             }
208         }
209     }
210 }
211
Popular Tags