1 13 package net.sourceforge.cvsgrab; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 19 22 public class ThreadPool { 23 private static ThreadPool _instance; 24 private final boolean _debugging = false; 25 private List _allThreads = new ArrayList (); 26 List _pool = new ArrayList (); 27 boolean _stopped = false; 28 29 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 41 public static ThreadPool getInstance() { 42 return _instance; 43 } 44 45 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 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 80 public void doTask(Runnable 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 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 { 105 private boolean stop = false; 106 private Runnable task; 107 108 public WorkerThread(int i) { 109 setName("Worker" + i); 110 } 111 112 115 public synchronized void kill() { 116 stop = true; 117 notify(); 118 } 119 120 public void runTask(Runnable 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 ex) { 148 } 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 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 |