se.lth.cs.realtime
Class CyclicThread

java.lang.Object
  |
  +--se.lth.cs.realtime.RTThread
        |
        +--se.lth.cs.realtime.CyclicThread
All Implemented Interfaces:
java.util.EventListener, RTEventListener, java.lang.Runnable
Direct Known Subclasses:
PeriodicThread, SporadicThread

public abstract class CyclicThread
extends RTThread
implements java.lang.Runnable

This class models a real-time thread with cyclic behaviour.

Such a thread loops performing an action and waiting for some time to pass. The action performed and the waiting policy are not specified in this class, which is so declared abstract.

See Also:
PeriodicThread

Fields inherited from class se.lth.cs.realtime.RTThread
defaultEventBufferSize, listenerList, mailbox, MAX_PRIORITY, MIN_PRIORITY
 
Constructor Summary
CyclicThread()
          Allocates a new CyclicThread.
CyclicThread(RTThreadGroup group)
          Allocates a new CyclicThread.
CyclicThread(RTThreadGroup group, java.lang.String name)
          Allocates a new CyclicThread.
CyclicThread(RTThreadGroup group, java.lang.String name, boolean privateQueue)
          Allocates a new CyclicThread.
CyclicThread(java.lang.String name)
          Allocates a new CyclicThread.
 
Method Summary
 boolean isSuspended()
          Checks whether this thread is suspended.
protected  void onResuming()
          Perform any adjustments required when resuming a thread.
protected abstract  void perform()
          Defines the action performed by this thread.
 void resume()
          Asks a suspended thread to resume execution.
 void run()
          Defines the behaviour of a cyclic thread.
 void stop()
          Asks this thread to stop its execution.
 void suspend()
          Asks the thread to suspend its execution.
 java.lang.String toString()
          Returns a string representation of this thread, including the thread's name and priority.
protected abstract  void waitToPerform()
          Defines the waiting policy of this thread.
 
Methods inherited from class se.lth.cs.realtime.RTThread
currentRTThread, dumpStack, getName, getPriority, getRTThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, putEvent, setDaemon, setName, setPriority, sleep, sleep, sleepUntil, start, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CyclicThread

public CyclicThread()
Allocates a new CyclicThread.

Equivalent to CyclicThread(null, null, true).

See Also:
CyclicThread(RTThreadGroup, java.lang.String, boolean)

CyclicThread

public CyclicThread(java.lang.String name)
Allocates a new CyclicThread.

Equivalent to CyclicThread(null, name, true).

Parameters:
name - this thread's name.
See Also:
CyclicThread(RTThreadGroup, java.lang.String, boolean)

CyclicThread

public CyclicThread(RTThreadGroup group)
Allocates a new CyclicThread.

Equivalent to CyclicThread(group, null, true).

Parameters:
group - this thread's RTThreadGroup.
See Also:
CyclicThread(RTThreadGroup, java.lang.String, boolean)

CyclicThread

public CyclicThread(RTThreadGroup group,
                    java.lang.String name)
Allocates a new CyclicThread.

Equivalent to CyclicThread(group, name, true).

Parameters:
group - this thread's RTThreadGroup.
name - this thread's name.
See Also:
CyclicThread(RTThreadGroup, java.lang.String, boolean)

CyclicThread

public CyclicThread(RTThreadGroup group,
                    java.lang.String name,
                    boolean privateQueue)
Allocates a new CyclicThread.

Also sets whether this thread should use a private queue when waiting to be resumed. Using a private queue imposes space overhead, but guarantees better time predictability.

Parameters:
group - this thread's RTThreadGroup.
name - this thread's name.
privateQueue - whether this thread should use a private queue.
See Also:
RTThread.RTThread(RTThreadGroup, java.lang.String)
Method Detail

perform

protected abstract void perform()
Defines the action performed by this thread. A CyclicThread will execute cyclically this function.

waitToPerform

protected abstract void waitToPerform()
                               throws java.lang.InterruptedException
Defines the waiting policy of this thread. A CyclicThread will call this method (possibly more than once) before every call to perform.

The implementor is given the possibility of throwing or passing an InterruptedException, which may cause the acknowledgment of a suspend or a stop.

Throws:
java.lang.InterruptedException - if another thread interrupts this thread.

suspend

public final void suspend()
Asks the thread to suspend its execution. May not have an immediate effect, since a CyclicThread is allowed to suspend its execution only before each call to perform, to prevent deadlocks. It is possible to suspend a thread which has not yet been started. Calling suspend on a suspended thread has no effect.

isSuspended

public boolean isSuspended()
Checks whether this thread is suspended. A thread is considered to be suspended since it acknowledges a suspension request until the method onResuming is called.
Returns:
true if the thread is suspended; false otherwise.
See Also:
suspend()

resume

public final void resume()
Asks a suspended thread to resume execution. If acknowledged, onResuming will be called. It is possible to resume a thread which has not yet been started, to undo a previous call to suspend.. If the thread is not suspended, resume has no effect.
See Also:
suspend()

stop

public void stop()
Asks this thread to stop its execution. May not have an immediate effect, but is irrevocable. Stopping a not yet started thread will cause it to stop as soon as started. Stopping a stopped thread has no effect.

onResuming

protected void onResuming()
Perform any adjustments required when resuming a thread. This method is executed by the resumed thread after a resumption acknowledgement. This default implementation of onResuming does nothing.
See Also:
resume()

run

public final void run()
Defines the behaviour of a cyclic thread. A CyclicThread acts like this:

1. if has been asked to stop, it stops.

2. if has been asked to suspend itself, waits to be resumed and then calls onResuming.

3. calls waitToPerform (if an InterruptedException is caught, it goes to 1).

4. calls perform.

5. goes to 1.

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

toString

public java.lang.String toString()
Returns a string representation of this thread, including the thread's name and priority.
Overrides:
toString in class RTThread
Returns:
a string representation of this thread.