1 21 package org.jsmtpd.generic.threadpool; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 31 public class ThreadWorker extends Thread { 32 33 private boolean free = false; 34 private boolean running = true; 35 private IThreadedClass wrk; 36 private Log log = LogFactory.getLog(ThreadWorker.class); 37 38 public void run() { 39 while (true) { 40 goSleep(); 41 if (!running) { 42 log.debug("Worker "+getName()+" stopping"); 43 return; 44 } 45 try { 46 wrk.doJob(); 47 } catch (RuntimeException e) { 48 log.error("Generic Thread pool worker #" + Thread.currentThread().getId() + " Caught a runtime exception : ",e); 49 log.error("Thread job lost, recycling thread"); 50 } 51 } 52 } 53 54 public void forceShutdown() { 55 running = false; 56 free = false; 57 wrk.forceShutdown(); 58 wake(); 59 } 60 61 public void gracefullShutdown() { 62 running = false; 63 free = false; 64 wrk.gracefullShutdown(); 65 wake(); 66 } 67 68 public void setParam(Object o) { 69 wrk.setParam(o); 70 } 71 72 public void setWorker(IThreadedClass cl) { 73 wrk = cl; 74 } 75 76 public boolean isFree() { 77 return free; 78 } 79 80 public synchronized void wake() { 81 free = false; 82 notify(); 83 } 84 85 private synchronized void goSleep() { 86 free = true; 87 try { 88 wait(); 89 } catch (InterruptedException e) { 90 e.printStackTrace(); 91 } 92 } 93 } | Popular Tags |