ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.43
Committed: Wed Jul 17 21:17:43 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +27 -0 lines
Log Message:
fix doclint warnings

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.39 * <pre> {@code
27     * 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.39 * <pre> {@code
32     * 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     * Time unit representing one thousandth of a microsecond
45     */
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     * Time unit representing one thousandth of a millisecond
60     */
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     * Time unit representing one thousandth of a second
75     */
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     * Time unit representing one second
90     */
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     * Time unit representing sixty seconds
105     */
106 dl 1.34 MINUTES {
107 dl 1.27 public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); }
108     public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); }
109     public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); }
110     public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
111     public long toMinutes(long d) { return d; }
112     public long toHours(long d) { return d/(C5/C4); }
113     public long toDays(long d) { return d/(C6/C4); }
114     public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
115     int excessNanos(long d, long m) { return 0; }
116     },
117 jsr166 1.43
118     /**
119     * Time unit representing sixty minutes
120     */
121 dl 1.34 HOURS {
122 dl 1.27 public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); }
123     public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); }
124     public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); }
125     public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
126     public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
127     public long toHours(long d) { return d; }
128     public long toDays(long d) { return d/(C6/C5); }
129     public long convert(long d, TimeUnit u) { return u.toHours(d); }
130     int excessNanos(long d, long m) { return 0; }
131     },
132 jsr166 1.43
133     /**
134     * Time unit representing twenty four hours
135     */
136 dl 1.34 DAYS {
137 dl 1.27 public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); }
138     public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); }
139     public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); }
140     public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
141     public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
142     public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
143     public long toDays(long d) { return d; }
144     public long convert(long d, TimeUnit u) { return u.toDays(d); }
145     int excessNanos(long d, long m) { return 0; }
146     };
147 jsr166 1.29
148 dl 1.27 // Handy constants for conversion methods
149     static final long C0 = 1L;
150     static final long C1 = C0 * 1000L;
151     static final long C2 = C1 * 1000L;
152     static final long C3 = C2 * 1000L;
153     static final long C4 = C3 * 60L;
154     static final long C5 = C4 * 60L;
155     static final long C6 = C5 * 24L;
156 jsr166 1.29
157 dl 1.27 static final long MAX = Long.MAX_VALUE;
158 jsr166 1.29
159 dl 1.27 /**
160     * Scale d by m, checking for overflow.
161     * This has a short name to make above code more readable.
162     */
163     static long x(long d, long m, long over) {
164     if (d > over) return Long.MAX_VALUE;
165     if (d < -over) return Long.MIN_VALUE;
166     return d * m;
167     }
168 jsr166 1.29
169 jsr166 1.33 // To maintain full signature compatibility with 1.5, and to improve the
170     // clarity of the generated javadoc (see 6287639: Abstract methods in
171     // enum classes should not be listed as abstract), method convert
172     // etc. are not declared abstract but otherwise act as abstract methods.
173 dl 1.32
174 dl 1.27 /**
175 jsr166 1.42 * Converts the given time duration in the given unit to this unit.
176     * Conversions from finer to coarser granularities truncate, so
177     * lose precision. For example, converting {@code 999} milliseconds
178     * to seconds results in {@code 0}. Conversions from coarser to
179     * finer granularities with arguments that would numerically
180     * overflow saturate to {@code Long.MIN_VALUE} if negative or
181     * {@code Long.MAX_VALUE} if positive.
182 jsr166 1.33 *
183     * <p>For example, to convert 10 minutes to milliseconds, use:
184 jsr166 1.40 * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
185 dl 1.27 *
186 jsr166 1.40 * @param sourceDuration the time duration in the given {@code sourceUnit}
187     * @param sourceUnit the unit of the {@code sourceDuration} argument
188 dl 1.27 * @return the converted duration in this unit,
189 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
190     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
191 dl 1.27 */
192 dl 1.32 public long convert(long sourceDuration, TimeUnit sourceUnit) {
193     throw new AbstractMethodError();
194     }
195 jsr166 1.29
196 dl 1.27 /**
197 jsr166 1.41 * Equivalent to
198     * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
199 dl 1.27 * @param duration the duration
200     * @return the converted duration,
201 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
202     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
203 dl 1.27 */
204 dl 1.32 public long toNanos(long duration) {
205     throw new AbstractMethodError();
206     }
207 jsr166 1.29
208 dl 1.27 /**
209 jsr166 1.41 * Equivalent to
210     * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
211 dl 1.27 * @param duration the duration
212     * @return the converted duration,
213 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
214     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
215 dl 1.27 */
216 dl 1.32 public long toMicros(long duration) {
217     throw new AbstractMethodError();
218     }
219 jsr166 1.29
220 dl 1.27 /**
221 jsr166 1.41 * Equivalent to
222     * {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
223 dl 1.27 * @param duration the duration
224     * @return the converted duration,
225 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
226     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
227 dl 1.27 */
228 dl 1.32 public long toMillis(long duration) {
229     throw new AbstractMethodError();
230     }
231 jsr166 1.29
232 dl 1.27 /**
233 jsr166 1.41 * Equivalent to
234     * {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
235 dl 1.27 * @param duration the duration
236     * @return the converted duration,
237 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
238     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
239 dl 1.27 */
240 dl 1.32 public long toSeconds(long duration) {
241     throw new AbstractMethodError();
242     }
243 jsr166 1.29
244 dl 1.27 /**
245 jsr166 1.41 * Equivalent to
246     * {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
247 dl 1.27 * @param duration the duration
248     * @return the converted duration,
249 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
250     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
251 dl 1.31 * @since 1.6
252 dl 1.27 */
253 dl 1.32 public long toMinutes(long duration) {
254     throw new AbstractMethodError();
255     }
256 jsr166 1.29
257 dl 1.27 /**
258 jsr166 1.41 * Equivalent to
259     * {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
260 dl 1.27 * @param duration the duration
261     * @return the converted duration,
262 jsr166 1.40 * or {@code Long.MIN_VALUE} if conversion would negatively
263     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
264 dl 1.31 * @since 1.6
265 dl 1.27 */
266 dl 1.32 public long toHours(long duration) {
267     throw new AbstractMethodError();
268     }
269 jsr166 1.29
270 dl 1.27 /**
271 jsr166 1.41 * Equivalent to
272     * {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
273 dl 1.27 * @param duration the duration
274     * @return the converted duration
275 dl 1.31 * @since 1.6
276 dl 1.27 */
277 dl 1.32 public long toDays(long duration) {
278     throw new AbstractMethodError();
279     }
280 jsr166 1.29
281 dl 1.27 /**
282     * Utility to compute the excess-nanosecond argument to wait,
283     * sleep, join.
284     * @param d the duration
285     * @param m the number of milliseconds
286     * @return the number of nanoseconds
287     */
288     abstract int excessNanos(long d, long m);
289 jsr166 1.29
290 dl 1.27 /**
291 jsr166 1.37 * Performs a timed {@link Object#wait(long, int) Object.wait}
292     * using this time unit.
293 dl 1.27 * This is a convenience method that converts timeout arguments
294 jsr166 1.40 * into the form required by the {@code Object.wait} method.
295 dl 1.27 *
296 jsr166 1.40 * <p>For example, you could implement a blocking {@code poll}
297 dl 1.27 * method (see {@link BlockingQueue#poll BlockingQueue.poll})
298     * using:
299     *
300 jsr166 1.36 * <pre> {@code
301     * public synchronized Object poll(long timeout, TimeUnit unit)
302     * throws InterruptedException {
303     * while (empty) {
304     * unit.timedWait(this, timeout);
305     * ...
306     * }
307     * }}</pre>
308 dl 1.27 *
309     * @param obj the object to wait on
310 dl 1.28 * @param timeout the maximum time to wait. If less than
311     * or equal to zero, do not wait at all.
312 jsr166 1.37 * @throws InterruptedException if interrupted while waiting
313 dl 1.27 */
314     public void timedWait(Object obj, long timeout)
315 jsr166 1.37 throws InterruptedException {
316 dl 1.27 if (timeout > 0) {
317     long ms = toMillis(timeout);
318     int ns = excessNanos(timeout, ms);
319     obj.wait(ms, ns);
320     }
321     }
322 jsr166 1.29
323 dl 1.27 /**
324 jsr166 1.37 * Performs a timed {@link Thread#join(long, int) Thread.join}
325     * using this time unit.
326 dl 1.27 * This is a convenience method that converts time arguments into the
327 jsr166 1.40 * form required by the {@code Thread.join} method.
328 jsr166 1.37 *
329 dl 1.27 * @param thread the thread to wait for
330 dl 1.28 * @param timeout the maximum time to wait. If less than
331     * or equal to zero, do not wait at all.
332 jsr166 1.37 * @throws InterruptedException if interrupted while waiting
333 dl 1.27 */
334     public void timedJoin(Thread thread, long timeout)
335 jsr166 1.37 throws InterruptedException {
336 dl 1.27 if (timeout > 0) {
337     long ms = toMillis(timeout);
338     int ns = excessNanos(timeout, ms);
339     thread.join(ms, ns);
340     }
341     }
342 jsr166 1.29
343 dl 1.27 /**
344 jsr166 1.37 * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
345     * this time unit.
346 dl 1.27 * This is a convenience method that converts time arguments into the
347 jsr166 1.40 * form required by the {@code Thread.sleep} method.
348 jsr166 1.37 *
349 dl 1.28 * @param timeout the minimum time to sleep. If less than
350     * or equal to zero, do not sleep at all.
351 jsr166 1.37 * @throws InterruptedException if interrupted while sleeping
352 dl 1.27 */
353     public void sleep(long timeout) throws InterruptedException {
354     if (timeout > 0) {
355     long ms = toMillis(timeout);
356     int ns = excessNanos(timeout, ms);
357     Thread.sleep(ms, ns);
358     }
359     }
360 jsr166 1.29
361 tim 1.1 }