se.lth.cs.realtime
Class SporadicThread

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

public class SporadicThread
extends CyclicThread

This class models a real-time thread that executes the same function every time it is triggered.

The execution frequency must be upper-bound specifying a shortest period for this thread(if FixedPeriod is implemented, the shortest period can be modified only before this thread is started).

Being a CyclicThread, a SporadicThread may be suspended and resumed by other threads. By default, the "triggering events" that occurre during the suspension time are served after the resumption.


Field Summary
protected  int pendingTriggers
          Checked in waitToPerform to let this thread stop waiting.
 
Fields inherited from class se.lth.cs.realtime.RTThread
defaultEventBufferSize, listenerList, mailbox, MAX_PRIORITY, MIN_PRIORITY
 
Constructor Summary
SporadicThread(long shortestPeriod)
          Allocates a new SporadicThread object.
SporadicThread(RTThreadGroup group, long shortestPeriod)
          Allocates a new SporadicThread object.
SporadicThread(RTThreadGroup group, java.lang.String name, boolean privateQueue, long shortestPeriod)
          Allocates a new SporadicThread object.
SporadicThread(RTThreadGroup group, java.lang.String name, long shortestPeriod)
          Allocates a new SporadicThread object.
SporadicThread(java.lang.String name, long shortestPeriod)
          Allocates a new SporadicThread object.
 
Method Summary
 long getShortestPeriod()
          Returns the shortest period of this thread, in milliseconds.
protected  void perform()
          Does nothing.
 void setShortestPeriod(long shortestPeriod)
          Sets the shortest period of this thread, in milliseconds.
 java.lang.String toString()
          Returns a string representation of this thread, including the thread's name, priority and shortest period in milliseconds.
 void trigger()
          Causes the execution of the perform method.
protected  void waitToPerform()
          Lets this thread wait for a call to the function trigger.
 
Methods inherited from class se.lth.cs.realtime.CyclicThread
isSuspended, onResuming, resume, run, stop, suspend
 
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
 

Field Detail

pendingTriggers

protected volatile int pendingTriggers
Checked in waitToPerform to let this thread stop waiting. Note that its value is decremented when returning from waitToPerform.
Constructor Detail

SporadicThread

public SporadicThread(long shortestPeriod)
Allocates a new SporadicThread object.

Equivalent to SporadicThread(null, null, true, shortestPeriod).

Parameters:
shortestPeriod - the shortest period of the new thread, in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the shortest period is not positive.
See Also:
SporadicThread(RTThreadGroup, java.lang.String, boolean, long)

SporadicThread

public SporadicThread(java.lang.String name,
                      long shortestPeriod)
Allocates a new SporadicThread object.

Equivalent to SporadicThread(null, name, true, shortestPeriod).

Parameters:
name - the name of the new thread.
shortestPeriod - the shortest period of the new thread, in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the shortest period is not positive.
See Also:
SporadicThread(RTThreadGroup, java.lang.String, boolean, long)

SporadicThread

public SporadicThread(RTThreadGroup group,
                      long shortestPeriod)
Allocates a new SporadicThread object.

Equivalent to SporadicThread(group, null, true, shortestPeriod).

Parameters:
group - this thread's RTThreadGroup..
shortestPeriod - the shortest period of the new thread, in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the shortest period is not positive.
See Also:
SporadicThread(RTThreadGroup, java.lang.String, boolean, long)

SporadicThread

public SporadicThread(RTThreadGroup group,
                      java.lang.String name,
                      long shortestPeriod)
Allocates a new SporadicThread object.

Equivalent to SporadicThread(group, name, true, shortestPeriod).

Parameters:
name - the name of the new thread.
group - this thread's RTThreadGroup..
shortestPeriod - the shortest period of the new thread, in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the shortest period is not positive.
See Also:
SporadicThread(RTThreadGroup, java.lang.String, boolean, long)

SporadicThread

public SporadicThread(RTThreadGroup group,
                      java.lang.String name,
                      boolean privateQueue,
                      long shortestPeriod)
Allocates a new SporadicThread object.

It is possible to specify whether this thread should use a private object to wait on, when suspended or waiting to be triggered.

Parameters:
name - the name of the new thread.
group - this thread's RTThreadGroup..
privateQueue - whether this thread should use a private queue.
shortestPeriod - the shortest period of the new thread, in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the shortest period is not positive.
See Also:
CyclicThread.CyclicThread(RTThreadGroup, java.lang.String, boolean)
Method Detail

trigger

public void trigger()
             throws FrequencyExcessException
Causes the execution of the perform method. If called before shortestPeriod milliseconds since the last call, an exception is thrown.
Throws:
FrequencyExcessException - if called too often.
IllegalThreadStateException - if the thread has not been started.

perform

protected void perform()
Does nothing. This empty implementation allows the use of a SporadicThread as a TriggeredEvent generator only.
Overrides:
perform in class CyclicThread
See Also:
event.TriggeredEvent

waitToPerform

protected void waitToPerform()
                      throws java.lang.InterruptedException
Lets this thread wait for a call to the function trigger.
Overrides:
waitToPerform in class CyclicThread
Throws:
java.lang.InterruptedException - if another thread has interrupted this one.

getShortestPeriod

public long getShortestPeriod()
Returns the shortest period of this thread, in milliseconds.
Returns:
the shortest period in ms.

setShortestPeriod

public void setShortestPeriod(long shortestPeriod)
                       throws FixedPeriodException
Sets the shortest period of this thread, in milliseconds. If this thread implements FixedPeriodand is alive , an exception is thrown.
Parameters:
shortestPeriod - the new shortest period of this thread.
Throws:
java.lang.IllegalArgumentException - if the argument is not positive.
FixedPeriodException - if the thread is alive and implements FixedPeriod.

toString

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