fr.dyade.aaa.util
Class Daemon
- Runnable
public abstract class Daemon
extends java.lang.Object
implements Runnable
The Daemon class represents a basic active component in a server. It
provides usefull code to start and safely stop inner Thread.
Main loop of daemon:
try {
while (running) {
canStop = true;
// Get a notification, then execute the right reaction.
try {
// Get a request
...
} catch (InterruptedException exc) {
continue;
}
canStop = false;
// executes the request
...
}
} finally {
finish();
}
protected boolean | canStop - Boolean variable used to stop the daemon properly.
|
protected ThreadGroup | group - The group of this thread
|
protected Logger | logmon
|
protected int | priority - The
priority that is assigned to the daemon.
|
protected boolean | running - Boolean variable used to stop the daemon properly.
|
protected Thread | thread - The active component of this daemon.
|
Daemon(String name) - Allocates a new Daemon object.
|
Daemon(String name, Logger logmon) - Allocates a new Daemon object.
|
protected void | close() - Releases any resources attached to this daemon.
|
protected void | finish()
|
String | getName() - Returns this
daemon 's name.
|
void | interrupt() - Interrupts this daemon.
|
boolean | isCurrentThread() - Tests if the daemon's thread is the current one.
|
boolean | isRunning() - Tests if this daemon is alive.
|
void | setDaemon(boolean daemon) - Marks the daemon's thread as either a daemon thread a user thread.
|
void | setPriority(int newPriority) - Changes the priority of this daemon.
|
void | setThreadGroup(ThreadGroup group) - Set the thread group to which this daemon's thread belongs.
|
protected void | shutdown() - Interupts a thread that waits for long periods.
|
void | start() - Causes this daemon to begin execution.
|
void | stop() - Forces the daemon to stop executing.
|
String | toString() - Returns a string representation of this daemon.
|
canStop
protected boolean canStop
Boolean variable used to stop the daemon properly. If this variable
is true then the daemon is waiting for a long time and it can interupted,
else it handles a request and it will exit after (it tests the
running
variable between
each reaction)
group
protected ThreadGroup group
The group of this thread
logmon
protected Logger logmon
priority
protected int priority
The priority
that is assigned to the daemon.
running
protected boolean running
Boolean variable used to stop the daemon properly. The daemon tests
this variable between each request, and stops if it is false.
start
, stop
thread
protected Thread thread
The active component of this daemon.
Daemon
protected Daemon(String name)
Allocates a new Daemon object.
name
- the name of the new Daemon
Daemon
protected Daemon(String name,
Logger logmon)
Allocates a new Daemon object.
name
- the name of the new Daemonlogmon
- inherited logging monitor
close
protected void close()
Releases any resources attached to this daemon. Be careful, its method
should be called more than one time.
finish
protected final void finish()
getName
public final String getName()
Returns this daemon
's name.
- this
daemon
's name.
interrupt
public void interrupt()
Interrupts this daemon.
isCurrentThread
public boolean isCurrentThread()
Tests if the daemon's thread is the current one.
isRunning
public boolean isRunning()
Tests if this daemon is alive.
- true if this daemon is alive; false otherwise.
setDaemon
public void setDaemon(boolean daemon)
Marks the daemon's thread as either a daemon thread a user thread.
This method must be called before the daemon is started.
setPriority
public void setPriority(int newPriority)
Changes the priority of this daemon.
If the daemon is running calls the setPriority method on corresponding
thread, else stores value for next start.
Thread.setPriority
setThreadGroup
public void setThreadGroup(ThreadGroup group)
Set the thread group to which this daemon's thread belongs.
This method must be called before the daemon is started.
shutdown
protected void shutdown()
Interupts a thread that waits for long periods. In some cases, we must
use application specific tricks. For example, if a thread is waiting on
a known socket, we have to close the socket to cause the thread to return
immediately. Unfortunately, there really isn't any technique that works
in general.
start
public void start()
Causes this daemon to begin execution. A new thread is created to
execute the run method.
stop
public void stop()
Forces the daemon to stop executing. This method notifies thread that
it should stop running, if the thread is waiting it is first interupted
then the shutdown method is called to close all ressources.
toString
public String toString()
Returns a string representation of this daemon.
- A string representation of this daemon.
Copyright B) 2004 Scalagent - All rights reserved