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 |
*/ |
6 |
|
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. A |
13 |
* <tt>TimeUnit</tt> does not maintain time information, but only |
14 |
* helps organize and use time representations that may be maintained |
15 |
* separately across various contexts. |
16 |
* |
17 |
* <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods |
18 |
* how a given timing parameter should be interpreted. For example, |
19 |
* the following code will timeout in 50 milliseconds if the {@link |
20 |
* java.util.concurrent.locks.Lock lock} is not available: |
21 |
* |
22 |
* <pre> Lock lock = ...; |
23 |
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
24 |
* </pre> |
25 |
* while this code will timeout in 50 seconds: |
26 |
* <pre> |
27 |
* Lock lock = ...; |
28 |
* if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... |
29 |
* </pre> |
30 |
* |
31 |
* Note however, that there is no guarantee that a particular timeout |
32 |
* implementation will be able to notice the passage of time at the |
33 |
* same granularity as the given <tt>TimeUnit</tt>. |
34 |
* |
35 |
* @since 1.5 |
36 |
* @author Doug Lea |
37 |
*/ |
38 |
public enum TimeUnit { |
39 |
NANOSECONDS(0), MICROSECONDS(1), MILLISECONDS(2), SECONDS(3); |
40 |
|
41 |
/** the index of this unit */ |
42 |
private final int index; |
43 |
|
44 |
/** Internal constructor */ |
45 |
TimeUnit(int index) { |
46 |
this.index = index; |
47 |
} |
48 |
|
49 |
/** Lookup table for conversion factors */ |
50 |
private static final int[] multipliers = { |
51 |
1, |
52 |
1000, |
53 |
1000 * 1000, |
54 |
1000 * 1000 * 1000 |
55 |
}; |
56 |
|
57 |
/** |
58 |
* Lookup table to check saturation. Note that because we are |
59 |
* dividing these down, we don't have to deal with asymmetry of |
60 |
* MIN/MAX values. |
61 |
*/ |
62 |
private static final long[] overflows = { |
63 |
0, // unused |
64 |
Long.MAX_VALUE / 1000, |
65 |
Long.MAX_VALUE / (1000 * 1000), |
66 |
Long.MAX_VALUE / (1000 * 1000 * 1000) |
67 |
}; |
68 |
|
69 |
/** |
70 |
* Perform conversion based on given delta representing the |
71 |
* difference between units |
72 |
* @param delta the difference in index values of source and target units |
73 |
* @param duration the duration |
74 |
* @return converted duration or saturated value |
75 |
*/ |
76 |
private static long doConvert(int delta, long duration) { |
77 |
if (delta == 0) |
78 |
return duration; |
79 |
if (delta < 0) |
80 |
return duration / multipliers[-delta]; |
81 |
if (duration > overflows[delta]) |
82 |
return Long.MAX_VALUE; |
83 |
if (duration < -overflows[delta]) |
84 |
return Long.MIN_VALUE; |
85 |
return duration * multipliers[delta]; |
86 |
} |
87 |
|
88 |
/** |
89 |
* Convert the given time duration in the given unit to this |
90 |
* unit. Conversions from finer to coarser granularities |
91 |
* truncate, so lose precision. For example converting |
92 |
* <tt>999</tt> milliseconds to seconds results in |
93 |
* <tt>0</tt>. Conversions from coarser to finer granularities |
94 |
* with arguments that would numerically overflow saturate to |
95 |
* <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt> |
96 |
* if positive. |
97 |
* |
98 |
* @param duration the time duration in the given <tt>unit</tt> |
99 |
* @param unit the unit of the <tt>duration</tt> argument |
100 |
* @return the converted duration in this unit, |
101 |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
102 |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
103 |
*/ |
104 |
public long convert(long duration, TimeUnit unit) { |
105 |
return doConvert(unit.index - index, duration); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>. |
110 |
* @param duration the duration |
111 |
* @return the converted duration, |
112 |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
113 |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
114 |
* @see #convert |
115 |
*/ |
116 |
public long toNanos(long duration) { |
117 |
return doConvert(index, duration); |
118 |
} |
119 |
|
120 |
/** |
121 |
* Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>. |
122 |
* @param duration the duration |
123 |
* @return the converted duration, |
124 |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
125 |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
126 |
* @see #convert |
127 |
*/ |
128 |
public long toMicros(long duration) { |
129 |
return doConvert(index - MICROSECONDS.index, duration); |
130 |
} |
131 |
|
132 |
/** |
133 |
* Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>. |
134 |
* @param duration the duration |
135 |
* @return the converted duration, |
136 |
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively |
137 |
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow. |
138 |
* @see #convert |
139 |
*/ |
140 |
public long toMillis(long duration) { |
141 |
return doConvert(index - MILLISECONDS.index, duration); |
142 |
} |
143 |
|
144 |
/** |
145 |
* Equivalent to <tt>SECONDS.convert(duration, this)</tt>. |
146 |
* @param duration the duration |
147 |
* @return the converted duration. |
148 |
* @see #convert |
149 |
*/ |
150 |
public long toSeconds(long duration) { |
151 |
return doConvert(index - SECONDS.index, duration); |
152 |
} |
153 |
|
154 |
|
155 |
/** |
156 |
* Utility method to compute the excess-nanosecond argument to |
157 |
* wait, sleep, join. |
158 |
*/ |
159 |
private int excessNanos(long time, long ms) { |
160 |
if (this == NANOSECONDS) |
161 |
return (int) (time - (ms * 1000 * 1000)); |
162 |
if (this == MICROSECONDS) |
163 |
return (int) ((time * 1000) - (ms * 1000 * 1000)); |
164 |
return 0; |
165 |
} |
166 |
|
167 |
/** |
168 |
* Perform a timed <tt>Object.wait</tt> using this time unit. |
169 |
* This is a convenience method that converts timeout arguments |
170 |
* into the form required by the <tt>Object.wait</tt> method. |
171 |
* |
172 |
* <p>For example, you could implement a blocking <tt>poll</tt> |
173 |
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
174 |
* using: |
175 |
* |
176 |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
177 |
* while (empty) { |
178 |
* unit.timedWait(this, timeout); |
179 |
* ... |
180 |
* } |
181 |
* }</pre> |
182 |
* |
183 |
* @param obj the object to wait on |
184 |
* @param timeout the maximum time to wait. |
185 |
* @throws InterruptedException if interrupted while waiting. |
186 |
* @see Object#wait(long, int) |
187 |
*/ |
188 |
public void timedWait(Object obj, long timeout) |
189 |
throws InterruptedException { |
190 |
if (timeout > 0) { |
191 |
long ms = toMillis(timeout); |
192 |
int ns = excessNanos(timeout, ms); |
193 |
obj.wait(ms, ns); |
194 |
} |
195 |
} |
196 |
|
197 |
/** |
198 |
* Perform a timed <tt>Thread.join</tt> using this time unit. |
199 |
* This is a convenience method that converts time arguments into the |
200 |
* form required by the <tt>Thread.join</tt> method. |
201 |
* @param thread the thread to wait for |
202 |
* @param timeout the maximum time to wait |
203 |
* @throws InterruptedException if interrupted while waiting. |
204 |
* @see Thread#join(long, int) |
205 |
*/ |
206 |
public void timedJoin(Thread thread, long timeout) |
207 |
throws InterruptedException { |
208 |
if (timeout > 0) { |
209 |
long ms = toMillis(timeout); |
210 |
int ns = excessNanos(timeout, ms); |
211 |
thread.join(ms, ns); |
212 |
} |
213 |
} |
214 |
|
215 |
/** |
216 |
* Perform a <tt>Thread.sleep</tt> using this unit. |
217 |
* This is a convenience method that converts time arguments into the |
218 |
* form required by the <tt>Thread.sleep</tt> method. |
219 |
* @param timeout the minimum time to sleep |
220 |
* @throws InterruptedException if interrupted while sleeping. |
221 |
* @see Thread#sleep |
222 |
*/ |
223 |
public void sleep(long timeout) throws InterruptedException { |
224 |
if (timeout > 0) { |
225 |
long ms = toMillis(timeout); |
226 |
int ns = excessNanos(timeout, ms); |
227 |
Thread.sleep(ms, ns); |
228 |
} |
229 |
} |
230 |
|
231 |
} |