ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.49
Committed: Wed Mar 23 14:03:06 2016 UTC (8 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.48: +75 -118 lines
Log Message:
Avoid megamorphic dispatch

File Contents

# Content
1 /*
2 * 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/publicdomain/zero/1.0/
5 */
6
7 package java.util.concurrent;
8
9 import java.time.temporal.ChronoUnit;
10 import java.util.Objects;
11
12 /**
13 * A {@code TimeUnit} represents time durations at a given unit of
14 * granularity and provides utility methods to convert across units,
15 * and to perform timing and delay operations in these units. A
16 * {@code TimeUnit} does not maintain time information, but only
17 * helps organize and use time representations that may be maintained
18 * separately across various contexts. A nanosecond is defined as one
19 * thousandth of a microsecond, a microsecond as one thousandth of a
20 * millisecond, a millisecond as one thousandth of a second, a minute
21 * as sixty seconds, an hour as sixty minutes, and a day as twenty four
22 * hours.
23 *
24 * <p>A {@code TimeUnit} is mainly used to inform time-based methods
25 * how a given timing parameter should be interpreted. For example,
26 * the following code will timeout in 50 milliseconds if the {@link
27 * java.util.concurrent.locks.Lock lock} is not available:
28 *
29 * <pre> {@code
30 * Lock lock = ...;
31 * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre>
32 *
33 * while this code will timeout in 50 seconds:
34 * <pre> {@code
35 * Lock lock = ...;
36 * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre>
37 *
38 * Note however, that there is no guarantee that a particular timeout
39 * implementation will be able to notice the passage of time at the
40 * same granularity as the given {@code TimeUnit}.
41 *
42 * @since 1.5
43 * @author Doug Lea
44 */
45 public enum TimeUnit {
46 /**
47 * Time unit representing one thousandth of a microsecond.
48 */
49 NANOSECONDS(1L), // (cannot use symbolic scale names here)
50 /**
51 * Time unit representing one thousandth of a millisecond.
52 */
53 MICROSECONDS(1000L),
54 /**
55 * Time unit representing one thousandth of a second.
56 */
57 MILLISECONDS(1000L * 1000L),
58 /**
59 * Time unit representing one second.
60 */
61 SECONDS(1000L * 1000L * 1000L),
62 /**
63 * Time unit representing sixty seconds.
64 * @since 1.6
65 */
66 MINUTES(1000L * 1000L * 1000L * 60L),
67 /**
68 * Time unit representing sixty minutes.
69 * @since 1.6
70 */
71 HOURS(1000L * 1000L * 1000L * 60L * 60L),
72 /**
73 * Time unit representing twenty four hours.
74 * @since 1.6
75 */
76 DAYS(1000L * 1000L * 1000L * 60L * 60L * 24L);
77
78 // Scales as constants
79 private static final long NANO_SCALE = 1L;
80 private static final long MICRO_SCALE = 1000L * NANO_SCALE;
81 private static final long MILLI_SCALE = 1000L * MICRO_SCALE;
82 private static final long SECOND_SCALE = 1000L * MILLI_SCALE;
83 private static final long MINUTE_SCALE = 60L * SECOND_SCALE;
84 private static final long HOUR_SCALE = 60L * MINUTE_SCALE;
85 private static final long DAY_SCALE = 24L * HOUR_SCALE;
86
87 /*
88 * Instances cache conversion ratios and saturation cutoffs for
89 * the two units commonly used for JDK timing, used in methods
90 * toNanos and toMillis. Other cases compute them, in method cvt.
91 */
92
93 private final long scale;
94 private final long maxNanos;
95 private final long millisRatio;
96 private final long maxMillis;
97
98 TimeUnit(long scale) {
99 this.scale = scale;
100 this.maxNanos = Long.MAX_VALUE / scale;
101 long r = (scale >= MILLI_SCALE ? scale / MILLI_SCALE :
102 MILLI_SCALE / scale);
103 this.millisRatio = r;
104 this.maxMillis = Long.MAX_VALUE / r;
105 }
106
107 /**
108 * General conversion utility.
109 *
110 * @param d duration
111 * @param dst result scale unit
112 * @param src source scale unit
113 */
114 private static long cvt(long d, long dst, long src) {
115 long r, m;
116 return ((src == dst) ? d :
117 (src < dst) ? d / (dst / src) :
118 (d > (m = Long.MAX_VALUE / (r = src / dst))) ? Long.MAX_VALUE :
119 (d < -m) ? Long.MIN_VALUE :
120 d * r);
121 }
122
123 /**
124 * Converts the given time duration in the given unit to this unit.
125 * Conversions from finer to coarser granularities truncate, so
126 * lose precision. For example, converting {@code 999} milliseconds
127 * to seconds results in {@code 0}. Conversions from coarser to
128 * finer granularities with arguments that would numerically
129 * overflow saturate to {@code Long.MIN_VALUE} if negative or
130 * {@code Long.MAX_VALUE} if positive.
131 *
132 * <p>For example, to convert 10 minutes to milliseconds, use:
133 * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
134 *
135 * @param sourceDuration the time duration in the given {@code sourceUnit}
136 * @param sourceUnit the unit of the {@code sourceDuration} argument
137 * @return the converted duration in this unit,
138 * or {@code Long.MIN_VALUE} if conversion would negatively
139 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
140 */
141 public long convert(long sourceDuration, TimeUnit sourceUnit) {
142 return cvt(sourceDuration, scale, sourceUnit.scale);
143 }
144
145 /**
146 * Equivalent to
147 * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
148 * @param duration the duration
149 * @return the converted duration,
150 * or {@code Long.MIN_VALUE} if conversion would negatively
151 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
152 */
153 public long toNanos(long duration) {
154 long s, m;
155 return (((s = scale) == NANO_SCALE) ? duration :
156 (duration > (m = maxNanos)) ? Long.MAX_VALUE :
157 (duration < -m) ? Long.MIN_VALUE :
158 duration * s);
159 }
160
161 /**
162 * Equivalent to
163 * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
164 * @param duration the duration
165 * @return the converted duration,
166 * or {@code Long.MIN_VALUE} if conversion would negatively
167 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
168 */
169 public long toMicros(long duration) {
170 return cvt(duration, MICRO_SCALE, scale);
171 }
172
173 /**
174 * Equivalent to
175 * {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
176 * @param duration the duration
177 * @return the converted duration,
178 * or {@code Long.MIN_VALUE} if conversion would negatively
179 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
180 */
181 public long toMillis(long duration) {
182 long s, m;
183 return ( ((s = scale) == MILLI_SCALE) ? duration :
184 (s < MILLI_SCALE) ? duration / millisRatio :
185 (duration > (m = maxMillis)) ? Long.MAX_VALUE :
186 (duration < -m) ? Long.MIN_VALUE :
187 duration * millisRatio);
188 }
189
190 /**
191 * Equivalent to
192 * {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
193 * @param duration the duration
194 * @return the converted duration,
195 * or {@code Long.MIN_VALUE} if conversion would negatively
196 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
197 */
198 public long toSeconds(long duration) {
199 return cvt(duration, SECOND_SCALE, scale);
200 }
201
202 /**
203 * Equivalent to
204 * {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
205 * @param duration the duration
206 * @return the converted duration,
207 * or {@code Long.MIN_VALUE} if conversion would negatively
208 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
209 * @since 1.6
210 */
211 public long toMinutes(long duration) {
212 return cvt(duration, MINUTE_SCALE, scale);
213 }
214
215 /**
216 * Equivalent to
217 * {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
218 * @param duration the duration
219 * @return the converted duration,
220 * or {@code Long.MIN_VALUE} if conversion would negatively
221 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
222 * @since 1.6
223 */
224 public long toHours(long duration) {
225 return cvt(duration, HOUR_SCALE, scale);
226 }
227
228 /**
229 * Equivalent to
230 * {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
231 * @param duration the duration
232 * @return the converted duration
233 * @since 1.6
234 */
235 public long toDays(long duration) {
236 return cvt(duration, DAY_SCALE, scale);
237 }
238
239 /**
240 * Utility to compute the excess-nanosecond argument to wait,
241 * sleep, join.
242 * @param d the duration
243 * @param m the number of milliseconds
244 * @return the number of nanoseconds
245 */
246 private int excessNanos(long d, long m) {
247 long s;
248 return (((s = scale) == NANO_SCALE) ? (int)(d - (m * MILLI_SCALE)) :
249 (s == MICRO_SCALE) ? (int)((d * 1000L) - (m * MILLI_SCALE)) :
250 0);
251 }
252
253 /**
254 * Performs a timed {@link Object#wait(long, int) Object.wait}
255 * using this time unit.
256 * This is a convenience method that converts timeout arguments
257 * into the form required by the {@code Object.wait} method.
258 *
259 * <p>For example, you could implement a blocking {@code poll}
260 * method (see {@link BlockingQueue#poll BlockingQueue.poll})
261 * using:
262 *
263 * <pre> {@code
264 * public synchronized Object poll(long timeout, TimeUnit unit)
265 * throws InterruptedException {
266 * while (empty) {
267 * unit.timedWait(this, timeout);
268 * ...
269 * }
270 * }}</pre>
271 *
272 * @param obj the object to wait on
273 * @param timeout the maximum time to wait. If less than
274 * or equal to zero, do not wait at all.
275 * @throws InterruptedException if interrupted while waiting
276 */
277 public void timedWait(Object obj, long timeout)
278 throws InterruptedException {
279 if (timeout > 0) {
280 long ms = toMillis(timeout);
281 int ns = excessNanos(timeout, ms);
282 obj.wait(ms, ns);
283 }
284 }
285
286 /**
287 * Performs a timed {@link Thread#join(long, int) Thread.join}
288 * using this time unit.
289 * This is a convenience method that converts time arguments into the
290 * form required by the {@code Thread.join} method.
291 *
292 * @param thread the thread to wait for
293 * @param timeout the maximum time to wait. If less than
294 * or equal to zero, do not wait at all.
295 * @throws InterruptedException if interrupted while waiting
296 */
297 public void timedJoin(Thread thread, long timeout)
298 throws InterruptedException {
299 if (timeout > 0) {
300 long ms = toMillis(timeout);
301 int ns = excessNanos(timeout, ms);
302 thread.join(ms, ns);
303 }
304 }
305
306 /**
307 * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
308 * this time unit.
309 * This is a convenience method that converts time arguments into the
310 * form required by the {@code Thread.sleep} method.
311 *
312 * @param timeout the minimum time to sleep. If less than
313 * or equal to zero, do not sleep at all.
314 * @throws InterruptedException if interrupted while sleeping
315 */
316 public void sleep(long timeout) throws InterruptedException {
317 if (timeout > 0) {
318 long ms = toMillis(timeout);
319 int ns = excessNanos(timeout, ms);
320 Thread.sleep(ms, ns);
321 }
322 }
323
324 /**
325 * Converts this {@code TimeUnit} to the equivalent {@code ChronoUnit}.
326 *
327 * @return the converted equivalent ChronoUnit
328 * @since 9
329 */
330 public ChronoUnit toChronoUnit() {
331 switch (this) {
332 case NANOSECONDS: return ChronoUnit.NANOS;
333 case MICROSECONDS: return ChronoUnit.MICROS;
334 case MILLISECONDS: return ChronoUnit.MILLIS;
335 case SECONDS: return ChronoUnit.SECONDS;
336 case MINUTES: return ChronoUnit.MINUTES;
337 case HOURS: return ChronoUnit.HOURS;
338 case DAYS: return ChronoUnit.DAYS;
339 default: throw new AssertionError();
340 }
341 }
342
343 /**
344 * Converts a {@code ChronoUnit} to the equivalent {@code TimeUnit}.
345 *
346 * @param chronoUnit the ChronoUnit to convert
347 * @return the converted equivalent TimeUnit
348 * @throws IllegalArgumentException if {@code chronoUnit} has no
349 * equivalent TimeUnit
350 * @throws NullPointerException if {@code chronoUnit} is null
351 * @since 9
352 */
353 public static TimeUnit of(ChronoUnit chronoUnit) {
354 switch (Objects.requireNonNull(chronoUnit, "chronoUnit")) {
355 case NANOS: return TimeUnit.NANOSECONDS;
356 case MICROS: return TimeUnit.MICROSECONDS;
357 case MILLIS: return TimeUnit.MILLISECONDS;
358 case SECONDS: return TimeUnit.SECONDS;
359 case MINUTES: return TimeUnit.MINUTES;
360 case HOURS: return TimeUnit.HOURS;
361 case DAYS: return TimeUnit.DAYS;
362 default:
363 throw new IllegalArgumentException(
364 "No TimeUnit equivalent for " + chronoUnit);
365 }
366 }
367
368 }