1 22 package org.jboss.util; 23 24 33 public class WorkerQueue 34 { 35 36 protected Thread m_queueThread; 37 38 39 private JobItem m_currentJob; 40 41 44 public WorkerQueue() 45 { 46 this("Worker Thread"); 47 } 48 49 52 public WorkerQueue(String threadName) 53 { 54 m_queueThread = new Thread (createQueueLoop(), threadName); 55 } 56 57 61 public WorkerQueue(String threadName, boolean isDaemon) 62 { 63 m_queueThread = new Thread (createQueueLoop(), threadName); 64 m_queueThread.setDaemon(isDaemon); 65 } 66 67 71 public void start() 72 { 73 if (m_queueThread != null) {m_queueThread.start();} 74 } 75 76 86 public synchronized void stop() 87 { 88 if (m_queueThread != null) {m_queueThread.interrupt();} 89 } 90 91 96 public synchronized void putJob(Executable job) 97 { 98 if (m_queueThread == null || !m_queueThread.isAlive()) { 100 throw new IllegalStateException ("Can't put job, thread is not alive or not present"); 101 } 102 103 if (isInterrupted()) { 104 throw new IllegalStateException ("Can't put job, thread was interrupted"); 105 } 106 107 putJobImpl(job); 108 } 109 110 118 protected boolean isInterrupted() 119 { 120 return m_queueThread.isInterrupted(); 121 } 122 123 129 protected synchronized Executable getJob() throws InterruptedException 130 { 131 if (m_queueThread == null || !m_queueThread.isAlive()) { 133 throw new IllegalStateException (); 134 } 135 136 return getJobImpl(); 137 } 138 139 144 protected Executable getJobImpl() throws InterruptedException 145 { 146 while (m_currentJob == null) {wait();} 149 JobItem item = m_currentJob; 151 m_currentJob = m_currentJob.m_next; 153 return item.m_job; 154 } 155 156 161 protected void putJobImpl(Executable job) 162 { 163 JobItem posted = new JobItem(job); 164 if (m_currentJob == null) 165 { 166 m_currentJob = posted; 169 notifyAll(); 170 } 171 else 172 { 173 JobItem item = m_currentJob; 174 while (item.m_next != null) {item = item.m_next;} 177 item.m_next = posted; 178 } 179 } 180 181 185 protected void clear() 186 { 187 m_queueThread = null; 188 m_currentJob = null; 189 } 190 191 195 protected Runnable createQueueLoop() { 196 return new QueueLoop(); 197 } 198 199 203 protected class QueueLoop 204 implements Runnable 205 { 206 public void run() 207 { 208 try 209 { 210 while (true) 211 { 212 try 213 { 214 if (isInterrupted()) 215 { 216 flush(); 217 break; 218 } 219 else 220 { 221 getJob().execute(); 222 } 223 } 224 catch (InterruptedException e) 225 { 226 try { 227 flush(); 228 } 229 catch (Exception ignored) {} 230 break; 231 } 232 catch (Exception e) { 233 ThrowableHandler.add(ThrowableHandler.Type.ERROR, e); 234 } 235 } 236 } 237 finally { 238 clear(); 239 } 240 } 241 242 protected void flush() throws Exception 243 { 244 while (m_currentJob != null) 246 { 247 m_currentJob.m_job.execute(); 248 m_currentJob = m_currentJob.m_next; 249 } 250 } 251 } 252 253 256 private class JobItem 257 { 258 private Executable m_job; 259 private JobItem m_next; 260 private JobItem(Executable job) {m_job = job;} 261 } 262 } 263 | Popular Tags |