Class ThreadServer

  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.GenericServer
          extended bySK.gnome.dwarf.thread.ThreadServer
All Implemented Interfaces:
Reportable, java.lang.Runnable, Server, Service
Direct Known Subclasses:
Console, MultiThreadServer

public abstract class ThreadServer
extends GenericServer
implements java.lang.Runnable

Provides threaded server.

This server starts a new thread to perform the actual operation. It can be used as a common base for all servers 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
Constructor Summary
ThreadServer(java.lang.String name)
          Creates a new ThreadServer.
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.GenericServer
addService, addService, getAuthenticator, getLogger, getParameters, getService, getServices, getServices, init, removeService, report
Methods inherited from class SK.gnome.dwarf.GenericService
getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getName, getPrincipal, getShutdownTimeout, getState, 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
Methods inherited from interface SK.gnome.dwarf.Service
getFullName, getName, getState, log, log, login, logout

Field Detail


protected java.lang.Thread thread
The service thread.


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


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

Method Detail


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

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


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 the this class implements the Runnable interface. The reference to the thread object is stored in the thread field.

It throws an IllegalStateException if the thread field is not null and its isAlive() method returns true.

Specified by:
start in interface Service
start in class GenericServer


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
stop in class GenericServer


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
shutdown in class GenericServer


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


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.

true if the thread is enabled


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() method.


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
   { try
     { wait();
     catch (InterruptedException e)

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