author | sla |
Thu, 05 Feb 2015 12:59:01 +0100 | |
changeset 28777 | 01aad659d989 |
parent 25859 | 3317bb8137f4 |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
/** |
|
18768 | 39 |
* A {@code TimeUnit} represents time durations at a given unit of |
2 | 40 |
* granularity and provides utility methods to convert across units, |
41 |
* and to perform timing and delay operations in these units. A |
|
18768 | 42 |
* {@code TimeUnit} does not maintain time information, but only |
2 | 43 |
* helps organize and use time representations that may be maintained |
44 |
* separately across various contexts. A nanosecond is defined as one |
|
45 |
* thousandth of a microsecond, a microsecond as one thousandth of a |
|
46 |
* millisecond, a millisecond as one thousandth of a second, a minute |
|
47 |
* as sixty seconds, an hour as sixty minutes, and a day as twenty four |
|
48 |
* hours. |
|
49 |
* |
|
18768 | 50 |
* <p>A {@code TimeUnit} is mainly used to inform time-based methods |
2 | 51 |
* how a given timing parameter should be interpreted. For example, |
52 |
* the following code will timeout in 50 milliseconds if the {@link |
|
53 |
* java.util.concurrent.locks.Lock lock} is not available: |
|
54 |
* |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
55 |
* <pre> {@code |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
56 |
* Lock lock = ...; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
57 |
* if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre> |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
58 |
* |
2 | 59 |
* while this code will timeout in 50 seconds: |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
60 |
* <pre> {@code |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
61 |
* Lock lock = ...; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
62 |
* if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre> |
2 | 63 |
* |
64 |
* Note however, that there is no guarantee that a particular timeout |
|
65 |
* implementation will be able to notice the passage of time at the |
|
18768 | 66 |
* same granularity as the given {@code TimeUnit}. |
2 | 67 |
* |
68 |
* @since 1.5 |
|
69 |
* @author Doug Lea |
|
70 |
*/ |
|
71 |
public enum TimeUnit { |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
72 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
73 |
* Time unit representing one thousandth of a microsecond |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
74 |
*/ |
2 | 75 |
NANOSECONDS { |
76 |
public long toNanos(long d) { return d; } |
|
77 |
public long toMicros(long d) { return d/(C1/C0); } |
|
78 |
public long toMillis(long d) { return d/(C2/C0); } |
|
79 |
public long toSeconds(long d) { return d/(C3/C0); } |
|
80 |
public long toMinutes(long d) { return d/(C4/C0); } |
|
81 |
public long toHours(long d) { return d/(C5/C0); } |
|
82 |
public long toDays(long d) { return d/(C6/C0); } |
|
83 |
public long convert(long d, TimeUnit u) { return u.toNanos(d); } |
|
84 |
int excessNanos(long d, long m) { return (int)(d - (m*C2)); } |
|
85 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
86 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
87 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
88 |
* Time unit representing one thousandth of a millisecond |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
89 |
*/ |
2 | 90 |
MICROSECONDS { |
91 |
public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } |
|
92 |
public long toMicros(long d) { return d; } |
|
93 |
public long toMillis(long d) { return d/(C2/C1); } |
|
94 |
public long toSeconds(long d) { return d/(C3/C1); } |
|
95 |
public long toMinutes(long d) { return d/(C4/C1); } |
|
96 |
public long toHours(long d) { return d/(C5/C1); } |
|
97 |
public long toDays(long d) { return d/(C6/C1); } |
|
98 |
public long convert(long d, TimeUnit u) { return u.toMicros(d); } |
|
99 |
int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } |
|
100 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
101 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
102 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
103 |
* Time unit representing one thousandth of a second |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
104 |
*/ |
2 | 105 |
MILLISECONDS { |
106 |
public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } |
|
107 |
public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } |
|
108 |
public long toMillis(long d) { return d; } |
|
109 |
public long toSeconds(long d) { return d/(C3/C2); } |
|
110 |
public long toMinutes(long d) { return d/(C4/C2); } |
|
111 |
public long toHours(long d) { return d/(C5/C2); } |
|
112 |
public long toDays(long d) { return d/(C6/C2); } |
|
113 |
public long convert(long d, TimeUnit u) { return u.toMillis(d); } |
|
114 |
int excessNanos(long d, long m) { return 0; } |
|
115 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
116 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
117 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
118 |
* Time unit representing one second |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
119 |
*/ |
2 | 120 |
SECONDS { |
121 |
public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } |
|
122 |
public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } |
|
123 |
public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } |
|
124 |
public long toSeconds(long d) { return d; } |
|
125 |
public long toMinutes(long d) { return d/(C4/C3); } |
|
126 |
public long toHours(long d) { return d/(C5/C3); } |
|
127 |
public long toDays(long d) { return d/(C6/C3); } |
|
128 |
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } |
|
129 |
int excessNanos(long d, long m) { return 0; } |
|
130 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
131 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
132 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
133 |
* Time unit representing sixty seconds |
28777 | 134 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
135 |
*/ |
2 | 136 |
MINUTES { |
137 |
public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } |
|
138 |
public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } |
|
139 |
public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); } |
|
140 |
public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); } |
|
141 |
public long toMinutes(long d) { return d; } |
|
142 |
public long toHours(long d) { return d/(C5/C4); } |
|
143 |
public long toDays(long d) { return d/(C6/C4); } |
|
144 |
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } |
|
145 |
int excessNanos(long d, long m) { return 0; } |
|
146 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
147 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
148 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
149 |
* Time unit representing sixty minutes |
28777 | 150 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
151 |
*/ |
2 | 152 |
HOURS { |
153 |
public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } |
|
154 |
public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } |
|
155 |
public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); } |
|
156 |
public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); } |
|
157 |
public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); } |
|
158 |
public long toHours(long d) { return d; } |
|
159 |
public long toDays(long d) { return d/(C6/C5); } |
|
160 |
public long convert(long d, TimeUnit u) { return u.toHours(d); } |
|
161 |
int excessNanos(long d, long m) { return 0; } |
|
162 |
}, |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
163 |
|
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
164 |
/** |
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
165 |
* Time unit representing twenty four hours |
28777 | 166 |
* @since 1.6 |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18768
diff
changeset
|
167 |
*/ |
2 | 168 |
DAYS { |
169 |
public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } |
|
170 |
public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } |
|
171 |
public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); } |
|
172 |
public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); } |
|
173 |
public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); } |
|
174 |
public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); } |
|
175 |
public long toDays(long d) { return d; } |
|
176 |
public long convert(long d, TimeUnit u) { return u.toDays(d); } |
|
177 |
int excessNanos(long d, long m) { return 0; } |
|
178 |
}; |
|
179 |
||
180 |
// Handy constants for conversion methods |
|
181 |
static final long C0 = 1L; |
|
182 |
static final long C1 = C0 * 1000L; |
|
183 |
static final long C2 = C1 * 1000L; |
|
184 |
static final long C3 = C2 * 1000L; |
|
185 |
static final long C4 = C3 * 60L; |
|
186 |
static final long C5 = C4 * 60L; |
|
187 |
static final long C6 = C5 * 24L; |
|
188 |
||
189 |
static final long MAX = Long.MAX_VALUE; |
|
190 |
||
191 |
/** |
|
192 |
* Scale d by m, checking for overflow. |
|
193 |
* This has a short name to make above code more readable. |
|
194 |
*/ |
|
195 |
static long x(long d, long m, long over) { |
|
196 |
if (d > over) return Long.MAX_VALUE; |
|
197 |
if (d < -over) return Long.MIN_VALUE; |
|
198 |
return d * m; |
|
199 |
} |
|
200 |
||
201 |
// To maintain full signature compatibility with 1.5, and to improve the |
|
202 |
// clarity of the generated javadoc (see 6287639: Abstract methods in |
|
203 |
// enum classes should not be listed as abstract), method convert |
|
204 |
// etc. are not declared abstract but otherwise act as abstract methods. |
|
205 |
||
206 |
/** |
|
18768 | 207 |
* Converts the given time duration in the given unit to this unit. |
208 |
* Conversions from finer to coarser granularities truncate, so |
|
209 |
* lose precision. For example, converting {@code 999} milliseconds |
|
210 |
* to seconds results in {@code 0}. Conversions from coarser to |
|
211 |
* finer granularities with arguments that would numerically |
|
212 |
* overflow saturate to {@code Long.MIN_VALUE} if negative or |
|
213 |
* {@code Long.MAX_VALUE} if positive. |
|
2 | 214 |
* |
215 |
* <p>For example, to convert 10 minutes to milliseconds, use: |
|
18768 | 216 |
* {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)} |
2 | 217 |
* |
18768 | 218 |
* @param sourceDuration the time duration in the given {@code sourceUnit} |
219 |
* @param sourceUnit the unit of the {@code sourceDuration} argument |
|
2 | 220 |
* @return the converted duration in this unit, |
18768 | 221 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
222 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 223 |
*/ |
224 |
public long convert(long sourceDuration, TimeUnit sourceUnit) { |
|
225 |
throw new AbstractMethodError(); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
18768 | 229 |
* Equivalent to |
230 |
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}. |
|
2 | 231 |
* @param duration the duration |
232 |
* @return the converted duration, |
|
18768 | 233 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
234 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 235 |
*/ |
236 |
public long toNanos(long duration) { |
|
237 |
throw new AbstractMethodError(); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
18768 | 241 |
* Equivalent to |
242 |
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}. |
|
2 | 243 |
* @param duration the duration |
244 |
* @return the converted duration, |
|
18768 | 245 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
246 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 247 |
*/ |
248 |
public long toMicros(long duration) { |
|
249 |
throw new AbstractMethodError(); |
|
250 |
} |
|
251 |
||
252 |
/** |
|
18768 | 253 |
* Equivalent to |
254 |
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}. |
|
2 | 255 |
* @param duration the duration |
256 |
* @return the converted duration, |
|
18768 | 257 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
258 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 259 |
*/ |
260 |
public long toMillis(long duration) { |
|
261 |
throw new AbstractMethodError(); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
18768 | 265 |
* Equivalent to |
266 |
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}. |
|
2 | 267 |
* @param duration the duration |
268 |
* @return the converted duration, |
|
18768 | 269 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
270 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 271 |
*/ |
272 |
public long toSeconds(long duration) { |
|
273 |
throw new AbstractMethodError(); |
|
274 |
} |
|
275 |
||
276 |
/** |
|
18768 | 277 |
* Equivalent to |
278 |
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}. |
|
2 | 279 |
* @param duration the duration |
280 |
* @return the converted duration, |
|
18768 | 281 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
282 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 283 |
* @since 1.6 |
284 |
*/ |
|
285 |
public long toMinutes(long duration) { |
|
286 |
throw new AbstractMethodError(); |
|
287 |
} |
|
288 |
||
289 |
/** |
|
18768 | 290 |
* Equivalent to |
291 |
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}. |
|
2 | 292 |
* @param duration the duration |
293 |
* @return the converted duration, |
|
18768 | 294 |
* or {@code Long.MIN_VALUE} if conversion would negatively |
295 |
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow. |
|
2 | 296 |
* @since 1.6 |
297 |
*/ |
|
298 |
public long toHours(long duration) { |
|
299 |
throw new AbstractMethodError(); |
|
300 |
} |
|
301 |
||
302 |
/** |
|
18768 | 303 |
* Equivalent to |
304 |
* {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}. |
|
2 | 305 |
* @param duration the duration |
306 |
* @return the converted duration |
|
307 |
* @since 1.6 |
|
308 |
*/ |
|
309 |
public long toDays(long duration) { |
|
310 |
throw new AbstractMethodError(); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Utility to compute the excess-nanosecond argument to wait, |
|
315 |
* sleep, join. |
|
316 |
* @param d the duration |
|
317 |
* @param m the number of milliseconds |
|
318 |
* @return the number of nanoseconds |
|
319 |
*/ |
|
320 |
abstract int excessNanos(long d, long m); |
|
321 |
||
322 |
/** |
|
7518 | 323 |
* Performs a timed {@link Object#wait(long, int) Object.wait} |
324 |
* using this time unit. |
|
2 | 325 |
* This is a convenience method that converts timeout arguments |
18768 | 326 |
* into the form required by the {@code Object.wait} method. |
2 | 327 |
* |
18768 | 328 |
* <p>For example, you could implement a blocking {@code poll} |
2 | 329 |
* method (see {@link BlockingQueue#poll BlockingQueue.poll}) |
330 |
* using: |
|
331 |
* |
|
7518 | 332 |
* <pre> {@code |
333 |
* public synchronized Object poll(long timeout, TimeUnit unit) |
|
334 |
* throws InterruptedException { |
|
335 |
* while (empty) { |
|
336 |
* unit.timedWait(this, timeout); |
|
337 |
* ... |
|
338 |
* } |
|
339 |
* }}</pre> |
|
2 | 340 |
* |
341 |
* @param obj the object to wait on |
|
342 |
* @param timeout the maximum time to wait. If less than |
|
343 |
* or equal to zero, do not wait at all. |
|
7518 | 344 |
* @throws InterruptedException if interrupted while waiting |
2 | 345 |
*/ |
346 |
public void timedWait(Object obj, long timeout) |
|
7518 | 347 |
throws InterruptedException { |
2 | 348 |
if (timeout > 0) { |
349 |
long ms = toMillis(timeout); |
|
350 |
int ns = excessNanos(timeout, ms); |
|
351 |
obj.wait(ms, ns); |
|
352 |
} |
|
353 |
} |
|
354 |
||
355 |
/** |
|
7518 | 356 |
* Performs a timed {@link Thread#join(long, int) Thread.join} |
357 |
* using this time unit. |
|
2 | 358 |
* This is a convenience method that converts time arguments into the |
18768 | 359 |
* form required by the {@code Thread.join} method. |
7518 | 360 |
* |
2 | 361 |
* @param thread the thread to wait for |
362 |
* @param timeout the maximum time to wait. If less than |
|
363 |
* or equal to zero, do not wait at all. |
|
7518 | 364 |
* @throws InterruptedException if interrupted while waiting |
2 | 365 |
*/ |
366 |
public void timedJoin(Thread thread, long timeout) |
|
7518 | 367 |
throws InterruptedException { |
2 | 368 |
if (timeout > 0) { |
369 |
long ms = toMillis(timeout); |
|
370 |
int ns = excessNanos(timeout, ms); |
|
371 |
thread.join(ms, ns); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
/** |
|
7518 | 376 |
* Performs a {@link Thread#sleep(long, int) Thread.sleep} using |
377 |
* this time unit. |
|
2 | 378 |
* This is a convenience method that converts time arguments into the |
18768 | 379 |
* form required by the {@code Thread.sleep} method. |
7518 | 380 |
* |
2 | 381 |
* @param timeout the minimum time to sleep. If less than |
382 |
* or equal to zero, do not sleep at all. |
|
7518 | 383 |
* @throws InterruptedException if interrupted while sleeping |
2 | 384 |
*/ |
385 |
public void sleep(long timeout) throws InterruptedException { |
|
386 |
if (timeout > 0) { |
|
387 |
long ms = toMillis(timeout); |
|
388 |
int ns = excessNanos(timeout, ms); |
|
389 |
Thread.sleep(ms, ns); |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
} |