edu.cs.ai.kreator.control
Class WorkerController

java.lang.Object
  extended by edu.cs.ai.kreator.control.WorkerController
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.util.EventListener

public class WorkerController
extends java.lang.Object
implements java.beans.PropertyChangeListener

This controller manages a pool of running threads. You can add KreatorWorkers to this pool, which will then be executed. Each worker can publish status information, which the WorkerController displays in the GUI.

Also this controller makes it possible for every thread to get access to it's correspondent KreatorWorker. This enables us to display status information from every part of KReator, without passing ViewStatus to every function in a thread-safe way.

Author:
Niklas Wulf

Field Summary
static org.apache.log4j.Logger LOG
           
 
Constructor Summary
WorkerController()
          Initializes the WorkerController.
 
Method Summary
 void abortWorker(KreatorWorker<?> w)
          Tells the given worker to abort.
 void addAndExecuteWorker(KreatorWorker<?> worker)
          Adds a KReatorWorker to the pool and executes it, as soon as there is a free thread.
 void cancelWorkerAbortion(KreatorWorker<?> w)
          After having told the worker to abort, one may use this function to revoke this plead.
 KreatorConsole getMyConsole()
          Provided for convenience.
 KreatorWorker<?> getMyWorker()
          Returns the worker, which belongs to the current thread, or null if the current thread does not run in a background worker.
 void pauseWorker(KreatorWorker<?> w)
          Tells the given worker to sleep until resumed.
 void propertyChange(java.beans.PropertyChangeEvent e)
          Listens for property changes of registered workers.
protected  void registerWorker(KreatorWorker<?> worker)
          Connects the worker with it's thread.
 void resumeWorker(KreatorWorker<?> w)
          Resumes this worker.
 boolean shutDown()
           
 void threadWaitOrAbort()
          If the current thread has been told to wait, this method waits until it is resumed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Logger LOG
Constructor Detail

WorkerController

public WorkerController()
Initializes the WorkerController.

Method Detail

shutDown

public boolean shutDown()

addAndExecuteWorker

public void addAndExecuteWorker(KreatorWorker<?> worker)
Adds a KReatorWorker to the pool and executes it, as soon as there is a free thread.

Parameters:
worker -

registerWorker

protected void registerWorker(KreatorWorker<?> worker)
Connects the worker with it's thread. This function is only called from KreatorWorker.doInBackground(), which every worker has to call.

Parameters:
w -
See Also:
KreatorWorker.doInBackground(), getMyWorker()

getMyWorker

public KreatorWorker<?> getMyWorker()
Returns the worker, which belongs to the current thread, or null if the current thread does not run in a background worker. You can use this worker to print information in the ViewStatus, which is connected to it. With this functionality there is no need to pass ViewStatus to every method.

Returns:
the worker, to which the current thread belongs or null if the current thread does not run in a background worker

getMyConsole

public KreatorConsole getMyConsole()
Provided for convenience. Returns the console belonging to the current thread's worker. If the current thread runs in no worker, or no console is connected to the worker null is returned. Use instead of ...getMyWorker().getConsole();

Returns:
the console belonging to the current thread's worker or null if there is no console

threadWaitOrAbort

public void threadWaitOrAbort()
                       throws AbortedExecutionException
If the current thread has been told to wait, this method waits until it is resumed.
If the thread has been told to abort, this method throws an AbortedExecutionException.
Does nothing, if the thread has not been paused or aborted.

Throws:
AbortedExecutionException - if the thread has been told to abort.

pauseWorker

public void pauseWorker(KreatorWorker<?> w)
Tells the given worker to sleep until resumed.

Parameters:
w -

resumeWorker

public void resumeWorker(KreatorWorker<?> w)
Resumes this worker. Does nothing if the thread is not sleeping.

Parameters:
w -

abortWorker

public void abortWorker(KreatorWorker<?> w)
Tells the given worker to abort.


cancelWorkerAbortion

public void cancelWorkerAbortion(KreatorWorker<?> w)
After having told the worker to abort, one may use this function to revoke this plead. This is useful if you have reacted to the AbortedExecutionException from inside the working method and want to continue working.

See Also:
WorkerController#cancleWorkerAbortion(KreatorWorker)

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent e)
Listens for property changes of registered workers.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener