11 g53srp: clocks and times in rtsj chris greenhalgh school of computer science
TRANSCRIPT
11
G53SRP: Clocks and Times in RTSJ
Chris GreenhalghChris Greenhalgh
School of Computer ScienceSchool of Computer Science
22
Contents• OverviewOverview• Time classesTime classes
– HighResolutionTime, AbsoluteTime, RelativeTimeHighResolutionTime, AbsoluteTime, RelativeTime
• Clock classClock class• ExampleExample• SummarySummary
• Book: Wellings ch. 9Book: Wellings ch. 9
3
Class Overview
3
<<abstract>>javax.realtime.
HighResolutionTime
javax.realtime.AbsoluteTime
javax.realtime.RelativeTime
extends
javax.realtime.Clockhas
javax.realtime.RationalTime
extends
Deprecated(do not use)
4
HighResolutionTime classpackage javax.realtime;public abstract class HighResolutionTime { // nano-second resolution times public long getMilliseconds(); public int getNanoseconds(); public void set(long millis, int nanos); public Clock getClock(); // various utility/conversion methods … // high-res wait public static waitForObject(Object target, HighResolutionTime time) throws InterruptedException;}
Every time is based on a clock
(it came from)
5
AbsoluteTime Classpackage javax.realtime;public class AbsoluteTime extends HighResolutionTime { // cons (also others) public AbsoluteTime(long millis, int nanos); // various utility conversions including… public AbsoluteTime add(RelativeTime time); public RelativeTime subtract(AbsoluteTime time); public AbsoluteTime subtract(RelativeTime time); …}
• Time is offset from clock-specific EpochTime is offset from clock-specific Epoch– Default clock Epoch is midnight 1Default clock Epoch is midnight 1stst January 1970 GMT January 1970 GMT
6
RelativeTime classpackage javax.realtime;public class RelativeTime extends HighResolutionTime { // cons (also others) public RelativeTime(long millis, int nanos); // various utility conversions including… public RelativeTime add(RelativeTime time); public RelativeTime subtract(RelativeTime time); …}
Note: still based on a Clock, e.g. for resolution (and accuracy)
7
Clock classpackage javax.realtime;public abstract class Clock { // standard system realtime clock public static Clock getRealtimeClock(); public AbsoluteTime getTime(); public RelativeTime getResolution(); // offset of this clock vs Epoch public RelativeTime getEpochOffset(); …}
Note: may be other clocks without date, e.g. timer from power-on of system.If so, this throws UnsupportedOperationException
8
E.g. RTClock.java …
Clock clock = Clock.getRealtimeClock();
AbsoluteTime time = clock.getTime();
AbsoluteTime newTime = clock.getTime();
RelativeTime elapsed = newTime.subtract(time);
RelativeTime delay = new RelativeTime(1000,0);
AbsoluteTime delayTime = newTime.add(delay);
…
9
E.g. RTSleep.java …
Clock clock = Clock.getRealtimeClock();
AbsoluteTime time = clock.getTime();
RelativeTime delay = new RelativeTime(1000,0);
AbsoluteTime delayTime = time.add(delay);
RealtimeThread.sleep(delay);
RealtimeThread.sleep(delayTime);
…
Relative sleepi.e. delay ms/ns
Absolute sleepi.e. until delayTime
10
Summary• RTSJ supports nanosecond-resolution times
– HighResolutionTime– (but Clocks probably won’t be nanosecond granularity)
• Can be used for – Wait variant (HighResolutionTime.waitForObject) – Sleep variant (RealtimeThread.sleep)– Other time specifications (e.g. ReleaseProperties – later)
• Distinguishes relative and absolute times– AbsoluteTime & RelativeTime– Includes various utility manipulation operations
• Supports at least one real-time clock– Clock.getRealtimeClock()– May support more