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

# 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 /**
10 * A {@code TimeUnit} 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 * {@code TimeUnit} 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 {@code TimeUnit} 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> {@code
27 * Lock lock = ...;
28 * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre>
29 *
30 * while this code will timeout in 50 seconds:
31 * <pre> {@code
32 * Lock lock = ...;
33 * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</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 {@code TimeUnit}.
38 *
39 * @since 1.5
40 * @author Doug Lea
41 */
42 public enum TimeUnit {
43 /**
44 * Time unit representing one thousandth of a microsecond
45 */
46 NANOSECONDS {
47 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
58 /**
59 * Time unit representing one thousandth of a millisecond
60 */
61 MICROSECONDS {
62 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
73 /**
74 * Time unit representing one thousandth of a second
75 */
76 MILLISECONDS {
77 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
88 /**
89 * Time unit representing one second
90 */
91 SECONDS {
92 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
103 /**
104 * Time unit representing sixty seconds
105 */
106 MINUTES {
107 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
118 /**
119 * Time unit representing sixty minutes
120 */
121 HOURS {
122 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
133 /**
134 * Time unit representing twenty four hours
135 */
136 DAYS {
137 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
148 // 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
157 static final long MAX = Long.MAX_VALUE;
158
159 /**
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
169 // 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
174 /**
175 * 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 *
183 * <p>For example, to convert 10 minutes to milliseconds, use:
184 * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
185 *
186 * @param sourceDuration the time duration in the given {@code sourceUnit}
187 * @param sourceUnit the unit of the {@code sourceDuration} argument
188 * @return the converted duration in this unit,
189 * or {@code Long.MIN_VALUE} if conversion would negatively
190 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
191 */
192 public long convert(long sourceDuration, TimeUnit sourceUnit) {
193 throw new AbstractMethodError();
194 }
195
196 /**
197 * Equivalent to
198 * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
199 * @param duration the duration
200 * @return the converted duration,
201 * or {@code Long.MIN_VALUE} if conversion would negatively
202 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
203 */
204 public long toNanos(long duration) {
205 throw new AbstractMethodError();
206 }
207
208 /**
209 * Equivalent to
210 * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
211 * @param duration the duration
212 * @return the converted duration,
213 * or {@code Long.MIN_VALUE} if conversion would negatively
214 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
215 */
216 public long toMicros(long duration) {
217 throw new AbstractMethodError();
218 }
219
220 /**
221 * Equivalent to
222 * {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
223 * @param duration the duration
224 * @return the converted duration,
225 * or {@code Long.MIN_VALUE} if conversion would negatively
226 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
227 */
228 public long toMillis(long duration) {
229 throw new AbstractMethodError();
230 }
231
232 /**
233 * Equivalent to
234 * {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
235 * @param duration the duration
236 * @return the converted duration,
237 * or {@code Long.MIN_VALUE} if conversion would negatively
238 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
239 */
240 public long toSeconds(long duration) {
241 throw new AbstractMethodError();
242 }
243
244 /**
245 * Equivalent to
246 * {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
247 * @param duration the duration
248 * @return the converted duration,
249 * or {@code Long.MIN_VALUE} if conversion would negatively
250 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
251 * @since 1.6
252 */
253 public long toMinutes(long duration) {
254 throw new AbstractMethodError();
255 }
256
257 /**
258 * Equivalent to
259 * {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
260 * @param duration the duration
261 * @return the converted duration,
262 * or {@code Long.MIN_VALUE} if conversion would negatively
263 * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
264 * @since 1.6
265 */
266 public long toHours(long duration) {
267 throw new AbstractMethodError();
268 }
269
270 /**
271 * Equivalent to
272 * {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
273 * @param duration the duration
274 * @return the converted duration
275 * @since 1.6
276 */
277 public long toDays(long duration) {
278 throw new AbstractMethodError();
279 }
280
281 /**
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
290 /**
291 * Performs a timed {@link Object#wait(long, int) Object.wait}
292 * using this time unit.
293 * This is a convenience method that converts timeout arguments
294 * into the form required by the {@code Object.wait} method.
295 *
296 * <p>For example, you could implement a blocking {@code poll}
297 * method (see {@link BlockingQueue#poll BlockingQueue.poll})
298 * using:
299 *
300 * <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 *
309 * @param obj the object to wait on
310 * @param timeout the maximum time to wait. If less than
311 * or equal to zero, do not wait at all.
312 * @throws InterruptedException if interrupted while waiting
313 */
314 public void timedWait(Object obj, long timeout)
315 throws InterruptedException {
316 if (timeout > 0) {
317 long ms = toMillis(timeout);
318 int ns = excessNanos(timeout, ms);
319 obj.wait(ms, ns);
320 }
321 }
322
323 /**
324 * Performs a timed {@link Thread#join(long, int) Thread.join}
325 * using this time unit.
326 * This is a convenience method that converts time arguments into the
327 * form required by the {@code Thread.join} method.
328 *
329 * @param thread the thread to wait for
330 * @param timeout the maximum time to wait. If less than
331 * or equal to zero, do not wait at all.
332 * @throws InterruptedException if interrupted while waiting
333 */
334 public void timedJoin(Thread thread, long timeout)
335 throws InterruptedException {
336 if (timeout > 0) {
337 long ms = toMillis(timeout);
338 int ns = excessNanos(timeout, ms);
339 thread.join(ms, ns);
340 }
341 }
342
343 /**
344 * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
345 * this time unit.
346 * This is a convenience method that converts time arguments into the
347 * form required by the {@code Thread.sleep} method.
348 *
349 * @param timeout the minimum time to sleep. If less than
350 * or equal to zero, do not sleep at all.
351 * @throws InterruptedException if interrupted while sleeping
352 */
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
361 }