Difference between revisions of "Clocks and Time"
From Suhrid.net Wiki
Jump to navigationJump to search (→Intro) |
(→Intro) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
* Relative Time - Represents a time interval milliseconds + nanoseconds seconds long. It generally is used to represent a time relative to now. | * Relative Time - Represents a time interval milliseconds + nanoseconds seconds long. It generally is used to represent a time relative to now. | ||
* Standard Java only has a wall clock - calendar time. But real time systems require: | * Standard Java only has a wall clock - calendar time. But real time systems require: | ||
− | ** A monotonic clock. | + | ** A monotonic clock: A monotonic clock that 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. |
− | ** A countdown clock. | + | ** A countdown clock: A countdown clock that can be paused, continued or reset. |
** A CPU execution time clock - measures amount of CPU time consumed by a particular thread or object. | ** A CPU execution time clock - measures amount of CPU time consumed by a particular thread or object. | ||
Line 61: | Line 61: | ||
** The HighResolutionTime has a static waitForObject(Object, HighResolutionTime) method which is calls Object.wait() but with a HighResolutionTime parameter instead. | ** The HighResolutionTime has a static waitForObject(Object, HighResolutionTime) method which is calls Object.wait() but with a HighResolutionTime parameter instead. | ||
− | [[Category: | + | * Rational Time: Is a Relative Time type with an associated frequency. Used to represent the rate at which certain events occur. |
+ | * A rational time with an interval of 1 second and a frequency of 100ms, will have an inter arrival time of 10ms. | ||
+ | * However note that the inter arrival time (the release) is not guaranteed only the frequency is. | ||
+ | |||
+ | [[Category:RealtimeJava]] |
Latest revision as of 09:28, 10 January 2012
Intro
- Absolute Time - Represents a specific point in time given by milliseconds plus nanoseconds past some point in time fixed by the clock. For the default real-time clock the fixed point is the Epoch (January 1, 1970, 00:00:00 GMT)
- Relative Time - Represents a time interval milliseconds + nanoseconds seconds long. It generally is used to represent a time relative to now.
- Standard Java only has a wall clock - calendar time. But real time systems require:
- A monotonic clock: A monotonic clock that 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.
- A countdown clock: A countdown clock that can be paused, continued or reset.
- A CPU execution time clock - measures amount of CPU time consumed by a particular thread or object.
- Time is supported through the HighResolutionTime abstract class. Three implementing concrete classes - absolute, relative and rational.
- Clock is supported through an abstract Clock class.
- The realtime clock advances monotonically.
- HighResolutionTime offers conversion between absolute and relative time values.
import java.util.Calendar;
import java.util.Date;
import javax.realtime.*;
public class TimeTest2 {
public static void main(String[] args) {
Clock clock = Clock.getRealtimeClock();
Calendar cal = Calendar.getInstance();
cal.set(2012, 0, 1, 0, 0, 0);
Date newYear2012 = cal.getTime();
AbsoluteTime at = new AbsoluteTime(newYear2012);
//Convert absolute to relative
RelativeTime rt = at.relative(clock);
System.out.println("The number of ms between now and new year 2012 " + rt);
//To confirm - add the current time and the relative time and we'll get new year 2012.
Date d = new Date(System.currentTimeMillis() + rt.getMilliseconds());
System.out.println(d);
long oneweekms = 86400 * 7 * 1000;
RelativeTime oneweek = new RelativeTime(oneweekms, 0);
//Convert relative to absolute
AbsoluteTime abs = oneweek.absolute(clock);
//Will print an absolute date one week from now
System.out.println(abs.getDate());
}
}
- Dont' change the clock associated with a time value because it is potentially unsafe. Particularly for absolute time values:
- This is because absolute time values are internally represented as a number of milliseconds and nanoseconds since an epoch.
- and different clocks may have different epochs.
- The HighResolutionTime has a static waitForObject(Object, HighResolutionTime) method which is calls Object.wait() but with a HighResolutionTime parameter instead.
- Rational Time: Is a Relative Time type with an associated frequency. Used to represent the rate at which certain events occur.
- A rational time with an interval of 1 second and a frequency of 100ms, will have an inter arrival time of 10ms.
- However note that the inter arrival time (the release) is not guaranteed only the frequency is.