KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > 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;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 /**
20  * Simple fized sized Thread Pool
21  */

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

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

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

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

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

80     public void doTask(Runnable JavaDoc task) {
81         WorkerThread worker = null;
82
83         synchronized (_pool) {
84             while (_pool.isEmpty()) {
85                 if (_stopped) {
86                     return;
87                 }
88
89                 try {
90                     _pool.wait();
91                 } catch (InterruptedException JavaDoc ex) {
92                     System.err.println(ex.getMessage());
93
94                     return;
95                 }
96             }
97
98             worker = (WorkerThread) _pool.remove(0);
99         }
100
101         worker.runTask(task);
102     }
103
104     private class WorkerThread extends Thread JavaDoc {
105         private boolean stop = false;
106         private Runnable JavaDoc task;
107
108         public WorkerThread(int i) {
109             setName("Worker" + i);
110         }
111
112         /**
113          * Interupt the WorkerThread
114          */

115         public synchronized void kill() {
116             stop = true;
117             notify();
118         }
119
120         public void runTask(Runnable JavaDoc runnable) {
121             task = runnable;
122
123             synchronized (this) {
124                 notifyAll();
125             }
126         }
127
128         public void run() {
129             if (_debugging) {
130                 System.err.println(getName() + ".run: ->");
131             }
132
133             while (!stop) {
134                 if (_debugging) {
135                     System.err.println(getName() + ".run: Loop, task = " + task);
136                 }
137
138                 if (task == null) {
139                     if (_debugging) {
140                         System.err.println(getName() + ".run: Waiting for task ...");
141                     }
142
143                     try {
144                         synchronized (this) {
145                             wait();
146                         }
147                     } catch (InterruptedException JavaDoc ex) {
148                         // ignore
149
}
150
151                     if (_debugging) {
152                         System.err.println(getName() + ".run: Done waiting");
153                     }
154                 } else {
155                     if (_debugging) {
156                         System.err.println(getName() + ".run: Task = " + task);
157                     }
158
159                     try {
160
161                         task.run();
162
163                         if (_debugging) {
164                             System.err.println(getName() + ".run: Done " + task);
165                         }
166                     } catch (Throwable JavaDoc t) {
167                         t.printStackTrace(System.err);
168                     }
169
170                     task = null;
171
172                     if (_stopped) {
173                         if (_debugging) {
174                             System.err.println(getName() + ".run: <- (Stopped)");
175                         }
176
177                         return;
178                     }
179
180                     synchronized (_pool) {
181                         _pool.add(this);
182                         _pool.notifyAll();
183                     }
184                 }
185             }
186
187             if (_debugging) {
188                 System.err.println(getName() + ".run: <-");
189             }
190         }
191     }
192 }
193
Popular Tags