1 package hudson.model; 2 3 import java.util.TimerTask ; 4 import java.util.logging.Level ; 5 import java.util.logging.Logger ; 6 7 12 public abstract class PeriodicWork extends TimerTask { 13 14 17 private final String name; 18 private Thread thread; 19 20 protected final Logger logger = Logger.getLogger(getClass().getName()); 21 22 protected PeriodicWork(String name) { 23 this.name = name; 24 } 25 26 29 public final void run() { 30 try { 31 if(thread!=null && thread.isAlive()) { 32 logger.log(Level.INFO, name+" thread is still running. Execution aborted."); 33 return; 34 } 35 thread = new Thread (new Runnable () { 36 public void run() { 37 logger.log(Level.INFO, "Started "+name); 38 long startTime = System.currentTimeMillis(); 39 40 execute(); 41 42 logger.log(Level.INFO, "Finished "+name+". "+ 43 (System.currentTimeMillis()-startTime)+" ms"); 44 } 45 },name+" thread"); 46 thread.start(); 47 } catch (Throwable t) { 48 logger.log(Level.SEVERE, name+" thread failed with error", t); 49 } 50 } 51 52 protected abstract void execute(); 53 } 54 | Popular Tags |