se.lth.cs.realtime
Class PeriodicThread

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

public class PeriodicThread
extends CyclicThread

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

Such a thread loops performing an action once every period milliseconds (if FixedPeriod is implemented, the period can be modified only before the thread is started).

Being a CyclicThread, a PeriodicThread may be suspended and resumed by other threads. By default, the calls to perform which would have been executed during the suspension time are issued after the resumption.


Field Summary
protected  long baseTime
          The absolute time (calculated in ms since midnight, January 1, 1970 UTC) used to compute the time of the next call to perform.
 
Fields inherited from class se.lth.cs.realtime.RTThread
defaultEventBufferSize, listenerList, mailbox, MAX_PRIORITY, MIN_PRIORITY
 
Constructor Summary
PeriodicThread(long period)
          Allocates a new PeriodicThread object.
PeriodicThread(RTThreadGroup group, long period)
          Allocates a new PeriodicThread object.
PeriodicThread(RTThreadGroup group, java.lang.String name, boolean privateQueue, long period)
          Allocates a new PeriodicThread object.
PeriodicThread(RTThreadGroup group, java.lang.String name, long period)
          Allocates a new PeriodicThread object.
PeriodicThread(java.lang.String name, long period)
          Allocates a new PeriodicThread object.
 
Method Summary
 long getPeriod()
          Returns the period of this thread, in milliseconds.
protected  void perform()
          Does nothing.
 void setPeriod(long period)
          Sets the period, in milliseconds, of this thread.
 void start()
          Overrides RTThread.start.
 java.lang.String toString()
          Returns a string representation of this thread, including the thread's name, priority and period in milliseconds.
protected  void waitToPerform()
          Wait until the beginning of the next period.
 
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, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

baseTime

protected long baseTime
The absolute time (calculated in ms since midnight, January 1, 1970 UTC) used to compute the time of the next call to perform.

PLEASE NOTE: in this implementation, this field is given protected visibility, thus allowing easy modifications from subclasses, but also, possibly, from within other threads of execution, which could result in inconsistent values of baseTime, being a long . Hence, care must be taken that baseTime be accessed only by this thread itself (i.e. in onResuming).

Constructor Detail

PeriodicThread

public PeriodicThread(long period)
Allocates a new PeriodicThread object.

Equivalent to PeriodicThread(null, null, true, period).

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

PeriodicThread

public PeriodicThread(java.lang.String name,
                      long period)
Allocates a new PeriodicThread object.

Equivalent to PeriodicThread(null, name, true, period).

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

PeriodicThread

public PeriodicThread(RTThreadGroup group,
                      long period)
Allocates a new PeriodicThread object.

Equivalent to PeriodicThread(group, null, true, period).

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

PeriodicThread

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

Equivalent to PeriodicThread(group, name, true, period).

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

PeriodicThread

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

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

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

start

public void start()
Overrides RTThread.start. Perform the settings needed to ensure a correct periodical behaviour and calls RTThread.start, which in turns causes the call of CyclicThread.run.
Overrides:
start in class RTThread
See Also:
RTThread.start(), CyclicThread.run()

perform

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

waitToPerform

protected void waitToPerform()
                      throws java.lang.InterruptedException
Wait until the beginning of the next period.
Overrides:
waitToPerform in class CyclicThread
Throws:
java.lang.InterruptedException - if another thread has interrupted this one.

getPeriod

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

setPeriod

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

toString

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