1 13 package net.sourceforge.cvsgrab.util; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import net.sourceforge.cvsgrab.CVSGrab; 19 20 21 24 public class ThreadPool { 25 private static ThreadPool _instance; 26 private final boolean _debugging = false; 27 private List _allThreads = new ArrayList (); 28 private List _pool = new ArrayList (); 29 private boolean _stopped = false; 30 31 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 43 public static ThreadPool getInstance() { 44 return _instance; 45 } 46 47 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 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 82 public void doTask(Runnable 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 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 110 protected List getPool() { 111 return _pool; 112 } 113 114 118 protected boolean isStopped() { 119 return _stopped; 120 } 121 122 private class WorkerThread extends Thread { 123 private boolean stop = false; 124 private Runnable task; 125 126 public WorkerThread(int i) { 127 setName("Worker" + i); 128 } 129 130 133 public synchronized void kill() { 134 stop = true; 135 notify(); 136 } 137 138 public void runTask(Runnable 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 ex) { 166 } 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 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 |