001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.core.util;
011:
012: import org.mmbase.module.core.MMBaseContext;
013: import org.mmbase.util.logging.*;
014:
015: /**
016: * Defines a daemon thread that runs in the threadgroup belonging to this MMBase context.
017: * @since MMBase-1.8
018: * @author Pierre van Rooden
019: * @version $Id: DaemonThread.java,v 1.2 2005/12/10 11:45:02 michiel Exp $
020: */
021: public class DaemonThread extends Thread implements DaemonTask {
022:
023: /**
024: * Default sleep period for a daemon thread (one minute).
025: */
026: public static final int DEFAULT_SLEEP_PERIOD = 60000;
027:
028: private static final Logger log = Logging
029: .getLoggerInstance(DaemonThread.class);
030:
031: /**
032: * The threads sleep period.
033: * This period is used when a Daemonthread runs on its own (that is, without an assigned task)
034: * When a DaemonThread is assigned a task, it uses the sleep period of that task.
035: */
036: protected int sleepPeriod = DEFAULT_SLEEP_PERIOD;
037:
038: private Runnable target = null;
039: private DaemonTask task = null;
040: private boolean running = false;
041:
042: /**
043: * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
044: */
045: public DaemonThread() {
046: this ((Runnable) null, (String) null);
047: }
048:
049: /**
050: * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
051: * @param name the name of the thread
052: */
053: public DaemonThread(String name) {
054: this ((Runnable) null, name);
055: }
056:
057: /**
058: * Create a MMBase daemon thread (associated with this MMBase's threadgroup).
059: * @param target the target thread
060: * @param name the name of the thread
061: */
062: public DaemonThread(Runnable target, String name) {
063: super (MMBaseContext.getThreadGroup(), target, name);
064: this .target = target;
065: setDaemon(true);
066: }
067:
068: /**
069: * Sets the task this thread should run when started.
070: * @param task the task to run
071: */
072: public void setTask(DaemonTask task) {
073: this .task = task;
074: }
075:
076: /**
077: * Returns the task this thread runs when started.
078: */
079: public DaemonTask getTask() {
080: return task;
081: }
082:
083: public int getSleepPeriod() {
084: if (task != null) {
085: return task.getSleepPeriod();
086: } else {
087: return sleepPeriod;
088: }
089: }
090:
091: public void start() {
092: running = true;
093: log.service("Starting " + getName());
094: super .start();
095: }
096:
097: public void interrupt() {
098: running = false;
099: super .interrupt();
100: }
101:
102: public boolean isRunning() {
103: return running;
104: }
105:
106: public void executeTask() {
107: if (task != null) {
108: task.executeTask();
109: } else {
110: throw new UnsupportedOperationException(
111: "No execute task defined");
112: }
113: }
114:
115: /**
116: * Default behavior (when no target is specified) is to run continuously until interrupted.
117: */
118: public void run() {
119: if (target != null) {
120: target.run();
121: } else {
122: while (isRunning()) {
123: try {
124: Thread.sleep(getSleepPeriod());
125: executeTask();
126: } catch (InterruptedException e) {
127: log.debug(Thread.currentThread().getName()
128: + " was interrupted.");
129: }
130: }
131: }
132: }
133:
134: }
|