7 |
|
package java.util.concurrent; |
8 |
|
|
9 |
|
/** |
10 |
< |
* A <tt>TimeUnit</tt> 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. |
13 |
< |
* <tt>TimeUnit</tt> is a "featherweight" class. |
14 |
< |
* It does not maintain time information, but only helps organize and |
15 |
< |
* use time representations that may be maintained separately across |
10 |
> |
* A <tt>TimeUnit</tt> 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. |
13 |
> |
* <tt>TimeUnit</tt> is a "featherweight" class. |
14 |
> |
* It does not maintain time information, but only helps organize and |
15 |
> |
* use time representations that may be maintained separately across |
16 |
|
* various contexts. |
17 |
|
* |
18 |
< |
* <p>The <tt>TimeUnit</tt> class cannot be directly instantiated. |
18 |
> |
* <p>The <tt>TimeUnit</tt> class cannot be directly instantiated. |
19 |
|
* Use the {@link #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, |
20 |
|
* and {@link #NANOSECONDS} static instances that provide predefined |
21 |
|
* units of precision. If you use these frequently, consider |
23 |
|
* |
24 |
|
* <p>A <tt>TimeUnit</tt> is mainly used to inform blocking methods which |
25 |
|
* can timeout, how the timeout parameter should be interpreted. For example, |
26 |
< |
* the following code will timeout in 50 milliseconds if the {@link Lock lock} |
26 |
> |
* the following code will timeout in 50 milliseconds if the {@link java.util.concurrent.locks.Lock lock} |
27 |
|
* is not available: |
28 |
|
* <pre> Lock lock = ...; |
29 |
|
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
30 |
|
* </pre> |
31 |
|
* while this code will timeout in 50 seconds: |
32 |
< |
* <pre> |
32 |
> |
* <pre> |
33 |
|
* Lock lock = ...; |
34 |
|
* if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... |
35 |
|
* </pre> |
58 |
|
public long convert(long duration, TimeUnit unit) { |
59 |
|
if (unit == this) |
60 |
|
return duration; |
61 |
< |
if (index > unit.index) |
61 |
> |
if (index > unit.index) |
62 |
|
return duration / multipliers[index - unit.index]; |
63 |
< |
else |
63 |
> |
else |
64 |
|
return duration * multipliers[unit.index - index]; |
65 |
|
} |
66 |
|
|
75 |
|
else |
76 |
|
return duration * multipliers[index]; |
77 |
|
} |
78 |
< |
|
78 |
> |
|
79 |
|
/** |
80 |
< |
* Perform a timed <tt>Object.wait</tt> using the current time unit. |
80 |
> |
* Perform a timed <tt>Object.wait</tt> using the current time unit. |
81 |
|
* This is a convenience method that converts timeout arguments into the |
82 |
|
* form required by the <tt>Object.wait</tt> method. |
83 |
|
* <p>For example, you could implement a blocking <tt>poll</tt> method (see |
115 |
|
int ns = excessNanos(timeout, ms); |
116 |
|
thread.join(ms, ns); |
117 |
|
} |
118 |
< |
|
118 |
> |
|
119 |
|
/** |
120 |
|
* Perform a <tt>Thread.sleep</tt> using the current time unit. |
121 |
|
* This is a convenience method that converts time arguments into the |
122 |
|
* form required by the <tt>Thread.sleep</tt> method. |
123 |
< |
* @param timeout the minimum time to sleep |
123 |
> |
* @param timeout the minimum time to sleep |
124 |
|
* @throws InterruptedException if interrupted while sleeping. |
125 |
|
* @see Thread#sleep |
126 |
|
*/ |
145 |
|
/** private constructor */ |
146 |
|
TimeUnit(int index) { this.index = index; } |
147 |
|
|
148 |
< |
/** |
148 |
> |
/** |
149 |
|
* Utility method to compute the excess-nanosecond argument to |
150 |
|
* wait, sleep, join. |
151 |
|
* @fixme overflow? |
152 |
|
*/ |
153 |
|
private int excessNanos(long time, long ms) { |
154 |
< |
if (index == NS) |
154 |
> |
if (index == NS) |
155 |
|
return (int) (time - (ms * multipliers[MS-NS])); |
156 |
< |
else if (index == US) |
156 |
> |
else if (index == US) |
157 |
|
return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); |
158 |
< |
else |
158 |
> |
else |
159 |
|
return 0; |
160 |
|
} |
161 |
|
|