1 26 27 package net.sourceforge.groboutils.util.thread.v1; 28 29 30 43 public class LoopThread 44 { 45 public static final long MILLI_IN_SECOND = 1000; 46 47 48 49 private boolean tryPause = false; 50 51 52 private boolean tryStop = false; 53 54 55 private boolean isPaused = false; 56 57 58 private boolean isRunning = false; 59 60 61 private Object pauseObject = new Object (); 62 63 64 private long sleepTime = 6 * MILLI_IN_SECOND; 65 66 67 private Thread thisThread; 68 69 70 private Runnable runnable; 71 72 76 private class LoopRunnable implements Runnable 77 { 78 public void run() 79 { 80 isRunning = true; 81 isPaused = false; 82 do 83 { 84 synchronized( pauseObject ) 88 { 89 if (tryPause) 90 { 91 isPaused = true; 92 try 93 { 94 pauseObject.wait(); 95 } 96 catch (InterruptedException ie) 97 { 98 tryStop = true; 100 } 101 isPaused = false; 102 continue; 105 } 106 } 107 109 if (sleepTime > 0 && !tryStop) 110 { 111 try { Thread.sleep( sleepTime ); } 113 catch (InterruptedException ie) {} 114 } 115 116 if (!tryStop) 118 { 119 runnable.run(); 121 } 122 } while (!tryStop); 123 isRunning = false; 124 } 125 } 126 127 128 132 public LoopThread() 133 { 134 this.thisThread = new Thread ( new LoopRunnable() ); 135 } 136 137 140 public LoopThread( Runnable loop ) 141 { 142 this.runnable = loop; 143 this.thisThread = new Thread ( new LoopRunnable() ); 144 } 145 146 149 public LoopThread( Runnable loop, String threadName ) 150 { 151 this.runnable = loop; 152 this.thisThread = new Thread ( new LoopRunnable() ); 153 } 154 155 158 public LoopThread( Runnable loop, ThreadGroup group ) 159 { 160 this.runnable = loop; 161 this.thisThread = new Thread ( group, new LoopRunnable() ); 162 } 163 164 167 public LoopThread( Runnable loop, ThreadGroup group, String threadName ) 168 { 169 this.runnable = loop; 170 this.thisThread = new Thread ( group, new LoopRunnable(), threadName ); 171 } 172 173 174 177 public int getPriority() 178 { 179 return this.thisThread.getPriority(); 180 } 181 182 183 187 public void setPriority( int priority ) 188 { 189 this.thisThread.setPriority( priority ); 190 } 191 192 193 197 public ThreadGroup getThreadGroup() 198 { 199 return this.thisThread.getThreadGroup(); 200 } 201 202 206 public boolean isAlive() 207 { 208 return this.thisThread.isAlive(); 209 } 210 211 212 216 public boolean isDaemon() 217 { 218 return this.thisThread.isDaemon(); 219 } 220 221 222 226 public void setDaemon( boolean on ) 227 { 228 this.thisThread.setDaemon( on ); 229 } 230 231 235 public void join() throws InterruptedException 236 { 237 if (this.isRunning) 238 { 239 throw new InterruptedException ("can't join - thread is running"); 240 } 241 this.thisThread.join(); 242 } 243 244 245 249 public void setRunnable( Runnable run ) 250 { 251 if (this.isRunning) 252 { 253 throw new IllegalStateException ("can't change a running thread's "+ 254 "runnable" ); 255 } 256 this.runnable = run; 257 } 258 259 260 261 265 public void start() 266 { 267 if (this.isRunning) 268 { 269 throw new IllegalStateException ("can't start a running thread"); 270 } 271 this.tryPause = false; 272 this.tryStop = false; 273 this.thisThread.start(); 274 } 275 276 277 282 public void suspend() 283 { 284 if (this.tryStop || !this.isRunning) 285 { 286 throw new IllegalStateException ("Cannot pause a stopped thread"); 287 } 288 this.tryPause = true; 289 } 290 291 295 public void resume() 296 { 297 synchronized( this.pauseObject ) 298 { 299 if (!this.isPaused) return; 300 this.tryPause = false; 301 this.pauseObject.notifyAll(); 302 } 303 } 304 305 309 public void stop() 310 { 311 if (!this.isRunning) 312 { 313 return; } 315 this.tryStop = true; 316 317 resume(); 320 } 321 322 325 public boolean isPaused() 326 { 327 return this.isPaused; 328 } 329 330 333 public boolean isRunning() 334 { 335 return this.isRunning; 336 } 337 338 341 public int getSleepTime() 342 { 343 return (int)(this.sleepTime / MILLI_IN_SECOND); 344 } 345 346 349 public void setSleepTime( int seconds ) 350 { 351 this.sleepTime = seconds * MILLI_IN_SECOND; 352 } 353 354 357 public void setSleepTimeMillis( long millis ) 358 { 359 this.sleepTime = millis; 360 } 361 362 363 366 public long getSleepTimeMillis() 367 { 368 return this.sleepTime; 369 } 370 371 372 373 377 public String toString() 378 { 379 return this.thisThread.toString(); 380 } 381 } 382 | Popular Tags |