1 8 package org.mmbase.applications.crontab; 9 import java.util.*; 10 11 import org.mmbase.util.logging.*; 12 13 19 20 public class Interruptable implements Runnable { 21 private static final Logger log = Logging.getLoggerInstance(Interruptable.class); 22 private Thread runThread = null; 23 private Date startTime; 24 private final Runnable runnable; 25 private final Collection collection; 26 public Interruptable(Runnable run, Collection col) { 27 runnable = run; 28 collection = col; 29 } 30 31 public void run() { 32 if (runThread != null) throw new IllegalStateException (); 33 if (collection != null) collection.add(this); 34 runThread = Thread.currentThread(); 35 startTime = new Date(); 36 try { 37 runnable.run(); 38 } catch (Throwable t) { 39 log.error(t.getMessage(), t); 40 } 41 42 43 runThread = null; 44 if (collection != null) collection.remove(this); 45 } 46 47 public boolean interrupt() { 48 Thread t = runThread; 49 if (t != null) { 50 t.interrupt(); 51 return true; 52 } 53 return false; 54 } 55 public boolean isAlive() { 56 Thread t = runThread; 57 return t != null && t.isAlive(); 58 } 59 public Date getStartTime() { 60 return startTime; 61 } 62 } 63 | Popular Tags |