1 20 21 package org.snmp4j.util; 22 23 import java.util.*; 24 25 35 public class ThreadPool { 36 37 protected Vector taskManagers; 38 protected String name = "ThreadPool"; 39 protected volatile boolean stop = false; 40 protected boolean respawnThreads = false; 41 42 protected ThreadPool() { 43 } 44 45 protected String getTaskManagerName(String prefix, int index) { 46 return prefix+"."+index; 47 } 48 49 protected void setup(String name, int size) { 50 this.name = name; 51 taskManagers = new Vector(size); 52 for (int i=0; i<size; i++) { 53 TaskManager tm = new TaskManager(getTaskManagerName(name, i)); 54 taskManagers.add(tm); 55 tm.start(); 56 } 57 } 58 59 69 public static ThreadPool create(String name, int size) { 70 ThreadPool pool = new ThreadPool(); 71 pool.setup(name, size); 72 return pool; 73 } 74 75 82 public synchronized void execute(Runnable task) { 83 while (true) { 84 for (int i=0; i<taskManagers.size(); i++) { 85 TaskManager tm = (TaskManager) taskManagers.get(i); 86 if ((respawnThreads) && (!tm.isAlive())) { 87 tm = new TaskManager(getTaskManagerName(name, i)); 88 } 89 if (tm.isIdle()) { 90 tm.execute(task); 91 return; 92 } 93 } 94 try { 95 wait(); 96 } 97 catch (InterruptedException ex) { 98 } 100 } 101 } 102 103 113 public synchronized boolean tryToExecute(Runnable task) { 114 for (int i=0; i<taskManagers.size(); i++) { 115 TaskManager tm = (TaskManager) taskManagers.get(i); 116 if ((respawnThreads) && (!tm.isAlive())) { 117 tm = new TaskManager(getTaskManagerName(name, i)); 118 } 119 if (tm.isIdle()) { 120 tm.execute(task); 121 return true; 122 } 123 } 124 return false; 125 } 126 127 133 public boolean isRespawnThreads() { 134 return respawnThreads; 135 } 136 137 143 public void setRespawnThreads(boolean respawnThreads) { 144 this.respawnThreads = respawnThreads; 145 } 146 147 152 public String getName() { 153 return name; 154 } 155 156 160 public void stop() { 161 List tms; 162 synchronized (this) { 163 stop = true; 164 tms = (List) taskManagers.clone(); 165 } 166 for (int i=0; i<tms.size(); i++) { 167 TaskManager tm = (TaskManager) tms.get(i); 168 tm.terminate(); 169 synchronized (tm) { 170 tm.notify(); 171 } 172 try { 173 tm.join(); 174 } 175 catch (InterruptedException ex) { 176 } 178 } 179 } 180 181 184 public synchronized void cancel() { 185 stop = true; 186 for (int i=0; i<taskManagers.size(); i++) { 187 TaskManager tm = (TaskManager) taskManagers.get(i); 188 tm.terminate(); 189 tm.interrupt(); 190 } 191 } 192 193 197 public synchronized void interrupt() { 198 for (int i=0; i<taskManagers.size(); i++) { 199 TaskManager tm = (TaskManager) taskManagers.get(i); 200 tm.interrupt(); 201 } 202 } 203 204 210 public synchronized boolean isIdle() { 211 for (int i=0; i<taskManagers.size(); i++) { 212 TaskManager tm = (TaskManager) taskManagers.get(i); 213 if (!tm.isIdle()) { 214 return false; 215 } 216 } 217 return true; 218 } 219 220 227 class TaskManager extends Thread { 228 229 private Runnable task = null; 230 private volatile boolean run = true; 231 232 public TaskManager(String name) { 233 super(name); 234 } 235 236 public synchronized void run() { 237 while ((!stop) && run) { 238 if (task != null) { 239 task.run(); 240 synchronized (ThreadPool.this) { 241 task = null; 242 ThreadPool.this.notify(); 243 } 244 } 245 else { 246 try { 247 wait(); 248 } 249 catch (InterruptedException ex) { 250 run = respawnThreads; 251 break; 252 } 253 } 254 } 255 } 256 257 public boolean isIdle() { 258 return ((task == null) && run); 259 } 260 261 public boolean isStopped() { 262 return stop; 263 } 264 265 public void terminate() { 266 stop = true; 267 } 268 269 public synchronized void execute(Runnable task) { 270 if (this.task == null) { 271 this.task = task; 272 notify(); 273 } 274 else { 275 throw new IllegalStateException ("TaskManager is not idle"); 276 } 277 } 278 } 279 } 280 | Popular Tags |