KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > util > DaemonThread


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

10 package org.mmbase.core.util;
11
12 import org.mmbase.module.core.MMBaseContext;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * Defines a daemon thread that runs in the threadgroup belonging to this MMBase context.
17  * @since MMBase-1.8
18  * @author Pierre van Rooden
19  * @version $Id: DaemonThread.java,v 1.2 2005/12/10 11:45:02 michiel Exp $
20  */

21 public class DaemonThread extends Thread JavaDoc implements DaemonTask {
22
23     /**
24      * Default sleep period for a daemon thread (one minute).
25      */

26     public static final int DEFAULT_SLEEP_PERIOD = 60000;
27
28     private static final Logger log = Logging.getLoggerInstance(DaemonThread.class);
29
30     /**
31      * The threads sleep period.
32      * This period is used when a Daemonthread runs on its own (that is, without an assigned task)
33      * When a DaemonThread is assigned a task, it uses the sleep period of that task.
34      */

35     protected int sleepPeriod = DEFAULT_SLEEP_PERIOD;
36
37     private Runnable JavaDoc target = null;
38     private DaemonTask task = null;
39     private boolean running = false;
40
41     /**
42      * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
43      */

44     public DaemonThread() {
45         this((Runnable JavaDoc)null, (String JavaDoc)null);
46     }
47
48     /**
49      * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
50      * @param name the name of the thread
51      */

52     public DaemonThread(String JavaDoc name) {
53         this((Runnable JavaDoc)null, name);
54     }
55
56     /**
57      * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
58      * @param target the target thread
59      * @param name the name of the thread
60      */

61     public DaemonThread(Runnable JavaDoc target, String JavaDoc name) {
62         super(MMBaseContext.getThreadGroup(), target, name);
63         this.target = target;
64         setDaemon(true);
65     }
66
67     /**
68      * Sets the task this thread should run when started.
69      * @param task the task to run
70      */

71     public void setTask(DaemonTask task) {
72         this.task = task;
73     }
74
75     /**
76      * Returns the task this thread runs when started.
77      */

78     public DaemonTask getTask() {
79         return task;
80     }
81
82     public int getSleepPeriod() {
83         if (task != null) {
84             return task.getSleepPeriod();
85         } else {
86             return sleepPeriod;
87         }
88     }
89
90     public void start() {
91         running = true;
92         log.service("Starting " + getName());
93         super.start();
94     }
95
96     public void interrupt() {
97         running = false;
98         super.interrupt();
99     }
100
101     public boolean isRunning() {
102         return running;
103     }
104
105     public void executeTask() {
106         if (task != null) {
107             task.executeTask();
108         } else {
109             throw new UnsupportedOperationException JavaDoc("No execute task defined");
110         }
111     }
112
113     /**
114      * Default behavior (when no target is specified) is to run continuously until interrupted.
115      */

116     public void run() {
117         if (target != null) {
118             target.run();
119         } else {
120             while (isRunning()) {
121                 try {
122                     Thread.sleep(getSleepPeriod());
123                     executeTask();
124                 } catch (InterruptedException JavaDoc e){
125                     log.debug(Thread.currentThread().getName() +" was interrupted.");
126                 }
127             }
128         }
129     }
130
131 }
132
133
Popular Tags