ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/java/lang/Clock.java
Revision: 1.1
Committed: Sun Sep 29 19:20:58 2002 UTC (21 years, 9 months ago) by jsr166
Branch: MAIN
Branch point for: jsr166
Log Message:
Initial revision

File Contents

# User Rev Content
1 jsr166 1.1 package java.lang;
2    
3     /**
4     * A Clock performs system timing functions at a given unit
5     * of granularity.
6     *
7     * The class cannot be directly instantiated.
8     * Use the <tt>seconds</tt>, <tt>milliseconds</tt>,
9     * <tt>microseconds</tt>, and <tt>nanoseconds</tt> static singletons.
10     **/
11    
12     public abstract class Clock implements java.io.Serializable {
13     /**
14     * The (negative of) power of ten of granularity.
15     * 0 for sec, 3 for msec, 6 for usec, 9 for nsec.
16     **/
17     final int powerOfTen;
18    
19     /** package-private constructor */
20     Clock(int power) { powerOfTen = power; }
21    
22     /** Time unit for one-second granularities */
23     public static final Clock SECONDS = new SecondClock();
24    
25     /** Time unit for one-millisecond granularities */
26     public static final Clock MILLISECONDS = new MillisecondClock();
27    
28     /** Time unit for one-microsecond granularities */
29     public static final Clock MICROSECONDS = new MicrosecondClock();
30    
31     /** Time unit for one-nanosecond granularities */
32     public static final Clock NANOSECONDS = new NanosecondClock();
33    
34     /**
35     * Return the current time in the current unit. The result is an
36     * offset from the "epoch", midnight, January 1, 1970 UTC. The time
37     * returned by any two clocks of different granularities may differ
38     * by one unit of the coarsest of the two.
39     **/
40     public abstract long currentTime();
41    
42     /**
43     * Return the time elapsed since the given time, in the current unit.
44     * The result may be unreliable if more than 2^63 time units have
45     * elapsed.
46     **/
47     public long since(long time) {
48     return currentTime() - time;
49     }
50    
51     /**
52     * Return the minimum ideally possible non-zero difference in values
53     * between two successive calls to current for this clock. For
54     * example, on a system with a 10-microsecond timer,
55     * <tt>Clock.nanooseconds.bestResolution()<tt> would return <tt>10000</tt>,
56     * <tt>Clock.microseconds.bestResolution()<tt> would return <tt>10</tt>,
57     * <tt>Clock.milliseconds.bestResolution()<tt> would return <tt>1</tt>, and
58     * <tt>Clock.seconds.bestResolution()<tt> would return <tt>1</tt>.
59     *
60     * <p>
61     * Note that this method does <em>NOT</em> imply any properties
62     * about the minimum duration of timed waits or sleeps.
63     **/
64     public abstract long bestResolution();
65    
66     /**
67     * Convert the given time value to the current granularity.
68     * This conversion may overflow or lose precision.
69     * @param time the time in given unit
70     * @param granularity the unit of the time argument
71     * @return the converted time.
72     **/
73     public long convert(long time, Clock granularity) {
74     int d = granularity.powerOfTen - this.powerOfTen;
75     // Note: if non-multiple-of-3 powers are ever used, this must change.
76     while (d > 0) {
77     d -= 3;
78     time *= 1000;
79     }
80     while (d < 0) {
81     d += 3;
82     time /= 1000;
83     }
84     return time;
85     }
86    
87    
88     /**
89     * Perform a timed Object.wait in current units.
90     * @param monitor the object to wait on
91     * @param time the maximum time to wait
92     * @throws InterruptedException if interrupted while waiting.
93     **/
94     public abstract void timedWait(Object monitor, long time)
95     throws InterruptedException;
96    
97     /**
98     * Perform a timed Thread.join in current units.
99     * @param monitor the thread to wait for
100     * @param time the maximum time to wait
101     * @throws InterruptedException if interrupted while waiting.
102     **/
103     public abstract void timedJoin(Thread thread, long time)
104     throws InterruptedException;
105    
106     /**
107     * Perform a timed sleep in current units.
108     * @param time the maximum time to wait
109     * @throws InterruptedException if interrupted while sleeping.
110     **/
111     public abstract void sleep(long time) throws InterruptedException;
112    
113    
114     static final class SecondClock extends Clock {
115     private SecondClock() { super(0); }
116     public long currentTime() {
117     return System.currentTimeMillis() / 1000;
118     }
119    
120     public long bestResolution() {
121     return 1;
122     }
123    
124     public void timedWait(Object monitor, long time) throws InterruptedException {
125     monitor.wait(time * 1000);
126     }
127    
128     public void timedJoin(Thread thread, long time) throws InterruptedException {
129     thread.join(time * 1000);
130     }
131    
132     public void sleep(long time) throws InterruptedException {
133     Thread.sleep(time * 1000);
134     }
135     }
136    
137     static final class MillisecondClock extends Clock {
138     private MillisecondClock() { super(3); }
139     public long currentTime() {
140     return System.currentTimeMillis();
141     }
142    
143     public long bestResolution() {
144     return 1;
145     }
146    
147     public void timedWait(Object monitor, long time) throws InterruptedException {
148     monitor.wait(time);
149     }
150    
151     public void timedJoin(Thread thread, long time) throws InterruptedException {
152     thread.join(time);
153     }
154    
155     public void sleep(long time) throws InterruptedException {
156     Thread.sleep(time);
157     }
158     }
159    
160     static final class MicrosecondClock extends Clock {
161     private MicrosecondClock() { super(6); }
162     public long currentTime() {
163     return System.currentTimeMillis() * 1000;
164     }
165    
166     public long bestResolution() {
167     return 1000;
168     }
169    
170     public void timedWait(Object monitor, long time) throws InterruptedException {
171     long ms = time / 1000;
172     int ns = (int)((time - ms * 1000) / 1000);
173     monitor.wait(ms, ns);
174     }
175    
176     public void timedJoin(Thread thread, long time) throws InterruptedException {
177     long ms = time / 1000;
178     int ns = (int)((time - ms * 1000) / 1000);
179     thread.join(ms, ns);
180     }
181    
182     public void sleep(long time) throws InterruptedException {
183     long ms = time / 1000;
184     int ns = (int)((time - ms * 1000) / 1000);
185     Thread.sleep(ms, ns);
186     }
187     }
188    
189    
190     static final class NanosecondClock extends Clock {
191     private NanosecondClock() { super(9); }
192     public long currentTime() {
193     return System.currentTimeMillis() * 1000000;
194     }
195    
196     public long bestResolution() {
197     return 1000000;
198     }
199    
200     public void timedWait(Object monitor, long time) throws InterruptedException {
201     long ms = time / 1000000;
202     int ns = (int)(time - ms * 1000000);
203     monitor.wait(ms, ns);
204     }
205    
206     public void timedJoin(Thread thread, long time) throws InterruptedException {
207     long ms = time / 1000000;
208     int ns = (int)(time - ms * 1000000);
209     thread.join(ms, ns);
210     }
211    
212     public void sleep(long time) throws InterruptedException {
213     long ms = time / 1000000;
214     int ns = (int)(time - ms * 1000000);
215     Thread.sleep(ms, ns);
216     }
217     }
218    
219    
220     }