1 19 20 package org.gjt.sp.util; 21 22 27 public class WorkThread extends Thread implements ThreadAbortMonitor 28 { 29 public WorkThread(WorkThreadPool pool, ThreadGroup group, String name) 30 { 31 super(group, name); 32 setPriority(Thread.MIN_PRIORITY); 35 36 this.pool = pool; 37 } 38 39 43 public void setAbortable(boolean abortable) 44 { 45 synchronized(abortLock) 46 { 47 this.abortable = abortable; 48 if(aborted) 49 stop(new Abort()); 50 } 51 } 52 53 56 public boolean isRequestRunning() 57 { 58 return requestRunning; 59 } 60 61 62 public boolean isAborted() 63 { 64 synchronized(abortLock) 65 { 66 return aborted; 67 } 68 } 69 70 73 public String getStatus() 74 { 75 return status; 76 } 77 78 82 public void setStatus(String status) 83 { 84 this.status = status; 85 pool.fireProgressChanged(this); 86 } 87 88 91 public int getProgressValue() 92 { 93 return progressValue; 94 } 95 96 100 public void setProgressValue(int progressValue) 101 { 102 this.progressValue = progressValue; 103 pool.fireProgressChanged(this); 104 } 105 106 109 public int getProgressMaximum() 110 { 111 return progressMaximum; 112 } 113 114 118 public void setProgressMaximum(int progressMaximum) 119 { 120 this.progressMaximum = progressMaximum; 121 pool.fireProgressChanged(this); 122 } 123 124 128 public void abortCurrentRequest() 129 { 130 synchronized(abortLock) 131 { 132 if(abortable && !aborted) 133 stop(new Abort()); 134 aborted = true; 135 } 136 } 137 138 public void run() 139 { 140 Log.log(Log.DEBUG,this,"Work request thread starting [" + getName() + "]"); 141 142 for(;;) 143 { 144 doRequests(); 145 } 146 } 147 148 private WorkThreadPool pool; 150 private Object abortLock = new Object (); 151 private boolean requestRunning; 152 private boolean abortable; 153 private boolean aborted; 154 private String status; 155 private int progressValue; 156 private int progressMaximum; 157 158 private void doRequests() 159 { 160 WorkThreadPool.Request request; 161 for(;;) 162 { 163 request = pool.getNextRequest(); 164 if(request == null) 165 break; 166 else 167 { 168 requestRunning = true; 169 pool.fireStatusChanged(this); 170 doRequest(request); 171 requestRunning = false; 172 } 173 } 174 175 pool.fireStatusChanged(this); 176 177 synchronized(pool.waitForAllLock) 178 { 179 pool.waitForAllLock.notifyAll(); 181 } 182 183 synchronized(pool.lock) 184 { 185 try 187 { 188 pool.lock.wait(); 189 } 190 catch(InterruptedException ie) 191 { 192 Log.log(Log.ERROR,this,ie); 193 } 194 } 195 } 196 197 private void doRequest(WorkThreadPool.Request request) 198 { 199 Log.log(Log.DEBUG,WorkThread.class,"Running in work thread: " + request); 200 201 try 202 { 203 request.run.run(); 204 } 205 catch(Abort a) 206 { 207 Log.log(Log.ERROR,WorkThread.class,"Unhandled abort", a); 208 } 209 catch(Throwable t) 210 { 211 Log.log(Log.ERROR,WorkThread.class,"Exception in work thread: ", t); 212 } 213 finally 214 { 215 synchronized(abortLock) 216 { 217 aborted = abortable = false; 218 } 219 status = null; 220 progressValue = progressMaximum = 0; 221 pool.requestDone(); 222 pool.fireStatusChanged(this); 223 } 224 } 225 226 public static class Abort extends Error 227 { 228 public Abort() 229 { 230 super("Work request aborted"); 231 } 232 } 233 } 234 | Popular Tags |