|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object SK.gnome.dwarf.GenericService SK.gnome.dwarf.thread.ThreadService
Provides threaded service.
This service starts a new thread to perform the actual operation. It can be used as
a common base for all services which needs to start a separate thread for any reason.
The protected loop()
method must be implemented in order to create an useful
threaded service, as well as the enableThread()
method, which actually enables
the creation of a new thread.
Field Summary | |
protected boolean |
daemon
Whether the thread will become a daemon thread. |
protected java.lang.Thread |
thread
The service thread. |
Fields inherited from class SK.gnome.dwarf.GenericService |
initParameters, parent |
Fields inherited from interface SK.gnome.dwarf.Service |
INITIALIZED, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_TRACE, LOG_WARN, LOG_XFER, SHUTDOWN, STARTED, STOPPED |
Constructor Summary | |
ThreadService(java.lang.String name)
Creates a new ThreadService. |
Method Summary | |
protected boolean |
enableThread()
Whether to start the thread. |
protected void |
finish()
Waits for the thread to finish. |
protected void |
loop()
The operational loop. |
void |
run()
Runs the service. |
void |
setDaemon(boolean enable)
Enables or disables the daemon thread. |
void |
shutdown()
Shuts down the service. |
void |
start()
Starts the service. |
void |
stop()
Stops the service. |
Methods inherited from class SK.gnome.dwarf.GenericService |
getAuthenticator, getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getLogger, getName, getParameters, getPrincipal, getShutdownTimeout, getState, init, log, log, login, logout, setAuthFacility, setInitParameters, setLogFacility, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected java.lang.Thread thread
protected boolean daemon
If set to true, the setDaemon(true) method will be invoked on the thread before it is actually started.
Default value: false
Constructor Detail |
public ThreadService(java.lang.String name)
Method Detail |
public void setDaemon(boolean enable)
enable
- true if the thread should become a daemon threaddaemon
public void start() throws ServiceException
If the enableThread()
method returns true, it creates and starts a new
thread. The thread's name corresponds to the service name and the thread's Runnable
target is the service itself since it implements the Runnable interface. The reference
to the thread object is stored in the thread
field.
It throws a IllegalStateException if the thread
field is not null and
its isAlive() method returns true.
start
in interface Service
start
in class GenericService
ServiceException
public void stop() throws ServiceException
It interrupts the thread
via its interrupt() method and returns
immediatelly.
stop
in interface Service
stop
in class GenericService
ServiceException
public void shutdown()
It interrupts the thread
via its interrupt() method and waits for
it to finish by calling the finish()
method.
shutdown
in interface Service
shutdown
in class GenericService
public void run()
This method calls the protected loop()
method in an infinite cycle until either the
stop()
or the shutdown()
method is invoked. This method catches all errors
and exceptions, therefore the only way how to end the thread cycle is to call one of those
state transition methods.
run
in interface java.lang.Runnable
protected boolean enableThread()
Enables or disables starting of the separate thread. This method returns always false. It should be overriden in the subclasses to enable the starting of the new thread.
protected void finish()
This method is called by the shutdown()
method before returning from it.
It should block the current executing thread until the thread
dies.
It actually joins the thread
via its join(timeout) method with
the timeout argument set to the value returned from the GenericService.getShutdownTimeout()
mmethod.
protected void loop()
This is the place where the actual operation of the service thread is performed. This
method is called from the run()
method in an infinite cycle until the service is
stopped or shut down. It must be overridden to implement an useful threaded service.
The actual implementation suspends the thread by calling the wait() method.
Sample implementation:
public void loop() { // do the right stuff ... // wait until someone notifies the thread that the right stuff must be done again synchronized(this) { try { wait(); } catch (InterruptedException e) { } } }
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |