1 45 package org.exolab.jms.common.threads; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 import org.exolab.jms.common.util.FifoQueue; 51 52 53 64 class ThreadPoolWorker { 65 66 69 private ThreadGroup _group; 70 71 74 private FifoQueue _idleWorkers; 75 76 81 private FifoQueue _waitUntilWork; 82 83 86 private Thread _worker; 87 88 91 private volatile boolean _noStopRequested; 92 93 96 private static final Log _log = LogFactory.getLog(ThreadPoolWorker.class); 97 98 107 public ThreadPoolWorker(ThreadGroup group, String name, 108 FifoQueue idleWorkers) { 109 _group = group; 110 _idleWorkers = idleWorkers; 111 112 _waitUntilWork = new FifoQueue(1); 114 _noStopRequested = true; 115 116 Runnable r = new Runnable () { 117 public void run() { 118 try { 119 runWork(); 120 } catch (Exception exception) { 121 _log.error("Thread " + _worker.getName() 122 + ": terminating on exception", exception); 123 } 124 } 125 }; 126 127 _worker = new Thread (_group, r, name); 129 _worker.setDaemon(_group.isDaemon()); 130 _worker.start(); 131 } 132 133 140 public void process(Runnable target) throws InterruptedException { 141 _waitUntilWork.add(target); 142 } 143 144 148 public void stopRequest() { 149 _noStopRequested = false; 150 _worker.interrupt(); 151 } 152 153 158 public boolean isAlive() { 159 return _worker.isAlive(); 160 } 161 162 166 private void runWork() { 167 while (_noStopRequested) { 168 try { 169 _idleWorkers.add(this); 173 174 Runnable r = (Runnable ) _waitUntilWork.get(); 176 177 runIt(r); 178 } catch (InterruptedException exception) { 179 Thread.currentThread().interrupt(); } 181 } 182 } 183 184 190 private void runIt(Runnable r) { 191 try { 192 r.run(); 193 } catch (Exception exception) { 194 _log.error("Thread " + _worker.getName() 196 + ": uncaught exception fell through from run()", 197 exception); 198 } finally { 199 Thread.interrupted(); 204 } 205 } 206 207 } 208 | Popular Tags |