org.almendra.commons.threads
Class ActiveObject

java.lang.Object
  extended by org.almendra.commons.threads.ActiveObject
All Implemented Interfaces:
java.lang.Runnable, INamedObject, ISimpleActiveObject
Direct Known Subclasses:
ActiveSchedulingObject

public abstract class ActiveObject
extends java.lang.Object
implements java.lang.Runnable, ISimpleActiveObject, INamedObject

Abstract active object that constantly calls doActivity() as long as it is active. An active object is an object that runs in its own thread. Defines several auxiliary methods to be used by concrete subclasses.

Author:
Oliver Plohmann

Field Summary
protected  java.lang.Object activeLock
          Lock object used to synchronize access the flag isActive.
protected  boolean isActive
          Holds information whether active object is active or not (has been started or has been stopped).
protected  java.lang.String name
          Name of the active object.
protected  int priority
          Defines the priority the active object should have when started.
protected  java.lang.Thread thread
          Thread the active object runs in.
protected  boolean verbose
          Specifies whether the active object should write trace information to the console.
protected  boolean waiting
          Flag that must be set to true when active object is blocked when it is waiting for input.
 
Constructor Summary
ActiveObject()
          Creates a new ActiveObject object.
ActiveObject(boolean verbose)
          Creates a new ActiveObject object.
ActiveObject(boolean verbose, int priority)
          Creates a new ActiveObject object.
 
Method Summary
protected abstract  void doActivity()
          Abstract method that is constantly called as long as active object is active.
 java.lang.String getName()
          Answer the user-defined name of the active object.
 int getPriority()
           
 boolean isActive()
          Answer whether the active object has been started and is active or not.
protected abstract  boolean isDaemon()
          Implement in subclass whether thread the active object will run in when started is a daemon thread or not.
protected  boolean isVerbose()
          Answer whether active object is in verbose mode writing trace information to the console.
protected  void messageStarting()
           
protected  void messageStopping()
           
 void run()
          Constantly calls doActivity as long as active object is active.
protected  void setActive(boolean isActive)
          Mark the active object as active or not as indicated by isActive.
 void setName(java.lang.String name)
          Set user-defined name of the active object.
 void setPriority(int priority)
          Set priority of active object.
 void setVerbose(boolean verbose)
          Set the active object to verbose mode which will cause trace information to be written to the console.
 void start()
          Start the active object.
 void stop()
          Stop the active object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected java.lang.String name
Name of the active object. Optionally can be set to identify active objects when tracing their activities.


verbose

protected boolean verbose
Specifies whether the active object should write trace information to the console.


thread

protected transient java.lang.Thread thread
Thread the active object runs in.


isActive

protected boolean isActive
Holds information whether active object is active or not (has been started or has been stopped).


activeLock

protected java.lang.Object activeLock
Lock object used to synchronize access the flag isActive.


priority

protected int priority
Defines the priority the active object should have when started.


waiting

protected boolean waiting
Flag that must be set to true when active object is blocked when it is waiting for input.

Constructor Detail

ActiveObject

public ActiveObject()
Creates a new ActiveObject object.


ActiveObject

public ActiveObject(boolean verbose)
Creates a new ActiveObject object.

Parameters:
verbose - define whether trace information should be written to the console

ActiveObject

public ActiveObject(boolean verbose,
                    int priority)
Creates a new ActiveObject object.

Parameters:
verbose - define whether trace information should be written to the console
priority - define priority of thread when active object is started.
Method Detail

run

public void run()
Constantly calls doActivity as long as active object is active.

Specified by:
run in interface java.lang.Runnable
See Also:
Runnable.run()

doActivity

protected abstract void doActivity()
Abstract method that is constantly called as long as active object is active. Must be implemented by every concrete subclass, for example to fetch items from an input list and process them as long as the list contains items.


getName

public java.lang.String getName()
Answer the user-defined name of the active object.

Specified by:
getName in interface INamedObject
Returns:
name
See Also:
org.almendra.commons.cas.INamedObject#getName()

setName

public void setName(java.lang.String name)
Set user-defined name of the active object. Displayed when trace informnation is written to the console.

Parameters:
name -

getPriority

public int getPriority()
Returns:
priority of the thread the active object is running in

setPriority

public void setPriority(int priority)
Set priority of active object. The active object will run in a thread of the specified object when started or its priority will be changed accordingly when already started.

Parameters:
priority -

isActive

public boolean isActive()
Answer whether the active object has been started and is active or not.

Specified by:
isActive in interface ISimpleActiveObject
Returns:

start

public void start()
Start the active object. Ignored if already started.

Specified by:
start in interface ISimpleActiveObject

messageStarting

protected void messageStarting()

stop

public void stop()
Stop the active object. Ignored if never started or already stopped.

Specified by:
stop in interface ISimpleActiveObject

messageStopping

protected void messageStopping()

setActive

protected void setActive(boolean isActive)
Mark the active object as active or not as indicated by isActive.

Parameters:
isActive -

isDaemon

protected abstract boolean isDaemon()
Implement in subclass whether thread the active object will run in when started is a daemon thread or not. A daemon thread only lives to server user threads.

Returns:

isVerbose

protected boolean isVerbose()
Answer whether active object is in verbose mode writing trace information to the console.

Returns:

setVerbose

public void setVerbose(boolean verbose)
Set the active object to verbose mode which will cause trace information to be written to the console.

Parameters:
verbose -