se.lth.cs.realtime
Class JThread

java.lang.Object
  extended byjava.lang.Thread
      extended byse.lth.cs.realtime.JThread
All Implemented Interfaces:
Activity, java.util.EventListener, RTEventListener, RTEventSource, java.lang.Runnable

public class JThread
extends java.lang.Thread
implements Activity, RTEventListener, RTEventSource

Thread of concurrent execution according to the Java platform, but extended with the facilities provided by the RTThread class. Using the JThread class, the event processing facilities provided by the RTThread is made available in a subclass of java.lang.Thread (which the RTThread is not). Another supported feature is the method sleepUntil. Preferably, threads are based on the RTThread which will run well on any Java platform and enable real-time execution on a real-time (Java) platform. However, for compatibility with existing software (requiring java.lang.Threads) in cases when real-time execution is known not to be needed, sub-classing JThread is useful. Thus, use RTThread for real-time threads and use JThread for concurrent (Java) threads.

See Also:
License, Thread, RTThread

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
JThread()
          Allocates a new JThread object with a newly generated name in its father's ThreadGroup.
JThread(java.lang.String name)
          Allocates a new JThread object with the specified name in its parent's ThreadGroup.
JThread(java.lang.ThreadGroup group)
          Allocates a new JThread object with a newly generated name in the specified ThreadGroup.
JThread(java.lang.ThreadGroup group, java.lang.String name)
          Allocates a new JThread object with the specified name in the specified ThreadGroup.
 
Method Summary
 void addListener(RTEventListener subscriber)
          Register subscriber as listener of application events emitted from this thread object.
 RTEventDispatcher getDispatcher()
          Obtain the object that internally stores the subscriptions of events, often referred to as the listener list.
 Environment getEnv()
          Environments not implemented yet.
 Timebase getTimebase()
          Time bases not implemented yet.
 RTEvent putEvent(RTEvent ev)
          Put the time-stamped event in the input buffer of this thread.
 void run()
          Default non-empty run method, first calling init, then calling perform while not interrupted, and finally calling exit.
static void sleep(long millis)
          Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
static void sleep(long millis, int nanos)
          Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.
static void sleepUntil(long wakeUpTime)
          Causes the currently executing thread to sleep (temporarily cease execution) until the specified moment in time.
 void terminate()
          Interrupt and join.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface se.lth.cs.realtime.Activity
isAlive, join, start
 

Constructor Detail

JThread

public JThread()
Allocates a new JThread object with a newly generated name in its father's ThreadGroup. Automatically generated names are of the form "Thread-"+n, where n is an integer.


JThread

public JThread(java.lang.String name)
Allocates a new JThread object with the specified name in its parent's ThreadGroup.

Parameters:
name - the name of the new thread.

JThread

public JThread(java.lang.ThreadGroup group)
Allocates a new JThread object with a newly generated name in the specified ThreadGroup.

Automatically generated names are of the form "Thread-"+n, where n is an integer.

Parameters:
group - the ThreadGroup of the new thread.

JThread

public JThread(java.lang.ThreadGroup group,
               java.lang.String name)
Allocates a new JThread object with the specified name in the specified ThreadGroup.

Parameters:
name - the name of the new thread.
group - the ThreadGroup of the new thread.
Method Detail

putEvent

public RTEvent putEvent(RTEvent ev)
Put the time-stamped event in the input buffer of this thread. If the buffer is full, the caller is blocked.

Specified by:
putEvent in interface RTEventListener
Parameters:
ev - Event to process or buffer for processing
Returns:
null, but subclasses are free to return an event if desired to inform about blocking or results.

getDispatcher

public RTEventDispatcher getDispatcher()
Obtain the object that internally stores the subscriptions of events, often referred to as the listener list. Opposed to listener lists in GUI packages (such as AWT and Swing), where the listener typically gets its processing method called and fully performed in the context of the GUI thread, the subscribers/listeners of RTEvents gets a call of putEvent which typically only stores/buffers the event for later processing by the receiving thread (object). Thus, the EventSource simply dispatches the event distribution, hence the name dispatcher of the lister list storage object.

Specified by:
getDispatcher in interface RTEventSource
Returns:
the object providing registration of RTEvent listeners. The returned dispatcher corresponds to the javax.swing.event.EventListenerList, but enhanced for real-time usage.

addListener

public void addListener(RTEventListener subscriber)
Register subscriber as listener of application events emitted from this thread object. An Activity calling OtherThread.addListener(this); is the same as
     OtherThread.getDispatcher().addListener(
         this, RTThread.currentThread().getPriority(), null);
 

Specified by:
addListener in interface RTEventSource
Parameters:
subscriber - the object interested in getting the events via its putEvent method.

sleepUntil

public static void sleepUntil(long wakeUpTime)
                       throws RTInterrupted,
                              RTDelayed
Causes the currently executing thread to sleep (temporarily cease execution) until the specified moment in time. Time is measured in milliseconds, starting from 00am, January 1st 1970, UTC. The thread does not lose ownership of any monitors.

Parameters:
wakeUpTime - the time to wake up.
Throws:
java.lang.InterruptedException - if another thread has interrupted this thread.
RTDelayed - if execution continues more than maxTimeLag too late.
RTInterrupted

sleep

public static void sleep(long millis)
                  throws java.lang.InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
java.lang.IllegalArgumentException - if the value of millis is negative.
java.lang.InterruptedException - if another thread has interrupted this thread.
See Also:
Object.notify()

sleep

public static void sleep(long millis,
                         int nanos)
                  throws java.lang.InterruptedException
Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds. The thread does not lose ownership of any monitors.

Parameters:
millis - the length of time to sleep in milliseconds.
nanos - 0-999999 additional nanoseconds to sleep.
Throws:
java.lang.IllegalArgumentException - if the value of millis is negative or the value of nanos is not in the range 0-999999.
java.lang.InterruptedException - if another thread has interrupted this thread.

run

public void run()
Default non-empty run method, first calling init, then calling perform while not interrupted, and finally calling exit. Hence, to define a repetitive thread, you can keep this run method and instead override the perform method. As well known, creation of a new thread is accomplished by calling start, which in turn results in (in the context of the new thread) run being called.

Specified by:
run in interface java.lang.Runnable

getTimebase

public Timebase getTimebase()
Time bases not implemented yet.

Specified by:
getTimebase in interface Activity
Returns:
null.

getEnv

public Environment getEnv()
Environments not implemented yet.

Specified by:
getEnv in interface Activity
Returns:
null.

terminate

public void terminate()
Interrupt and join.

Specified by:
terminate in interface Activity