Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 81 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
81
Dung lượng
394,99 KB
Nội dung
Lập trình thời gian thực trong Java Đồng hồ và lập lịch Clocks and Time Lecture aims To provide some background on clocks and time To explain absolute and relative time values To consider the support for real-time clocks To give some simple examples To review the parameter classes in detail To consider alternative schedulers 2 Vũ Quang Dũng Introduction I Standard Java only supports the notion of a wall clock (calendar time); for many applications, a clock based on UTC (Coordinated Universal Time) is sufficient However real-time systems often require A monotonic clock which progresses at a constant rate and is not subject to the insertion of extra ticks to reflect leap seconds (as UTC clocks are). A constant rate is needed for control algorithms which want to executed on a regular basis. Many monotonic clocks are also relative to system startup and can be used only to measure the passage of time, not calendar time A count down clock which can be paused, continued or reset (for example the clock which counts down to the launch of the Space Shuttle) A CPU execution time clock which measures the amount of CPU time that is being consumed by a particular thread or object 3 Vũ Quang Dũng Introduction II All of the above clocks also need a resolution which is potentially finer than the millisecond level They may all be based on the same underlying physical clock, or be supported by separate clocks Where more than one clock is provided, issues of the relationship between them may be importance; in particular, whether their values can drift apart We consider the additional clock and time classes that are provided by the RTSJ to augment the standard Java facilities 4 Vũ Quang Dũng Basic Model The RTSJ defines a hierarchy of time classes rooted on the abstract HighResolutionTime class This abstract class has three subclasses: one which represents absolute time one which represents relative time and one which represents rational time The intention is to allow support for time values down to the nanosecond accuracy Clocks are supported through an abstract Clock class 5 Vũ Quang Dũng High Resolution Time I 6 Vũ Quang Dũng public abstract class HighResolutionTime implements Comparable, Cloneable { // methods public abstract AbsoluteTime absolute(Clock clock); public int compareTo(HighResolutionTime time); public boolean equals(HighResolutionTime time); public final long getMilliseconds(); public final int getNanoseconds(); public abstract RelativeTime relative(Clock clock); public void set(HighResolutionTime time); public void set(long millis, int nanos); public static void waitForObject(Object target, HighResolutionTime time) throws InterruptedException; } High Resolution Time II The abstract methods (absolute and relative) allow time types that are relative to be re-expressed as absolute time values and vice versa. The methods also allow the clocks associated with the values to be changed. absolute to absolute The value returned has the same millisecond and nanosecond components as the encapsulated time value absolute to relative The value returned is the value of the encapsulated absolute time minus the current time as measured from the given clock parameter relative to relative The value returned has the same millisecond and nanosecond components as the encapsulated time value relative to absolute The value returned is the value of current time as measured from the given clock parameter plus the encapsulated relative time 7 Vũ Quang Dũng High Resolution Time III Changing the clock associated with a time value is potentially unsafe, particularly for absolute time values This is because absolute time values are represented as a number of milliseconds and nanoseconds since an epoch Different clocks may have different epochs. The waitForObject does not resolve the problem of determining if the schedulable object was woken by a notify method call or by a timeout It does allow both relative and absolute time values to be specified. 8 Vũ Quang Dũng Absolute Time I 9 Vũ Quang Dũng public class AbsoluteTime extends HighResolutionTime { // constructors public AbsoluteTime(); public AbsoluteTime(AbsoluteTime time); public AbsoluteTime(java.util.Date date); public AbsoluteTime(long millis, int nanos); // methods } Absolute Time II 10 Vũ Quang Dũng public class AbsoluteTime extends HighResolutionTime { public AbsoluteTime absolute(Clock clock); public AbsoluteTime add(long millis, int nanos); public final AbsoluteTime add(RelativeTime time); public java.util.Date getDate(); public RelativeTime relative(Clock clock); public void set(java.util.Date date); public RelativeTime subtract(AbsoluteTime time); public AbsoluteTime subtract(RelativeTime time); public String toString(); } [...]... RelativeTime getResolution(); public AbsoluteTime getTime(); public abstract void getTime(AbsoluteTime time) ; public abstract void setResolution( RelativeTime resolution); } Vũ Quang Dũng 14 Example: measuring elapse time { AbsoluteTime oldTime, newTime; RelativeTime interval; Clock clock = Clock.getRealtimeClock(); oldTime = clock.getTime(); // other computations newTime = clock.getTime(); interval = newTime.subtract(oldTime);...Relative Time I public class RelativeTime extends HighResolutionTime { // constructors public RelativeTime(); public RelativeTime(long millis, int nanos); public RelativeTime(RelativeTime time) ; } Vũ Quang Dũng 11 Relative Time II public class RelativeTime extends HighResolutionTime { // methods public AbsoluteTime absolute(Clock clock); public RelativeTime add(long millis, int nanos); public RelativeTime... new RelativeTime(1000,0); } private AbsoluteTime startTime; private RelativeTime remainingTime; private Clock myClock; private boolean counting; private boolean go; private RelativeTime tick; Vũ Quang Dũng 17 LaunchClock II public RelativeTime getResolution() { return tick; } public synchronized AbsoluteTime getCurrentLaunchTime() { return new AbsoluteTime( myClock.getTime().add(remainingTime)); //... add(RelativeTime time) ; public RelativeTime relative(Clock clock); public RelativeTime subtract(RelativeTime time) ; public String toString(); } Vũ Quang Dũng 12 Clocks The RTSJ Clock class defines the abstract class from which all clocks are derived The language allows many different types of clocks; eg, an execution -time clock which measures the amount of execution time being consumed There is real -time. .. while(myClock.getTime().compareTo(startTime) < 0) HighResolutionTime waitForObject(this, startTime); while(remainingTime.getMilliseconds() > 0) { while(!counting) wait(); HighResolutionTime.waitForObject(this, tick); remainingTime.set( remainingTime.getMilliseconds() tick.getMilliseconds()); } go = true; notifyAll(); } } catch(InterruptedException ie) { } } } Vũ Quang Dũng 21 Scheduling and Schedulable... newTime.subtract(oldTime); } Vũ Quang Dũng 15 Example: A launch clock A launch clock is clock which is initiated with a relative time value and an absolute time value The absolute time value is the time at which the clock is to start ticking; the relative time value is the duration of the countdown The count down can be stopped, restarted, or reset The class extends the Thread class The constructor saves the start time, ... the start time, the duration and the clock to be used The resolution of the count down is one second, and various functions allow the current launch time to be queried Vũ Quang Dũng 16 LaunchClock I public class LaunchClock extends Thread { public LaunchClock(AbsoluteTime at, RelativeTime countDown) { super(); startTime = at; remainingTime = countDown; myClock = Clock.getRealtimeClock(); counting = true;... Quang Dũng 22 Introduction Real -time systems must be able to interact with their environment in a timely and predictable manner Designers must engineer analysable systems whose timing properties can be predicted and mathematically proven correct Scheduling is the ordering of thread/process executions so that the underlying hardware resources (processors, networks, etc.) and software resources (shared... progress uniformly and not be subject to the insertion of leap ticks The static method getRealtimeClock allows this clock to be obtained Other methods are provided to get the resolution of a clock and, if the hardware permits, to set the resolution of a clock Vũ Quang Dũng 13 The Clock Class public abstract class Clock { // constructor public Clock(); // methods public static Clock getRealtimeClock(); public... objects at their creation time — although no particular priority assignment algorithm is mandated by FPS, it is usual to assign priorities according to the relative deadline of the schedulable object (relative to the schedulable object’s release time) ; the shorter the deadline, the higher the priority priority inheritance when accessing resources Vũ Quang Dũng 25 FPS: Mechanism and Analysis Mechanism: . Lập trình thời gian thực trong Java Đồng hồ và lập lịch Clocks and Time Lecture aims To provide some background on clocks and time To explain absolute and relative time values . time 15 Vũ Quang Dũng { AbsoluteTime oldTime, newTime; RelativeTime interval; Clock clock = Clock.getRealtimeClock(); oldTime = clock.getTime(); // other computations newTime = clock.getTime(); interval. AbsoluteTime extends HighResolutionTime { public AbsoluteTime absolute(Clock clock); public AbsoluteTime add(long millis, int nanos); public final AbsoluteTime add(RelativeTime time) ; public java. util.Date