SK.gnome.dwarf.thread
Class ThreadService

java.lang.Object
  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.thread.ThreadService
All Implemented Interfaces:
java.lang.Runnable, Service
Direct Known Subclasses:
Command, GenericLogger, Handler, Listener, LRUCache

public abstract class ThreadService
extends GenericService
implements java.lang.Runnable

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

thread

protected java.lang.Thread thread
The service thread.


daemon

protected boolean daemon
Whether the thread will become a daemon thread.

If set to true, the setDaemon(true) method will be invoked on the thread before it is actually started.

Default value: false

Constructor Detail

ThreadService

public ThreadService(java.lang.String name)
Creates a new ThreadService.

Method Detail

setDaemon

public void setDaemon(boolean enable)
Enables or disables the daemon thread.

Parameters:
enable - true if the thread should become a daemon thread
See Also:
daemon

start

public void start()
           throws ServiceException
Starts the service.

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.

Specified by:
start in interface Service
Overrides:
start in class GenericService
Throws:
ServiceException

stop

public void stop()
          throws ServiceException
Stops the service.

It interrupts the thread via its interrupt() method and returns immediatelly.

Specified by:
stop in interface Service
Overrides:
stop in class GenericService
Throws:
ServiceException

shutdown

public void shutdown()
Shuts down the service.

It interrupts the thread via its interrupt() method and waits for it to finish by calling the finish() method.

Specified by:
shutdown in interface Service
Overrides:
shutdown in class GenericService

run

public void run()
Runs the service.

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.

Specified by:
run in interface java.lang.Runnable

enableThread

protected boolean enableThread()
Whether to start the thread.

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.

Returns:
true if the thread is enabled

finish

protected void finish()
Waits for the thread to 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.


loop

protected void loop()
The operational 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)
     {
     }
   }
 }
 



Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.