ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.41
Committed: Mon Feb 11 04:29:50 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +14 -14 lines
Log Message:
javadoc link readability

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