1 package snow.concurrent; 2 3 import java.util.concurrent.*; 4 import java.util.*; 5 6 10 public abstract class InterruptableTask <T> implements Callable<T>, Runnable 11 { 12 Thread thread; 13 final public Interrupter interrupter = new Interrupter(); Future future; private boolean isExecuting = false; 16 17 18 public InterruptableTask() 19 { 20 21 } 23 public boolean isExecuting() { return isExecuting; } 24 25 public final T call() throws Exception 26 { 27 if(this.interrupter.shouldStopEvaluation()) 28 { 29 return null; 31 } 32 33 isExecuting = true; 34 thread = new Thread (this); 35 try 37 { 38 thread.start(); 39 thread.join(); 40 } 41 catch(Exception e) 42 { 43 kill(); 44 throw e; 45 } 46 finally 47 { 48 isExecuting = false; 49 } 50 return null; 51 } 52 53 58 public void kill() 59 { 60 if(thread!=null && thread.isAlive()) 61 { 62 thread.interrupt(); } 64 65 } 67 68 public void requestStop() 69 { 70 if(interrupter.shouldStopEvaluation()) 71 { 72 kill(); 74 } 75 else 76 { 77 interrupter.stopEvaluation(); 79 } 80 } 81 82 83 } | Popular Tags |