KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > crontab > Interruptable


1 /*
2  This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7  */

8 package org.mmbase.applications.crontab;
9 import java.util.*;
10
11 import org.mmbase.util.logging.*;
12
13 /**
14  * A Runnable wich also has an 'interrupt' method. This only works well
15  * if the job does sleeps (InterruptedException) or check Thread.isInterrupted().
16  * @author Michiel Meeuwissen
17  * @since MMBase-1.8
18  */

19
20 public class Interruptable implements Runnable JavaDoc {
21     private static final Logger log = Logging.getLoggerInstance(Interruptable.class);
22     private Thread JavaDoc runThread = null;
23     private Date startTime;
24     private final Runnable JavaDoc runnable;
25     private final Collection collection;
26     public Interruptable(Runnable JavaDoc run, Collection col) {
27         runnable = run;
28         collection = col;
29     }
30
31     public void run() {
32         if (runThread != null) throw new IllegalStateException JavaDoc();
33         if (collection != null) collection.add(this);
34         runThread = Thread.currentThread();
35         startTime = new Date();
36         try {
37             runnable.run();
38         } catch (Throwable JavaDoc 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 JavaDoc t = runThread;
49         if (t != null) {
50             t.interrupt();
51             return true;
52         }
53         return false;
54     }
55     public boolean isAlive() {
56         Thread JavaDoc t = runThread;
57         return t != null && t.isAlive();
58     }
59     public Date getStartTime() {
60         return startTime;
61     }
62 }
63
Popular Tags