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/licenses/publicdomain |
5 |
< |
*/ |
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 <tt>TimeUnit</tt> represents time durations at a given unit of |
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 |
< |
* <tt>TimeUnit</tt> does not maintain time information, but only |
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 |
18 |
|
* as sixty seconds, an hour as sixty minutes, and a day as twenty four |
19 |
|
* hours. |
20 |
|
* |
21 |
< |
* <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods |
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> Lock lock = ...; |
27 |
< |
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
28 |
< |
* </pre> |
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> |
32 |
< |
* Lock lock = ...; |
33 |
< |
* if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... |
33 |
< |
* </pre> |
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 <tt>TimeUnit</tt>. |
37 |
> |
* same granularity as the given {@code TimeUnit}. |
38 |
|
* |
39 |
|
* @since 1.5 |
40 |
|
* @author Doug Lea |
41 |
|
*/ |
42 |
|
public enum TimeUnit { |
43 |
< |
NANOSECONDS (0) { |
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); } |
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 |
< |
MICROSECONDS (1) { |
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); } |
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 |
< |
MILLISECONDS (2) { |
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; } |
84 |
|
public long convert(long d, TimeUnit u) { return u.toMillis(d); } |
85 |
|
int excessNanos(long d, long m) { return 0; } |
86 |
|
}, |
87 |
< |
SECONDS (3) { |
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)); } |
99 |
|
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } |
100 |
|
int excessNanos(long d, long m) { return 0; } |
101 |
|
}, |
102 |
< |
MINUTES (4) { |
102 |
> |
|
103 |
> |
/** |
104 |
> |
* Time unit representing sixty seconds |
105 |
> |
* @since 1.6 |
106 |
> |
*/ |
107 |
> |
MINUTES { |
108 |
|
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)); } |
115 |
|
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } |
116 |
|
int excessNanos(long d, long m) { return 0; } |
117 |
|
}, |
118 |
< |
HOURS (5) { |
118 |
> |
|
119 |
> |
/** |
120 |
> |
* Time unit representing sixty minutes |
121 |
> |
* @since 1.6 |
122 |
> |
*/ |
123 |
> |
HOURS { |
124 |
|
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)); } |
131 |
|
public long convert(long d, TimeUnit u) { return u.toHours(d); } |
132 |
|
int excessNanos(long d, long m) { return 0; } |
133 |
|
}, |
134 |
< |
DAYS (6) { |
134 |
> |
|
135 |
> |
/** |
136 |
> |
* Time unit representing twenty four hours |
137 |
> |
* @since 1.6 |
138 |
> |
*/ |
139 |
> |
DAYS { |
140 |
|
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)); } |
147 |
|
public long convert(long d, TimeUnit u) { return u.toDays(d); } |
148 |
|
int excessNanos(long d, long m) { return 0; } |
149 |
|
}; |
150 |
< |
|
121 |
< |
/** |
122 |
< |
* The index of this unit. This value is no longer used in this |
123 |
< |
* version of this class, but is retained for serialization |
124 |
< |
* compatibility with previous version. |
125 |
< |
*/ |
126 |
< |
private final int index; |
127 |
< |
|
128 |
< |
/** Internal constructor */ |
129 |
< |
TimeUnit(int index) { |
130 |
< |
this.index = index; |
131 |
< |
} |
132 |
< |
|
150 |
> |
|
151 |
|
// Handy constants for conversion methods |
152 |
|
static final long C0 = 1L; |
153 |
|
static final long C1 = C0 * 1000L; |
156 |
|
static final long C4 = C3 * 60L; |
157 |
|
static final long C5 = C4 * 60L; |
158 |
|
static final long C6 = C5 * 24L; |
159 |
< |
|
159 |
> |
|
160 |
|
static final long MAX = Long.MAX_VALUE; |
161 |
< |
|
161 |
> |
|
162 |
|
/** |
163 |
|
* Scale d by m, checking for overflow. |
164 |
|
* This has a short name to make above code more readable. |
168 |
|
if (d < -over) return Long.MIN_VALUE; |
169 |
|
return d * m; |
170 |
|
} |
171 |
< |
|
171 |
> |
|
172 |
> |
// 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 |
> |
|
177 |
|
/** |
178 |
< |
* Convert the given time duration in the given unit to this |
179 |
< |
* unit. Conversions from finer to coarser granularities |
180 |
< |
* truncate, so lose precision. For example converting |
181 |
< |
* <tt>999</tt> milliseconds to seconds results in |
182 |
< |
* <tt>0</tt>. Conversions from coarser to finer granularities |
183 |
< |
* with arguments that would numerically overflow saturate to |
184 |
< |
* <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt> |
185 |
< |
* if positive. |
178 |
> |
* 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 |
> |
* |
186 |
> |
* <p>For example, to convert 10 minutes to milliseconds, use: |
187 |
> |
* {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)} |
188 |
|
* |
189 |
< |
* @param duration the time duration in the given <tt>unit</tt> |
190 |
< |
* @param unit the unit of the <tt>duration</tt> argument |
189 |
> |
* @param sourceDuration the time duration in the given {@code sourceUnit} |
190 |
> |
* @param sourceUnit the unit of the {@code sourceDuration} argument |
191 |
|
* @return the converted duration in this unit, |
192 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
193 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
192 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
193 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
194 |
|
*/ |
195 |
< |
public abstract long convert(long duration, TimeUnit unit); |
196 |
< |
|
195 |
> |
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
196 |
> |
throw new AbstractMethodError(); |
197 |
> |
} |
198 |
> |
|
199 |
|
/** |
200 |
< |
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>. |
200 |
> |
* Equivalent to |
201 |
> |
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
202 |
|
* @param duration the duration |
203 |
|
* @return the converted duration, |
204 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
205 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
178 |
< |
* @see #convert |
204 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
205 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
206 |
|
*/ |
207 |
< |
public abstract long toNanos(long duration); |
208 |
< |
|
207 |
> |
public long toNanos(long duration) { |
208 |
> |
throw new AbstractMethodError(); |
209 |
> |
} |
210 |
> |
|
211 |
|
/** |
212 |
< |
* Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>. |
212 |
> |
* Equivalent to |
213 |
> |
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
214 |
|
* @param duration the duration |
215 |
|
* @return the converted duration, |
216 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
217 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
188 |
< |
* @see #convert |
216 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
217 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
218 |
|
*/ |
219 |
< |
public abstract long toMicros(long duration); |
220 |
< |
|
219 |
> |
public long toMicros(long duration) { |
220 |
> |
throw new AbstractMethodError(); |
221 |
> |
} |
222 |
> |
|
223 |
|
/** |
224 |
< |
* Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>. |
224 |
> |
* Equivalent to |
225 |
> |
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
226 |
|
* @param duration the duration |
227 |
|
* @return the converted duration, |
228 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
229 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
198 |
< |
* @see #convert |
228 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
229 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
230 |
|
*/ |
231 |
< |
public abstract long toMillis(long duration); |
232 |
< |
|
231 |
> |
public long toMillis(long duration) { |
232 |
> |
throw new AbstractMethodError(); |
233 |
> |
} |
234 |
> |
|
235 |
|
/** |
236 |
< |
* Equivalent to <tt>SECONDS.convert(duration, this)</tt>. |
236 |
> |
* Equivalent to |
237 |
> |
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
238 |
|
* @param duration the duration |
239 |
|
* @return the converted duration, |
240 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
241 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
208 |
< |
* @see #convert |
240 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
241 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
242 |
|
*/ |
243 |
< |
public abstract long toSeconds(long duration); |
244 |
< |
|
243 |
> |
public long toSeconds(long duration) { |
244 |
> |
throw new AbstractMethodError(); |
245 |
> |
} |
246 |
> |
|
247 |
|
/** |
248 |
< |
* Equivalent to <tt>MINUTES.convert(duration, this)</tt>. |
248 |
> |
* Equivalent to |
249 |
> |
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
250 |
|
* @param duration the duration |
251 |
|
* @return the converted duration, |
252 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
253 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
254 |
< |
* @see #convert |
252 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
253 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
254 |
> |
* @since 1.6 |
255 |
|
*/ |
256 |
< |
public abstract long toMinutes(long duration); |
257 |
< |
|
256 |
> |
public long toMinutes(long duration) { |
257 |
> |
throw new AbstractMethodError(); |
258 |
> |
} |
259 |
> |
|
260 |
|
/** |
261 |
< |
* Equivalent to <tt>HOURS.convert(duration, this)</tt>. |
261 |
> |
* Equivalent to |
262 |
> |
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
263 |
|
* @param duration the duration |
264 |
|
* @return the converted duration, |
265 |
< |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
266 |
< |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
267 |
< |
* @see #convert |
265 |
> |
* or {@code Long.MIN_VALUE} if conversion would negatively |
266 |
> |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
267 |
> |
* @since 1.6 |
268 |
|
*/ |
269 |
< |
public abstract long toHours(long duration); |
270 |
< |
|
269 |
> |
public long toHours(long duration) { |
270 |
> |
throw new AbstractMethodError(); |
271 |
> |
} |
272 |
> |
|
273 |
|
/** |
274 |
< |
* Equivalent to <tt>DAYS.convert(duration, this)</tt>. |
274 |
> |
* Equivalent to |
275 |
> |
* {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}. |
276 |
|
* @param duration the duration |
277 |
|
* @return the converted duration |
278 |
< |
* @see #convert |
278 |
> |
* @since 1.6 |
279 |
|
*/ |
280 |
< |
public abstract long toDays(long duration); |
281 |
< |
|
280 |
> |
public long toDays(long duration) { |
281 |
> |
throw new AbstractMethodError(); |
282 |
> |
} |
283 |
> |
|
284 |
|
/** |
285 |
|
* Utility to compute the excess-nanosecond argument to wait, |
286 |
|
* sleep, join. |
289 |
|
* @return the number of nanoseconds |
290 |
|
*/ |
291 |
|
abstract int excessNanos(long d, long m); |
292 |
< |
|
292 |
> |
|
293 |
|
/** |
294 |
< |
* Perform a timed <tt>Object.wait</tt> using this time unit. |
294 |
> |
* Performs a timed {@link Object#wait(long, int) Object.wait} |
295 |
> |
* using this time unit. |
296 |
|
* This is a convenience method that converts timeout arguments |
297 |
< |
* into the form required by the <tt>Object.wait</tt> method. |
297 |
> |
* into the form required by the {@code Object.wait} method. |
298 |
|
* |
299 |
< |
* <p>For example, you could implement a blocking <tt>poll</tt> |
299 |
> |
* <p>For example, you could implement a blocking {@code poll} |
300 |
|
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
301 |
|
* using: |
302 |
|
* |
303 |
< |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
304 |
< |
* while (empty) { |
305 |
< |
* unit.timedWait(this, timeout); |
306 |
< |
* ... |
307 |
< |
* } |
308 |
< |
* }</pre> |
303 |
> |
* <pre> {@code |
304 |
> |
* public synchronized Object poll(long timeout, TimeUnit unit) |
305 |
> |
* throws InterruptedException { |
306 |
> |
* while (empty) { |
307 |
> |
* unit.timedWait(this, timeout); |
308 |
> |
* ... |
309 |
> |
* } |
310 |
> |
* }}</pre> |
311 |
|
* |
312 |
|
* @param obj the object to wait on |
313 |
< |
* @param timeout the maximum time to wait. |
314 |
< |
* @throws InterruptedException if interrupted while waiting. |
315 |
< |
* @see Object#wait(long, int) |
313 |
> |
* @param timeout the maximum time to wait. If less than |
314 |
> |
* or equal to zero, do not wait at all. |
315 |
> |
* @throws InterruptedException if interrupted while waiting |
316 |
|
*/ |
317 |
|
public void timedWait(Object obj, long timeout) |
318 |
< |
throws InterruptedException { |
318 |
> |
throws InterruptedException { |
319 |
|
if (timeout > 0) { |
320 |
|
long ms = toMillis(timeout); |
321 |
|
int ns = excessNanos(timeout, ms); |
322 |
|
obj.wait(ms, ns); |
323 |
|
} |
324 |
|
} |
325 |
< |
|
325 |
> |
|
326 |
|
/** |
327 |
< |
* Perform a timed <tt>Thread.join</tt> using this time unit. |
327 |
> |
* Performs a timed {@link Thread#join(long, int) Thread.join} |
328 |
> |
* using this time unit. |
329 |
|
* This is a convenience method that converts time arguments into the |
330 |
< |
* form required by the <tt>Thread.join</tt> method. |
330 |
> |
* form required by the {@code Thread.join} method. |
331 |
> |
* |
332 |
|
* @param thread the thread to wait for |
333 |
< |
* @param timeout the maximum time to wait |
334 |
< |
* @throws InterruptedException if interrupted while waiting. |
335 |
< |
* @see Thread#join(long, int) |
333 |
> |
* @param timeout the maximum time to wait. If less than |
334 |
> |
* or equal to zero, do not wait at all. |
335 |
> |
* @throws InterruptedException if interrupted while waiting |
336 |
|
*/ |
337 |
|
public void timedJoin(Thread thread, long timeout) |
338 |
< |
throws InterruptedException { |
338 |
> |
throws InterruptedException { |
339 |
|
if (timeout > 0) { |
340 |
|
long ms = toMillis(timeout); |
341 |
|
int ns = excessNanos(timeout, ms); |
342 |
|
thread.join(ms, ns); |
343 |
|
} |
344 |
|
} |
345 |
< |
|
345 |
> |
|
346 |
|
/** |
347 |
< |
* Perform a <tt>Thread.sleep</tt> using this unit. |
347 |
> |
* Performs a {@link Thread#sleep(long, int) Thread.sleep} using |
348 |
> |
* this time unit. |
349 |
|
* This is a convenience method that converts time arguments into the |
350 |
< |
* form required by the <tt>Thread.sleep</tt> method. |
351 |
< |
* @param timeout the minimum time to sleep |
352 |
< |
* @throws InterruptedException if interrupted while sleeping. |
353 |
< |
* @see Thread#sleep |
350 |
> |
* form required by the {@code Thread.sleep} method. |
351 |
> |
* |
352 |
> |
* @param timeout the minimum time to sleep. If less than |
353 |
> |
* or equal to zero, do not sleep at all. |
354 |
> |
* @throws InterruptedException if interrupted while sleeping |
355 |
|
*/ |
356 |
|
public void sleep(long timeout) throws InterruptedException { |
357 |
|
if (timeout > 0) { |
360 |
|
Thread.sleep(ms, ns); |
361 |
|
} |
362 |
|
} |
363 |
< |
|
363 |
> |
|
364 |
|
} |