org.almendra.commons.threads
Class ActiveSchedulingObject

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

public abstract class ActiveSchedulingObject
extends ActiveObject

Defines an active object that consumes items provided by some producer object and processes them as long as the list of items to be processed is not empty. If it becomes empty the ActiveSchedulingObject falls asleep till a new item has been added to the list.

Author:
Oliver Plohmann

Field Summary
protected  EDU.oswego.cs.dl.util.concurrent.Semaphore available
          Lock used to synchronize access to the queue.
protected  boolean executing
          Answer whether is currently executing an object.
protected  IQueue queue
          Queue that hold the items to be processed.
 
Fields inherited from class org.almendra.commons.threads.ActiveObject
activeLock, isActive, name, priority, thread, verbose, waiting
 
Constructor Summary
protected ActiveSchedulingObject()
          Creates a new ActiveSchedulingObject object.
  ActiveSchedulingObject(IQueue queue)
          Creates a new ActiveSchedulingObject object.
  ActiveSchedulingObject(IQueue queue, boolean verbose)
          Creates a new ActiveSchedulingObject object.
  ActiveSchedulingObject(IQueue queue, boolean verbose, int priority)
          Creates a new ActiveSchedulingObject object.
 
Method Summary
 void add(IExecutable executable)
          Adds another item to the queue of items to be processed.
 long available()
           
protected  void doActivity()
          Constantly called by the superclass as long as the active object is active.
protected  void executableAdded(IExecutable executable)
          Called whentever a new item has been added to the queue.
protected  void executableRemoved(IExecutable executable)
           
protected abstract  void execute(IExecutable executable)
          Called to process the next item from the queue.
protected  void executePrimary(IExecutable executable)
          Called to process the next item from the queue.
protected  void finishedExecution(IExecutable executable)
           
protected  IExecutable getNext()
          Return the next item of the queue to be processed.
protected  IExecutable getNextPrim()
           
 long getQueueSize()
           
 boolean hasNext()
           
 boolean isIdle()
           
protected  void messageResuming()
           
protected  void messageSuspending()
           
 java.lang.Object remove(IExecutable executable)
          Remove an item from the queue.
protected  void resuming()
           
protected  void startingExecution(IExecutable executable)
           
protected  void suspending()
           
 
Methods inherited from class org.almendra.commons.threads.ActiveObject
getName, getPriority, isActive, isDaemon, isVerbose, messageStarting, messageStopping, run, setActive, setName, setPriority, setVerbose, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queue

protected IQueue queue
Queue that hold the items to be processed.


available

protected EDU.oswego.cs.dl.util.concurrent.Semaphore available
Lock used to synchronize access to the queue.


executing

protected volatile boolean executing
Answer whether is currently executing an object.

Constructor Detail

ActiveSchedulingObject

protected ActiveSchedulingObject()
Creates a new ActiveSchedulingObject object. Declared protected to enforce the use of the public constructors by objects that are not subclasses. The public constructors guarantee propper initialization with default values. Subclasses that want to set other user-defined values need to make sure themselves that the object is initialized propperly.


ActiveSchedulingObject

public ActiveSchedulingObject(IQueue queue)
Creates a new ActiveSchedulingObject object.

Parameters:
queue - the queue that holds items to be processed

ActiveSchedulingObject

public ActiveSchedulingObject(IQueue queue,
                              boolean verbose)
Creates a new ActiveSchedulingObject object.

Parameters:
queue - the queue that holds items to be processed
verbose - defines wether trace information is written to the console

ActiveSchedulingObject

public ActiveSchedulingObject(IQueue queue,
                              boolean verbose,
                              int priority)
Creates a new ActiveSchedulingObject object.

Parameters:
queue - the queue that holds items to be processed
verbose - defines wether trace information is written to the console
priority - the priority of the thread the active will run in
Method Detail

doActivity

protected void doActivity()
Constantly called by the superclass as long as the active object is active. For class ActiveSchedulingObject items to be processed are constantly picked from queue as long as this queue is not empty. Otherwise the active object falls asleep and wakes up when a new item has been added.

Specified by:
doActivity in class ActiveObject
See Also:
org.almendra.commons.cas.scheduling.ActiveObject#doActivity()

execute

protected abstract void execute(IExecutable executable)
Called to process the next item from the queue. Must be implemented in subclasses as appropriate.

Parameters:
next -

executePrimary

protected void executePrimary(IExecutable executable)
Called to process the next item from the queue.

Parameters:
next - the next item from the list to be processed

finishedExecution

protected void finishedExecution(IExecutable executable)
Parameters:
next -

startingExecution

protected void startingExecution(IExecutable executable)
Parameters:
next -

add

public void add(IExecutable executable)
Adds another item to the queue of items to be processed. Calls objectAdded() to notify instances of subclasses that an item has been added to the item list.

Parameters:
object -

executableAdded

protected void executableAdded(IExecutable executable)
Called whentever a new item has been added to the queue. May be redefined in subclasses as appropriate.


remove

public java.lang.Object remove(IExecutable executable)
Remove an item from the queue.

Parameters:
object - the object to be removed
Returns:
returns the removed object or null if not found

executableRemoved

protected void executableRemoved(IExecutable executable)

getNext

protected IExecutable getNext()
Return the next item of the queue to be processed. Blocks if queue is empty.

Returns:
returns next item in queue or null but only if the active object was stopped

resuming

protected void resuming()

messageResuming

protected void messageResuming()

suspending

protected void suspending()

messageSuspending

protected void messageSuspending()

hasNext

public boolean hasNext()

available

public long available()

getNextPrim

protected IExecutable getNextPrim()

isIdle

public boolean isIdle()

getQueueSize

public long getQueueSize()
Returns: