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