27 |
|
*/ |
28 |
|
public void testParkBeforeUnpark() throws InterruptedException { |
29 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
30 |
< |
Thread t = new Thread(new CheckedRunnable() { |
30 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
31 |
|
public void realRun() { |
32 |
|
threadStarted.countDown(); |
33 |
|
LockSupport.park(); |
34 |
|
}}); |
35 |
|
|
36 |
– |
t.start(); |
36 |
|
threadStarted.await(); |
37 |
|
Thread.sleep(SHORT_DELAY_MS); |
38 |
|
LockSupport.unpark(t); |
39 |
< |
joinWith(t); |
39 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
40 |
|
} |
41 |
|
|
42 |
|
/** |
44 |
|
*/ |
45 |
|
public void testParkUntilBeforeUnpark() throws InterruptedException { |
46 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
47 |
< |
Thread t = new Thread(new CheckedRunnable() { |
47 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
48 |
|
public void realRun() { |
49 |
|
long d = new Date().getTime() + LONG_DELAY_MS; |
50 |
|
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
54 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
55 |
|
}}); |
56 |
|
|
58 |
– |
t.start(); |
57 |
|
threadStarted.await(); |
58 |
|
Thread.sleep(SHORT_DELAY_MS); |
59 |
|
LockSupport.unpark(t); |
60 |
< |
joinWith(t); |
60 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
65 |
|
*/ |
66 |
|
public void testParkNanosBeforeUnpark() throws InterruptedException { |
67 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
68 |
< |
Thread t = new Thread(new CheckedRunnable() { |
68 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
69 |
|
public void realRun() { |
70 |
|
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
71 |
|
long t0 = System.nanoTime(); |
74 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
75 |
|
}}); |
76 |
|
|
79 |
– |
t.start(); |
77 |
|
threadStarted.await(); |
78 |
|
Thread.sleep(SHORT_DELAY_MS); |
79 |
|
LockSupport.unpark(t); |
80 |
< |
joinWith(t); |
80 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
81 |
|
} |
82 |
|
|
83 |
|
/** |
86 |
|
public void testParkAfterUnpark() throws Exception { |
87 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
88 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
89 |
< |
Thread t = new Thread(new CheckedRunnable() { |
89 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
|
public void realRun() throws Exception { |
91 |
|
threadStarted.countDown(); |
92 |
|
while (!unparked.get()) |
94 |
|
LockSupport.park(); |
95 |
|
}}); |
96 |
|
|
100 |
– |
t.start(); |
97 |
|
threadStarted.await(); |
98 |
|
LockSupport.unpark(t); |
99 |
|
unparked.set(true); |
100 |
< |
joinWith(t); |
100 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
106 |
|
public void testParkUntilAfterUnpark() throws Exception { |
107 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
108 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
109 |
< |
Thread t = new Thread(new CheckedRunnable() { |
109 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
110 |
|
public void realRun() throws Exception { |
111 |
|
threadStarted.countDown(); |
112 |
|
while (!unparked.get()) |
118 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
119 |
|
}}); |
120 |
|
|
125 |
– |
t.start(); |
121 |
|
threadStarted.await(); |
122 |
|
LockSupport.unpark(t); |
123 |
|
unparked.set(true); |
124 |
< |
joinWith(t); |
124 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
130 |
|
public void testParkNanosAfterUnpark() throws Exception { |
131 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
132 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
133 |
< |
Thread t = new Thread(new CheckedRunnable() { |
133 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
134 |
|
public void realRun() throws Exception { |
135 |
|
threadStarted.countDown(); |
136 |
|
while (!unparked.get()) |
141 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
142 |
|
}}); |
143 |
|
|
149 |
– |
t.start(); |
144 |
|
threadStarted.await(); |
145 |
|
LockSupport.unpark(t); |
146 |
|
unparked.set(true); |
147 |
< |
joinWith(t); |
147 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
152 |
|
*/ |
153 |
|
public void testParkBeforeInterrupt() throws InterruptedException { |
154 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
155 |
< |
Thread t = new Thread(new CheckedRunnable() { |
155 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
156 |
|
public void realRun() { |
157 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
158 |
|
threadStarted.countDown(); |
160 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
161 |
|
}}); |
162 |
|
|
169 |
– |
t.start(); |
163 |
|
threadStarted.await(); |
164 |
|
Thread.sleep(SHORT_DELAY_MS); |
165 |
|
t.interrupt(); |
166 |
< |
joinWith(t); |
166 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
171 |
|
*/ |
172 |
|
public void testParkUntilBeforeInterrupt() throws InterruptedException { |
173 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
174 |
< |
Thread t = new Thread(new CheckedRunnable() { |
174 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
175 |
|
public void realRun() { |
176 |
|
long d = new Date().getTime() + LONG_DELAY_MS; |
177 |
|
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
181 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
182 |
|
}}); |
183 |
|
|
191 |
– |
t.start(); |
184 |
|
threadStarted.await(); |
185 |
|
Thread.sleep(SHORT_DELAY_MS); |
186 |
|
t.interrupt(); |
187 |
< |
joinWith(t); |
187 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
192 |
|
*/ |
193 |
|
public void testParkNanosBeforeInterrupt() throws InterruptedException { |
194 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
195 |
< |
Thread t = new Thread(new CheckedRunnable() { |
195 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
196 |
|
public void realRun() { |
197 |
|
long nanos = LONG_DELAY_MS * 1000L * 1000L; |
198 |
|
long t0 = System.nanoTime(); |
201 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
202 |
|
}}); |
203 |
|
|
212 |
– |
t.start(); |
204 |
|
threadStarted.await(); |
205 |
|
Thread.sleep(SHORT_DELAY_MS); |
206 |
|
t.interrupt(); |
207 |
< |
joinWith(t); |
207 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
213 |
|
public void testParkAfterInterrupt() throws Exception { |
214 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
215 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
216 |
< |
Thread t = new Thread(new CheckedRunnable() { |
216 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
217 |
|
public void realRun() throws Exception { |
218 |
|
threadStarted.countDown(); |
219 |
|
while (!unparked.get()) |
223 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
224 |
|
}}); |
225 |
|
|
235 |
– |
t.start(); |
226 |
|
threadStarted.await(); |
227 |
|
t.interrupt(); |
228 |
|
unparked.set(true); |
229 |
< |
joinWith(t); |
229 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
235 |
|
public void testParkUntilAfterInterrupt() throws Exception { |
236 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
237 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
238 |
< |
Thread t = new Thread(new CheckedRunnable() { |
238 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
239 |
|
public void realRun() throws Exception { |
240 |
|
threadStarted.countDown(); |
241 |
|
while (!unparked.get()) |
247 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
248 |
|
}}); |
249 |
|
|
260 |
– |
t.start(); |
250 |
|
threadStarted.await(); |
251 |
|
t.interrupt(); |
252 |
|
unparked.set(true); |
253 |
< |
joinWith(t); |
253 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
259 |
|
public void testParkNanosAfterInterrupt() throws Exception { |
260 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
261 |
|
final AtomicBoolean unparked = new AtomicBoolean(false); |
262 |
< |
Thread t = new Thread(new CheckedRunnable() { |
262 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
263 |
|
public void realRun() throws Exception { |
264 |
|
threadStarted.countDown(); |
265 |
|
while (!unparked.get()) |
270 |
|
assertTrue(System.nanoTime() - t0 < nanos); |
271 |
|
}}); |
272 |
|
|
284 |
– |
t.start(); |
273 |
|
threadStarted.await(); |
274 |
|
t.interrupt(); |
275 |
|
unparked.set(true); |
276 |
< |
joinWith(t); |
276 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
|
* parkNanos times out if not unparked |
281 |
|
*/ |
282 |
|
public void testParkNanosTimesOut() throws InterruptedException { |
283 |
< |
Thread t = new Thread(new CheckedRunnable() { |
283 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
284 |
|
public void realRun() { |
285 |
|
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
286 |
|
long t0 = System.nanoTime(); |
288 |
|
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
289 |
|
}}); |
290 |
|
|
291 |
< |
t.start(); |
304 |
< |
joinWith(t); |
291 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
292 |
|
} |
293 |
|
|
294 |
|
|
296 |
|
* parkUntil times out if not unparked |
297 |
|
*/ |
298 |
|
public void testParkUntilTimesOut() throws InterruptedException { |
299 |
< |
Thread t = new Thread(new CheckedRunnable() { |
299 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
300 |
|
public void realRun() { |
301 |
|
long d = new Date().getTime() + SHORT_DELAY_MS; |
302 |
|
// beware of rounding |
306 |
|
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
307 |
|
}}); |
308 |
|
|
309 |
< |
t.start(); |
323 |
< |
joinWith(t); |
309 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
314 |
|
* Requires hotspot fix for: |
315 |
|
* 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever |
316 |
|
*/ |
317 |
< |
public void XXXtestParkUntil0Returns() throws InterruptedException { |
318 |
< |
Thread t = new Thread(new CheckedRunnable() { |
317 |
> |
public void XXXXtestParkUntil0Returns() throws InterruptedException { |
318 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
319 |
|
public void realRun() { |
320 |
|
LockSupport.parkUntil(0L); |
321 |
|
}}); |
322 |
|
|
323 |
< |
t.start(); |
338 |
< |
joinWith(t); |
339 |
< |
} |
340 |
< |
|
341 |
< |
private void joinWith(Thread t) throws InterruptedException { |
342 |
< |
t.join(MEDIUM_DELAY_MS); |
343 |
< |
if (t.isAlive()) { |
344 |
< |
fail("Test timed out"); |
345 |
< |
t.interrupt(); |
346 |
< |
} |
323 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
324 |
|
} |
325 |
|
} |