11 g53srp: clocks and times in rtsj chris greenhalgh school of computer science

10
1 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh Chris Greenhalgh School of Computer School of Computer Science Science

Upload: gerald-tucker

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

11

G53SRP: Clocks and Times in RTSJ

Chris GreenhalghChris Greenhalgh

School of Computer ScienceSchool of Computer Science

Page 2: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School 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

Page 3: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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)

Page 4: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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)

Page 5: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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

Page 6: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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)

Page 7: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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

Page 8: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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);

Page 9: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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

Page 10: 11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science

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