Package org.simpleframework.util.thread
Class Daemon
- java.lang.Object
-
- org.simpleframework.util.thread.Daemon
-
- All Implemented Interfaces:
java.lang.Runnable
public abstract class Daemon extends java.lang.Object implements java.lang.RunnableTheDaemonobject provides a named daemon thread which will execute therunmethod when started. This offers some convenience in that it hides the normal thread methods and also allows the object extending this to provide the name of the internal thread, which is given an incrementing sequence number appended to the name provided.- Author:
- Niall Gallagher
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDaemon()Constructor for theDaemonobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetName()This is used to acquire the name of the thread.voidinterrupt()This is used to interrupt the internal thread.booleanisStarted()This is used to determine if the daemon has already started.voidjoin()This is used to join with the internal thread of this daemon.voidstart()This is used to start the internal thread.
-
-
-
Constructor Detail
-
Daemon
protected Daemon()
Constructor for theDaemonobject. This will create the internal thread and ensure it is a daemon. When it is started the name of the internal thread is set using the name of the instance as taken fromgetName. If the name provided is null then no name is set for the thread.
-
-
Method Detail
-
start
public void start()
This is used to start the internal thread. Once started the internal thread will execute therunmethod of this instance. Aside from starting the thread this will also ensure the internal thread has a unique name.
-
isStarted
public boolean isStarted()
This is used to determine if the daemon has already started. Once started it can not be started again. This ensures that when dead it remains dead. The contract of this method is that if thestartmethod is invoked at any point this method will always return true.- Returns:
- true if the daemon has already been started
-
interrupt
public void interrupt()
This is used to interrupt the internal thread. This is used when there is a need to wake the thread from a sleeping or waiting state so that some other operation can be performed. Typically this is required when killing the thread.
-
join
public void join() throws java.lang.InterruptedExceptionThis is used to join with the internal thread of this daemon. Rather than exposing the internal thread ajoinmethod is provided. This allows asynchronous threads to wait for the daemon to complete simulating synchronous action.- Throws:
java.lang.InterruptedException- if the thread is interrupted
-
getName
public java.lang.String getName()
This is used to acquire the name of the thread. This will be overridden by instances that wish to provide a descriptive name for the thread. If this is not overridden then the name of the thread is the simple name of the implementation.- Returns:
- the name of the internal thread executed
-
-