ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.47
Committed: Sun Sep 20 17:03:23 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +7 -7 lines
Log Message:
Terminate javadoc with a period.

File Contents

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