ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.33
Committed: Sat Jun 18 19:36:25 2005 UTC (18 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.32: +7 -6 lines
Log Message:
doc fixes

File Contents

# User Rev Content
1 dl 1.2 /*
2 dl 1.22 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6 dl 1.2
7 tim 1.1 package java.util.concurrent;
8    
9     /**
10 dl 1.27 * A <tt>TimeUnit</tt> represents time durations at a given unit of
11     * granularity and provides utility methods to convert across units,
12     * and to perform timing and delay operations in these units. A
13     * <tt>TimeUnit</tt> does not maintain time information, but only
14     * helps organize and use time representations that may be maintained
15     * separately across various contexts. A nanosecond is defined as one
16     * thousandth of a microsecond, a microsecond as one thousandth of a
17     * millisecond, a millisecond as one thousandth of a second, a minute
18     * as sixty seconds, an hour as sixty minutes, and a day as twenty four
19     * hours.
20     *
21     * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
22     * how a given timing parameter should be interpreted. For example,
23     * the following code will timeout in 50 milliseconds if the {@link
24     * java.util.concurrent.locks.Lock lock} is not available:
25     *
26     * <pre> Lock lock = ...;
27     * if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
28     * </pre>
29     * while this code will timeout in 50 seconds:
30     * <pre>
31     * Lock lock = ...;
32     * if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
33     * </pre>
34     *
35     * Note however, that there is no guarantee that a particular timeout
36     * implementation will be able to notice the passage of time at the
37     * same granularity as the given <tt>TimeUnit</tt>.
38     *
39     * @since 1.5
40     * @author Doug Lea
41     */
42 dl 1.20 public enum TimeUnit {
43 dl 1.27 NANOSECONDS (0) {
44     public long toNanos(long d) { return d; }
45     public long toMicros(long d) { return d/(C1/C0); }
46     public long toMillis(long d) { return d/(C2/C0); }
47     public long toSeconds(long d) { return d/(C3/C0); }
48     public long toMinutes(long d) { return d/(C4/C0); }
49     public long toHours(long d) { return d/(C5/C0); }
50     public long toDays(long d) { return d/(C6/C0); }
51     public long convert(long d, TimeUnit u) { return u.toNanos(d); }
52     int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
53     },
54     MICROSECONDS (1) {
55     public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); }
56     public long toMicros(long d) { return d; }
57     public long toMillis(long d) { return d/(C2/C1); }
58     public long toSeconds(long d) { return d/(C3/C1); }
59     public long toMinutes(long d) { return d/(C4/C1); }
60     public long toHours(long d) { return d/(C5/C1); }
61     public long toDays(long d) { return d/(C6/C1); }
62     public long convert(long d, TimeUnit u) { return u.toMicros(d); }
63     int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
64     },
65     MILLISECONDS (2) {
66     public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); }
67     public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); }
68     public long toMillis(long d) { return d; }
69     public long toSeconds(long d) { return d/(C3/C2); }
70     public long toMinutes(long d) { return d/(C4/C2); }
71     public long toHours(long d) { return d/(C5/C2); }
72     public long toDays(long d) { return d/(C6/C2); }
73     public long convert(long d, TimeUnit u) { return u.toMillis(d); }
74     int excessNanos(long d, long m) { return 0; }
75     },
76     SECONDS (3) {
77     public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); }
78     public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); }
79     public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); }
80     public long toSeconds(long d) { return d; }
81     public long toMinutes(long d) { return d/(C4/C3); }
82     public long toHours(long d) { return d/(C5/C3); }
83     public long toDays(long d) { return d/(C6/C3); }
84     public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
85     int excessNanos(long d, long m) { return 0; }
86     },
87     MINUTES (4) {
88     public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); }
89     public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); }
90     public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); }
91     public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
92     public long toMinutes(long d) { return d; }
93     public long toHours(long d) { return d/(C5/C4); }
94     public long toDays(long d) { return d/(C6/C4); }
95     public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
96     int excessNanos(long d, long m) { return 0; }
97     },
98     HOURS (5) {
99     public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); }
100     public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); }
101     public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); }
102     public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
103     public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
104     public long toHours(long d) { return d; }
105     public long toDays(long d) { return d/(C6/C5); }
106     public long convert(long d, TimeUnit u) { return u.toHours(d); }
107     int excessNanos(long d, long m) { return 0; }
108     },
109     DAYS (6) {
110     public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); }
111     public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); }
112     public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); }
113     public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
114     public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
115     public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
116     public long toDays(long d) { return d; }
117     public long convert(long d, TimeUnit u) { return u.toDays(d); }
118     int excessNanos(long d, long m) { return 0; }
119     };
120 jsr166 1.29
121 dl 1.27 /**
122     * The index of this unit. This value is no longer used in this
123     * version of this class, but is retained for serialization
124     * compatibility with previous version.
125     */
126     private final int index;
127 jsr166 1.29
128 dl 1.27 /** Internal constructor */
129     TimeUnit(int index) {
130     this.index = index;
131     }
132 jsr166 1.29
133 dl 1.27 // Handy constants for conversion methods
134     static final long C0 = 1L;
135     static final long C1 = C0 * 1000L;
136     static final long C2 = C1 * 1000L;
137     static final long C3 = C2 * 1000L;
138     static final long C4 = C3 * 60L;
139     static final long C5 = C4 * 60L;
140     static final long C6 = C5 * 24L;
141 jsr166 1.29
142 dl 1.27 static final long MAX = Long.MAX_VALUE;
143 jsr166 1.29
144 dl 1.27 /**
145     * Scale d by m, checking for overflow.
146     * This has a short name to make above code more readable.
147     */
148     static long x(long d, long m, long over) {
149     if (d > over) return Long.MAX_VALUE;
150     if (d < -over) return Long.MIN_VALUE;
151     return d * m;
152     }
153 jsr166 1.29
154 jsr166 1.33 // To maintain full signature compatibility with 1.5, and to improve the
155     // clarity of the generated javadoc (see 6287639: Abstract methods in
156     // enum classes should not be listed as abstract), method convert
157     // etc. are not declared abstract but otherwise act as abstract methods.
158 dl 1.32
159 dl 1.27 /**
160     * Convert the given time duration in the given unit to this
161     * unit. Conversions from finer to coarser granularities
162     * truncate, so lose precision. For example converting
163     * <tt>999</tt> milliseconds to seconds results in
164     * <tt>0</tt>. Conversions from coarser to finer granularities
165     * with arguments that would numerically overflow saturate to
166     * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
167     * if positive.
168 jsr166 1.33 *
169     * <p>For example, to convert 10 minutes to milliseconds, use:
170 dl 1.32 * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
171 dl 1.27 *
172 jsr166 1.33 * @param sourceDuration the time duration in the given <tt>sourceUnit</tt>
173 dl 1.32 * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
174 dl 1.27 * @return the converted duration in this unit,
175     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
176     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
177     */
178 dl 1.32 public long convert(long sourceDuration, TimeUnit sourceUnit) {
179     throw new AbstractMethodError();
180     }
181 jsr166 1.29
182 dl 1.27 /**
183     * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
184     * @param duration the duration
185     * @return the converted duration,
186     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
187     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
188     * @see #convert
189     */
190 dl 1.32 public long toNanos(long duration) {
191     throw new AbstractMethodError();
192     }
193 jsr166 1.29
194 dl 1.27 /**
195     * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
196     * @param duration the duration
197     * @return the converted duration,
198     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
199     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
200     * @see #convert
201     */
202 dl 1.32 public long toMicros(long duration) {
203     throw new AbstractMethodError();
204     }
205 jsr166 1.29
206 dl 1.27 /**
207     * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
208     * @param duration the duration
209     * @return the converted duration,
210     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
211     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
212     * @see #convert
213     */
214 dl 1.32 public long toMillis(long duration) {
215     throw new AbstractMethodError();
216     }
217 jsr166 1.29
218 dl 1.27 /**
219     * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
220     * @param duration the duration
221     * @return the converted duration,
222     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
223     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
224     * @see #convert
225     */
226 dl 1.32 public long toSeconds(long duration) {
227     throw new AbstractMethodError();
228     }
229 jsr166 1.29
230 dl 1.27 /**
231     * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
232     * @param duration the duration
233     * @return the converted duration,
234     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
235     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
236     * @see #convert
237 dl 1.31 * @since 1.6
238 dl 1.27 */
239 dl 1.32 public long toMinutes(long duration) {
240     throw new AbstractMethodError();
241     }
242 jsr166 1.29
243 dl 1.27 /**
244     * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
245     * @param duration the duration
246     * @return the converted duration,
247     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
248     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
249     * @see #convert
250 dl 1.31 * @since 1.6
251 dl 1.27 */
252 dl 1.32 public long toHours(long duration) {
253     throw new AbstractMethodError();
254     }
255 jsr166 1.29
256 dl 1.27 /**
257     * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
258     * @param duration the duration
259     * @return the converted duration
260     * @see #convert
261 dl 1.31 * @since 1.6
262 dl 1.27 */
263 dl 1.32 public long toDays(long duration) {
264     throw new AbstractMethodError();
265     }
266 jsr166 1.29
267 dl 1.27 /**
268     * Utility to compute the excess-nanosecond argument to wait,
269     * sleep, join.
270     * @param d the duration
271     * @param m the number of milliseconds
272     * @return the number of nanoseconds
273     */
274     abstract int excessNanos(long d, long m);
275 jsr166 1.29
276 dl 1.27 /**
277 jsr166 1.29 * Performs a timed <tt>Object.wait</tt> using this time unit.
278 dl 1.27 * This is a convenience method that converts timeout arguments
279     * into the form required by the <tt>Object.wait</tt> method.
280     *
281     * <p>For example, you could implement a blocking <tt>poll</tt>
282     * method (see {@link BlockingQueue#poll BlockingQueue.poll})
283     * using:
284     *
285 jsr166 1.30 * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
286 dl 1.27 * while (empty) {
287     * unit.timedWait(this, timeout);
288     * ...
289     * }
290     * }</pre>
291     *
292     * @param obj the object to wait on
293 dl 1.28 * @param timeout the maximum time to wait. If less than
294     * or equal to zero, do not wait at all.
295 dl 1.27 * @throws InterruptedException if interrupted while waiting.
296     * @see Object#wait(long, int)
297     */
298     public void timedWait(Object obj, long timeout)
299     throws InterruptedException {
300     if (timeout > 0) {
301     long ms = toMillis(timeout);
302     int ns = excessNanos(timeout, ms);
303     obj.wait(ms, ns);
304     }
305     }
306 jsr166 1.29
307 dl 1.27 /**
308 jsr166 1.29 * Performs a timed <tt>Thread.join</tt> using this time unit.
309 dl 1.27 * This is a convenience method that converts time arguments into the
310     * form required by the <tt>Thread.join</tt> method.
311     * @param thread the thread to wait for
312 dl 1.28 * @param timeout the maximum time to wait. If less than
313     * or equal to zero, do not wait at all.
314 dl 1.27 * @throws InterruptedException if interrupted while waiting.
315     * @see Thread#join(long, int)
316     */
317     public void timedJoin(Thread thread, long timeout)
318     throws InterruptedException {
319     if (timeout > 0) {
320     long ms = toMillis(timeout);
321     int ns = excessNanos(timeout, ms);
322     thread.join(ms, ns);
323     }
324     }
325 jsr166 1.29
326 dl 1.27 /**
327 jsr166 1.29 * Performs a <tt>Thread.sleep</tt> using this unit.
328 dl 1.27 * This is a convenience method that converts time arguments into the
329     * form required by the <tt>Thread.sleep</tt> method.
330 dl 1.28 * @param timeout the minimum time to sleep. If less than
331     * or equal to zero, do not sleep at all.
332 dl 1.27 * @throws InterruptedException if interrupted while sleeping.
333     * @see Thread#sleep
334     */
335     public void sleep(long timeout) throws InterruptedException {
336     if (timeout > 0) {
337     long ms = toMillis(timeout);
338     int ns = excessNanos(timeout, ms);
339     Thread.sleep(ms, ns);
340     }
341     }
342 jsr166 1.29
343 tim 1.1 }