125 |
|
/** |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
< |
public void testhasQueuedThreads() { |
128 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
129 |
|
final Mutex sync = new Mutex(); |
130 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
132 |
< |
try { |
133 |
< |
assertFalse(sync.hasQueuedThreads()); |
134 |
< |
sync.acquire(1); |
135 |
< |
t1.start(); |
136 |
< |
Thread.sleep(SHORT_DELAY_MS); |
137 |
< |
assertTrue(sync.hasQueuedThreads()); |
138 |
< |
t2.start(); |
139 |
< |
Thread.sleep(SHORT_DELAY_MS); |
140 |
< |
assertTrue(sync.hasQueuedThreads()); |
141 |
< |
t1.interrupt(); |
142 |
< |
Thread.sleep(SHORT_DELAY_MS); |
143 |
< |
assertTrue(sync.hasQueuedThreads()); |
144 |
< |
sync.release(1); |
145 |
< |
Thread.sleep(SHORT_DELAY_MS); |
146 |
< |
assertFalse(sync.hasQueuedThreads()); |
147 |
< |
t1.join(); |
148 |
< |
t2.join(); |
149 |
< |
} catch (Exception e) { |
150 |
< |
unexpectedException(); |
151 |
< |
} |
132 |
> |
assertFalse(sync.hasQueuedThreads()); |
133 |
> |
sync.acquire(1); |
134 |
> |
t1.start(); |
135 |
> |
Thread.sleep(SHORT_DELAY_MS); |
136 |
> |
assertTrue(sync.hasQueuedThreads()); |
137 |
> |
t2.start(); |
138 |
> |
Thread.sleep(SHORT_DELAY_MS); |
139 |
> |
assertTrue(sync.hasQueuedThreads()); |
140 |
> |
t1.interrupt(); |
141 |
> |
Thread.sleep(SHORT_DELAY_MS); |
142 |
> |
assertTrue(sync.hasQueuedThreads()); |
143 |
> |
sync.release(1); |
144 |
> |
Thread.sleep(SHORT_DELAY_MS); |
145 |
> |
assertFalse(sync.hasQueuedThreads()); |
146 |
> |
t1.join(); |
147 |
> |
t2.join(); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
155 |
|
try { |
156 |
|
sync.isQueued(null); |
157 |
|
shouldThrow(); |
158 |
< |
} catch (NullPointerException success) { |
163 |
< |
} |
158 |
> |
} catch (NullPointerException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* isQueued reports whether a thread is queued. |
163 |
|
*/ |
164 |
< |
public void testIsQueued() { |
164 |
> |
public void testIsQueued() throws InterruptedException { |
165 |
|
final Mutex sync = new Mutex(); |
166 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
167 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
168 |
< |
try { |
169 |
< |
assertFalse(sync.isQueued(t1)); |
170 |
< |
assertFalse(sync.isQueued(t2)); |
171 |
< |
sync.acquire(1); |
172 |
< |
t1.start(); |
173 |
< |
Thread.sleep(SHORT_DELAY_MS); |
174 |
< |
assertTrue(sync.isQueued(t1)); |
175 |
< |
t2.start(); |
176 |
< |
Thread.sleep(SHORT_DELAY_MS); |
177 |
< |
assertTrue(sync.isQueued(t1)); |
178 |
< |
assertTrue(sync.isQueued(t2)); |
179 |
< |
t1.interrupt(); |
180 |
< |
Thread.sleep(SHORT_DELAY_MS); |
181 |
< |
assertFalse(sync.isQueued(t1)); |
182 |
< |
assertTrue(sync.isQueued(t2)); |
183 |
< |
sync.release(1); |
184 |
< |
Thread.sleep(SHORT_DELAY_MS); |
185 |
< |
assertFalse(sync.isQueued(t1)); |
186 |
< |
Thread.sleep(SHORT_DELAY_MS); |
187 |
< |
assertFalse(sync.isQueued(t2)); |
188 |
< |
t1.join(); |
194 |
< |
t2.join(); |
195 |
< |
} catch (Exception e) { |
196 |
< |
unexpectedException(); |
197 |
< |
} |
168 |
> |
assertFalse(sync.isQueued(t1)); |
169 |
> |
assertFalse(sync.isQueued(t2)); |
170 |
> |
sync.acquire(1); |
171 |
> |
t1.start(); |
172 |
> |
Thread.sleep(SHORT_DELAY_MS); |
173 |
> |
assertTrue(sync.isQueued(t1)); |
174 |
> |
t2.start(); |
175 |
> |
Thread.sleep(SHORT_DELAY_MS); |
176 |
> |
assertTrue(sync.isQueued(t1)); |
177 |
> |
assertTrue(sync.isQueued(t2)); |
178 |
> |
t1.interrupt(); |
179 |
> |
Thread.sleep(SHORT_DELAY_MS); |
180 |
> |
assertFalse(sync.isQueued(t1)); |
181 |
> |
assertTrue(sync.isQueued(t2)); |
182 |
> |
sync.release(1); |
183 |
> |
Thread.sleep(SHORT_DELAY_MS); |
184 |
> |
assertFalse(sync.isQueued(t1)); |
185 |
> |
Thread.sleep(SHORT_DELAY_MS); |
186 |
> |
assertFalse(sync.isQueued(t2)); |
187 |
> |
t1.join(); |
188 |
> |
t2.join(); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
193 |
|
*/ |
194 |
< |
public void testGetFirstQueuedThread() { |
194 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
195 |
|
final Mutex sync = new Mutex(); |
196 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
197 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
198 |
< |
try { |
199 |
< |
assertNull(sync.getFirstQueuedThread()); |
200 |
< |
sync.acquire(1); |
201 |
< |
t1.start(); |
202 |
< |
Thread.sleep(SHORT_DELAY_MS); |
203 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
204 |
< |
t2.start(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
207 |
< |
t1.interrupt(); |
208 |
< |
Thread.sleep(SHORT_DELAY_MS); |
209 |
< |
Thread.sleep(SHORT_DELAY_MS); |
210 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
211 |
< |
sync.release(1); |
212 |
< |
Thread.sleep(SHORT_DELAY_MS); |
213 |
< |
assertNull(sync.getFirstQueuedThread()); |
214 |
< |
t1.join(); |
224 |
< |
t2.join(); |
225 |
< |
} catch (Exception e) { |
226 |
< |
unexpectedException(); |
227 |
< |
} |
198 |
> |
assertNull(sync.getFirstQueuedThread()); |
199 |
> |
sync.acquire(1); |
200 |
> |
t1.start(); |
201 |
> |
Thread.sleep(SHORT_DELAY_MS); |
202 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
203 |
> |
t2.start(); |
204 |
> |
Thread.sleep(SHORT_DELAY_MS); |
205 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
206 |
> |
t1.interrupt(); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
Thread.sleep(SHORT_DELAY_MS); |
209 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
210 |
> |
sync.release(1); |
211 |
> |
Thread.sleep(SHORT_DELAY_MS); |
212 |
> |
assertNull(sync.getFirstQueuedThread()); |
213 |
> |
t1.join(); |
214 |
> |
t2.join(); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
/** |
219 |
|
* hasContended reports false if no thread has ever blocked, else true |
220 |
|
*/ |
221 |
< |
public void testHasContended() { |
221 |
> |
public void testHasContended() throws InterruptedException { |
222 |
|
final Mutex sync = new Mutex(); |
223 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
224 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
225 |
< |
try { |
226 |
< |
assertFalse(sync.hasContended()); |
227 |
< |
sync.acquire(1); |
228 |
< |
t1.start(); |
229 |
< |
Thread.sleep(SHORT_DELAY_MS); |
230 |
< |
assertTrue(sync.hasContended()); |
231 |
< |
t2.start(); |
232 |
< |
Thread.sleep(SHORT_DELAY_MS); |
233 |
< |
assertTrue(sync.hasContended()); |
234 |
< |
t1.interrupt(); |
235 |
< |
Thread.sleep(SHORT_DELAY_MS); |
236 |
< |
assertTrue(sync.hasContended()); |
237 |
< |
sync.release(1); |
238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
239 |
< |
assertTrue(sync.hasContended()); |
240 |
< |
t1.join(); |
254 |
< |
t2.join(); |
255 |
< |
} catch (Exception e) { |
256 |
< |
unexpectedException(); |
257 |
< |
} |
225 |
> |
assertFalse(sync.hasContended()); |
226 |
> |
sync.acquire(1); |
227 |
> |
t1.start(); |
228 |
> |
Thread.sleep(SHORT_DELAY_MS); |
229 |
> |
assertTrue(sync.hasContended()); |
230 |
> |
t2.start(); |
231 |
> |
Thread.sleep(SHORT_DELAY_MS); |
232 |
> |
assertTrue(sync.hasContended()); |
233 |
> |
t1.interrupt(); |
234 |
> |
Thread.sleep(SHORT_DELAY_MS); |
235 |
> |
assertTrue(sync.hasContended()); |
236 |
> |
sync.release(1); |
237 |
> |
Thread.sleep(SHORT_DELAY_MS); |
238 |
> |
assertTrue(sync.hasContended()); |
239 |
> |
t1.join(); |
240 |
> |
t2.join(); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
|
* getQueuedThreads includes waiting threads |
245 |
|
*/ |
246 |
< |
public void testGetQueuedThreads() { |
246 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
247 |
|
final Mutex sync = new Mutex(); |
248 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
249 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
250 |
< |
try { |
251 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
252 |
< |
sync.acquire(1); |
253 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
254 |
< |
t1.start(); |
255 |
< |
Thread.sleep(SHORT_DELAY_MS); |
256 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
257 |
< |
t2.start(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
260 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
261 |
< |
t1.interrupt(); |
262 |
< |
Thread.sleep(SHORT_DELAY_MS); |
263 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
264 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
265 |
< |
sync.release(1); |
266 |
< |
Thread.sleep(SHORT_DELAY_MS); |
267 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
268 |
< |
t1.join(); |
286 |
< |
t2.join(); |
287 |
< |
} catch (Exception e) { |
288 |
< |
unexpectedException(); |
289 |
< |
} |
250 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
251 |
> |
sync.acquire(1); |
252 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
253 |
> |
t1.start(); |
254 |
> |
Thread.sleep(SHORT_DELAY_MS); |
255 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
256 |
> |
t2.start(); |
257 |
> |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
259 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
260 |
> |
t1.interrupt(); |
261 |
> |
Thread.sleep(SHORT_DELAY_MS); |
262 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
263 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
264 |
> |
sync.release(1); |
265 |
> |
Thread.sleep(SHORT_DELAY_MS); |
266 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
267 |
> |
t1.join(); |
268 |
> |
t2.join(); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
|
* getExclusiveQueuedThreads includes waiting threads |
273 |
|
*/ |
274 |
< |
public void testGetExclusiveQueuedThreads() { |
274 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
275 |
|
final Mutex sync = new Mutex(); |
276 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
277 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
278 |
< |
try { |
279 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
280 |
< |
sync.acquire(1); |
281 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
282 |
< |
t1.start(); |
283 |
< |
Thread.sleep(SHORT_DELAY_MS); |
284 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
285 |
< |
t2.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
287 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
288 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
289 |
< |
t1.interrupt(); |
290 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
292 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
293 |
< |
sync.release(1); |
294 |
< |
Thread.sleep(SHORT_DELAY_MS); |
295 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
296 |
< |
t1.join(); |
318 |
< |
t2.join(); |
319 |
< |
} catch (Exception e) { |
320 |
< |
unexpectedException(); |
321 |
< |
} |
278 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
279 |
> |
sync.acquire(1); |
280 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
281 |
> |
t1.start(); |
282 |
> |
Thread.sleep(SHORT_DELAY_MS); |
283 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
284 |
> |
t2.start(); |
285 |
> |
Thread.sleep(SHORT_DELAY_MS); |
286 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
287 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
288 |
> |
t1.interrupt(); |
289 |
> |
Thread.sleep(SHORT_DELAY_MS); |
290 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
291 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
292 |
> |
sync.release(1); |
293 |
> |
Thread.sleep(SHORT_DELAY_MS); |
294 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
295 |
> |
t1.join(); |
296 |
> |
t2.join(); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
301 |
|
*/ |
302 |
< |
public void testGetSharedQueuedThreads() { |
302 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
303 |
|
final Mutex sync = new Mutex(); |
304 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
305 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
306 |
< |
try { |
307 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
308 |
< |
sync.acquire(1); |
309 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
310 |
< |
t1.start(); |
311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
312 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
313 |
< |
t2.start(); |
314 |
< |
Thread.sleep(SHORT_DELAY_MS); |
315 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
316 |
< |
t1.interrupt(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
318 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
319 |
< |
sync.release(1); |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
322 |
< |
t1.join(); |
348 |
< |
t2.join(); |
349 |
< |
} catch (Exception e) { |
350 |
< |
unexpectedException(); |
351 |
< |
} |
306 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
307 |
> |
sync.acquire(1); |
308 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
> |
t1.start(); |
310 |
> |
Thread.sleep(SHORT_DELAY_MS); |
311 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
> |
t2.start(); |
313 |
> |
Thread.sleep(SHORT_DELAY_MS); |
314 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
> |
t1.interrupt(); |
316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
317 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
> |
sync.release(1); |
319 |
> |
Thread.sleep(SHORT_DELAY_MS); |
320 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
321 |
> |
t1.join(); |
322 |
> |
t2.join(); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* tryAcquireNanos is interruptible. |
327 |
|
*/ |
328 |
< |
public void testInterruptedException2() { |
328 |
> |
public void testInterruptedException2() throws InterruptedException { |
329 |
|
final Mutex sync = new Mutex(); |
330 |
|
sync.acquire(1); |
331 |
|
Thread t = new Thread(new Runnable() { |
336 |
|
} catch (InterruptedException success) {} |
337 |
|
} |
338 |
|
}); |
339 |
< |
try { |
340 |
< |
t.start(); |
341 |
< |
t.interrupt(); |
342 |
< |
} catch (Exception e) { |
372 |
< |
unexpectedException(); |
373 |
< |
} |
339 |
> |
|
340 |
> |
t.start(); |
341 |
> |
t.interrupt(); |
342 |
> |
t.join(); |
343 |
|
} |
344 |
|
|
345 |
|
|
346 |
|
/** |
347 |
|
* TryAcquire on exclusively held sync fails |
348 |
|
*/ |
349 |
< |
public void testTryAcquireWhenSynced() { |
349 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
350 |
|
final Mutex sync = new Mutex(); |
351 |
|
sync.acquire(1); |
352 |
|
Thread t = new Thread(new Runnable() { |
354 |
|
threadAssertFalse(sync.tryAcquire(1)); |
355 |
|
} |
356 |
|
}); |
357 |
< |
try { |
358 |
< |
t.start(); |
359 |
< |
t.join(); |
360 |
< |
sync.release(1); |
392 |
< |
} catch (Exception e) { |
393 |
< |
unexpectedException(); |
394 |
< |
} |
357 |
> |
|
358 |
> |
t.start(); |
359 |
> |
t.join(); |
360 |
> |
sync.release(1); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
|
* tryAcquireNanos on an exclusively held sync times out |
365 |
|
*/ |
366 |
< |
public void testAcquireNanos_Timeout() { |
366 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
367 |
|
final Mutex sync = new Mutex(); |
368 |
|
sync.acquire(1); |
369 |
|
Thread t = new Thread(new Runnable() { |
375 |
|
} |
376 |
|
} |
377 |
|
}); |
378 |
< |
try { |
379 |
< |
t.start(); |
380 |
< |
t.join(); |
381 |
< |
sync.release(1); |
416 |
< |
} catch (Exception e) { |
417 |
< |
unexpectedException(); |
418 |
< |
} |
378 |
> |
|
379 |
> |
t.start(); |
380 |
> |
t.join(); |
381 |
> |
sync.release(1); |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
|
/** |
386 |
|
* getState is true when acquired and false when not |
387 |
|
*/ |
388 |
< |
public void testGetState() { |
388 |
> |
public void testGetState() throws InterruptedException { |
389 |
|
final Mutex sync = new Mutex(); |
390 |
|
sync.acquire(1); |
391 |
|
assertTrue(sync.isHeldExclusively()); |
403 |
|
sync.release(1); |
404 |
|
} |
405 |
|
}); |
406 |
< |
try { |
407 |
< |
t.start(); |
408 |
< |
Thread.sleep(SHORT_DELAY_MS); |
409 |
< |
assertTrue(sync.isHeldExclusively()); |
410 |
< |
t.join(); |
411 |
< |
assertFalse(sync.isHeldExclusively()); |
449 |
< |
} catch (Exception e) { |
450 |
< |
unexpectedException(); |
451 |
< |
} |
406 |
> |
|
407 |
> |
t.start(); |
408 |
> |
Thread.sleep(SHORT_DELAY_MS); |
409 |
> |
assertTrue(sync.isHeldExclusively()); |
410 |
> |
t.join(); |
411 |
> |
assertFalse(sync.isHeldExclusively()); |
412 |
|
} |
413 |
|
|
414 |
|
|
415 |
|
/** |
416 |
|
* acquireInterruptibly is interruptible. |
417 |
|
*/ |
418 |
< |
public void testAcquireInterruptibly1() { |
418 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
419 |
|
final Mutex sync = new Mutex(); |
420 |
|
sync.acquire(1); |
421 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
422 |
< |
try { |
423 |
< |
t.start(); |
424 |
< |
Thread.sleep(SHORT_DELAY_MS); |
425 |
< |
t.interrupt(); |
426 |
< |
Thread.sleep(SHORT_DELAY_MS); |
427 |
< |
sync.release(1); |
428 |
< |
t.join(); |
469 |
< |
} catch (Exception e) { |
470 |
< |
unexpectedException(); |
471 |
< |
} |
422 |
> |
|
423 |
> |
t.start(); |
424 |
> |
Thread.sleep(SHORT_DELAY_MS); |
425 |
> |
t.interrupt(); |
426 |
> |
Thread.sleep(SHORT_DELAY_MS); |
427 |
> |
sync.release(1); |
428 |
> |
t.join(); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
433 |
|
*/ |
434 |
< |
public void testAcquireInterruptibly2() { |
434 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
435 |
|
final Mutex sync = new Mutex(); |
436 |
< |
try { |
480 |
< |
sync.acquireInterruptibly(1); |
481 |
< |
} catch (Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
436 |
> |
sync.acquireInterruptibly(1); |
437 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
438 |
< |
try { |
439 |
< |
t.start(); |
440 |
< |
t.interrupt(); |
441 |
< |
assertTrue(sync.isHeldExclusively()); |
489 |
< |
t.join(); |
490 |
< |
} catch (Exception e) { |
491 |
< |
unexpectedException(); |
492 |
< |
} |
438 |
> |
t.start(); |
439 |
> |
t.interrupt(); |
440 |
> |
assertTrue(sync.isHeldExclusively()); |
441 |
> |
t.join(); |
442 |
|
} |
443 |
|
|
444 |
|
/** |
455 |
|
/** |
456 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
457 |
|
*/ |
458 |
< |
public void testAwait_IllegalMonitor() { |
458 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
459 |
|
final Mutex sync = new Mutex(); |
460 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
|
try { |
462 |
|
c.await(); |
463 |
|
shouldThrow(); |
464 |
|
} |
465 |
< |
catch (IllegalMonitorStateException success) { |
517 |
< |
} |
518 |
< |
catch (Exception ex) { |
519 |
< |
unexpectedException(); |
520 |
< |
} |
465 |
> |
catch (IllegalMonitorStateException success) {} |
466 |
|
} |
467 |
|
|
468 |
|
/** |
475 |
|
c.signal(); |
476 |
|
shouldThrow(); |
477 |
|
} |
478 |
< |
catch (IllegalMonitorStateException success) { |
534 |
< |
} |
535 |
< |
catch (Exception ex) { |
536 |
< |
unexpectedException(); |
537 |
< |
} |
478 |
> |
catch (IllegalMonitorStateException success) {} |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
|
* awaitNanos without a signal times out |
483 |
|
*/ |
484 |
< |
public void testAwaitNanos_Timeout() { |
484 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
485 |
|
final Mutex sync = new Mutex(); |
486 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
487 |
< |
try { |
488 |
< |
sync.acquire(1); |
489 |
< |
long t = c.awaitNanos(100); |
490 |
< |
assertTrue(t <= 0); |
550 |
< |
sync.release(1); |
551 |
< |
} |
552 |
< |
catch (Exception ex) { |
553 |
< |
unexpectedException(); |
554 |
< |
} |
487 |
> |
sync.acquire(1); |
488 |
> |
long t = c.awaitNanos(100); |
489 |
> |
assertTrue(t <= 0); |
490 |
> |
sync.release(1); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
|
* Timed await without a signal times out |
495 |
|
*/ |
496 |
< |
public void testAwait_Timeout() { |
496 |
> |
public void testAwait_Timeout() throws InterruptedException { |
497 |
|
final Mutex sync = new Mutex(); |
498 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
499 |
< |
try { |
500 |
< |
sync.acquire(1); |
501 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
566 |
< |
sync.release(1); |
567 |
< |
} |
568 |
< |
catch (Exception ex) { |
569 |
< |
unexpectedException(); |
570 |
< |
} |
499 |
> |
sync.acquire(1); |
500 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
501 |
> |
sync.release(1); |
502 |
|
} |
503 |
|
|
504 |
|
/** |
505 |
|
* awaitUntil without a signal times out |
506 |
|
*/ |
507 |
< |
public void testAwaitUntil_Timeout() { |
507 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
508 |
|
final Mutex sync = new Mutex(); |
509 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
510 |
< |
try { |
511 |
< |
sync.acquire(1); |
512 |
< |
java.util.Date d = new java.util.Date(); |
513 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
583 |
< |
sync.release(1); |
584 |
< |
} |
585 |
< |
catch (Exception ex) { |
586 |
< |
unexpectedException(); |
587 |
< |
} |
510 |
> |
sync.acquire(1); |
511 |
> |
java.util.Date d = new java.util.Date(); |
512 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
513 |
> |
sync.release(1); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
|
* await returns when signalled |
518 |
|
*/ |
519 |
< |
public void testAwait() { |
519 |
> |
public void testAwait() throws InterruptedException { |
520 |
|
final Mutex sync = new Mutex(); |
521 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
522 |
|
Thread t = new Thread(new Runnable() { |
532 |
|
} |
533 |
|
}); |
534 |
|
|
535 |
< |
try { |
536 |
< |
t.start(); |
537 |
< |
Thread.sleep(SHORT_DELAY_MS); |
538 |
< |
sync.acquire(1); |
539 |
< |
c.signal(); |
540 |
< |
sync.release(1); |
541 |
< |
t.join(SHORT_DELAY_MS); |
616 |
< |
assertFalse(t.isAlive()); |
617 |
< |
} |
618 |
< |
catch (Exception ex) { |
619 |
< |
unexpectedException(); |
620 |
< |
} |
535 |
> |
t.start(); |
536 |
> |
Thread.sleep(SHORT_DELAY_MS); |
537 |
> |
sync.acquire(1); |
538 |
> |
c.signal(); |
539 |
> |
sync.release(1); |
540 |
> |
t.join(SHORT_DELAY_MS); |
541 |
> |
assertFalse(t.isAlive()); |
542 |
|
} |
543 |
|
|
544 |
|
|
551 |
|
try { |
552 |
|
sync.hasWaiters(null); |
553 |
|
shouldThrow(); |
554 |
< |
} catch (NullPointerException success) { |
634 |
< |
} catch (Exception ex) { |
635 |
< |
unexpectedException(); |
636 |
< |
} |
554 |
> |
} catch (NullPointerException success) {} |
555 |
|
} |
556 |
|
|
557 |
|
/** |
562 |
|
try { |
563 |
|
sync.getWaitQueueLength(null); |
564 |
|
shouldThrow(); |
565 |
< |
} catch (NullPointerException success) { |
648 |
< |
} catch (Exception ex) { |
649 |
< |
unexpectedException(); |
650 |
< |
} |
565 |
> |
} catch (NullPointerException success) {} |
566 |
|
} |
567 |
|
|
568 |
|
|
574 |
|
try { |
575 |
|
sync.getWaitingThreads(null); |
576 |
|
shouldThrow(); |
577 |
< |
} catch (NullPointerException success) { |
663 |
< |
} catch (Exception ex) { |
664 |
< |
unexpectedException(); |
665 |
< |
} |
577 |
> |
} catch (NullPointerException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
|
583 |
|
*/ |
584 |
|
public void testHasWaitersIAE() { |
585 |
|
final Mutex sync = new Mutex(); |
586 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
586 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
587 |
|
final Mutex sync2 = new Mutex(); |
588 |
|
try { |
589 |
|
sync2.hasWaiters(c); |
590 |
|
shouldThrow(); |
591 |
< |
} catch (IllegalArgumentException success) { |
680 |
< |
} catch (Exception ex) { |
681 |
< |
unexpectedException(); |
682 |
< |
} |
591 |
> |
} catch (IllegalArgumentException success) {} |
592 |
|
} |
593 |
|
|
594 |
|
/** |
596 |
|
*/ |
597 |
|
public void testHasWaitersIMSE() { |
598 |
|
final Mutex sync = new Mutex(); |
599 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
599 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
600 |
|
try { |
601 |
|
sync.hasWaiters(c); |
602 |
|
shouldThrow(); |
603 |
< |
} catch (IllegalMonitorStateException success) { |
695 |
< |
} catch (Exception ex) { |
696 |
< |
unexpectedException(); |
697 |
< |
} |
603 |
> |
} catch (IllegalMonitorStateException success) {} |
604 |
|
} |
605 |
|
|
606 |
|
|
609 |
|
*/ |
610 |
|
public void testGetWaitQueueLengthIAE() { |
611 |
|
final Mutex sync = new Mutex(); |
612 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
612 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
613 |
|
final Mutex sync2 = new Mutex(); |
614 |
|
try { |
615 |
|
sync2.getWaitQueueLength(c); |
616 |
|
shouldThrow(); |
617 |
< |
} catch (IllegalArgumentException success) { |
712 |
< |
} catch (Exception ex) { |
713 |
< |
unexpectedException(); |
714 |
< |
} |
617 |
> |
} catch (IllegalArgumentException success) {} |
618 |
|
} |
619 |
|
|
620 |
|
/** |
622 |
|
*/ |
623 |
|
public void testGetWaitQueueLengthIMSE() { |
624 |
|
final Mutex sync = new Mutex(); |
625 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
625 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
626 |
|
try { |
627 |
|
sync.getWaitQueueLength(c); |
628 |
|
shouldThrow(); |
629 |
< |
} catch (IllegalMonitorStateException success) { |
727 |
< |
} catch (Exception ex) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
629 |
> |
} catch (IllegalMonitorStateException success) {} |
630 |
|
} |
631 |
|
|
632 |
|
|
635 |
|
*/ |
636 |
|
public void testGetWaitingThreadsIAE() { |
637 |
|
final Mutex sync = new Mutex(); |
638 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
638 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
639 |
|
final Mutex sync2 = new Mutex(); |
640 |
|
try { |
641 |
|
sync2.getWaitingThreads(c); |
642 |
|
shouldThrow(); |
643 |
< |
} catch (IllegalArgumentException success) { |
744 |
< |
} catch (Exception ex) { |
745 |
< |
unexpectedException(); |
746 |
< |
} |
643 |
> |
} catch (IllegalArgumentException success) {} |
644 |
|
} |
645 |
|
|
646 |
|
/** |
648 |
|
*/ |
649 |
|
public void testGetWaitingThreadsIMSE() { |
650 |
|
final Mutex sync = new Mutex(); |
651 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
651 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
652 |
|
try { |
653 |
|
sync.getWaitingThreads(c); |
654 |
|
shouldThrow(); |
655 |
< |
} catch (IllegalMonitorStateException success) { |
759 |
< |
} catch (Exception ex) { |
760 |
< |
unexpectedException(); |
761 |
< |
} |
655 |
> |
} catch (IllegalMonitorStateException success) {} |
656 |
|
} |
657 |
|
|
658 |
|
|
660 |
|
/** |
661 |
|
* hasWaiters returns true when a thread is waiting, else false |
662 |
|
*/ |
663 |
< |
public void testHasWaiters() { |
663 |
> |
public void testHasWaiters() throws InterruptedException { |
664 |
|
final Mutex sync = new Mutex(); |
665 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
666 |
|
Thread t = new Thread(new Runnable() { |
678 |
|
} |
679 |
|
}); |
680 |
|
|
681 |
< |
try { |
682 |
< |
t.start(); |
683 |
< |
Thread.sleep(SHORT_DELAY_MS); |
684 |
< |
sync.acquire(1); |
685 |
< |
assertTrue(sync.hasWaiters(c)); |
686 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
687 |
< |
c.signal(); |
688 |
< |
sync.release(1); |
689 |
< |
Thread.sleep(SHORT_DELAY_MS); |
690 |
< |
sync.acquire(1); |
691 |
< |
assertFalse(sync.hasWaiters(c)); |
692 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
693 |
< |
sync.release(1); |
694 |
< |
t.join(SHORT_DELAY_MS); |
801 |
< |
assertFalse(t.isAlive()); |
802 |
< |
} |
803 |
< |
catch (Exception ex) { |
804 |
< |
unexpectedException(); |
805 |
< |
} |
681 |
> |
t.start(); |
682 |
> |
Thread.sleep(SHORT_DELAY_MS); |
683 |
> |
sync.acquire(1); |
684 |
> |
assertTrue(sync.hasWaiters(c)); |
685 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
686 |
> |
c.signal(); |
687 |
> |
sync.release(1); |
688 |
> |
Thread.sleep(SHORT_DELAY_MS); |
689 |
> |
sync.acquire(1); |
690 |
> |
assertFalse(sync.hasWaiters(c)); |
691 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
692 |
> |
sync.release(1); |
693 |
> |
t.join(SHORT_DELAY_MS); |
694 |
> |
assertFalse(t.isAlive()); |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
|
* getWaitQueueLength returns number of waiting threads |
699 |
|
*/ |
700 |
< |
public void testGetWaitQueueLength() { |
700 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
701 |
|
final Mutex sync = new Mutex(); |
702 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
703 |
|
Thread t1 = new Thread(new Runnable() { |
730 |
|
} |
731 |
|
}); |
732 |
|
|
733 |
< |
try { |
734 |
< |
t1.start(); |
735 |
< |
Thread.sleep(SHORT_DELAY_MS); |
736 |
< |
t2.start(); |
737 |
< |
Thread.sleep(SHORT_DELAY_MS); |
738 |
< |
sync.acquire(1); |
739 |
< |
assertTrue(sync.hasWaiters(c)); |
740 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
741 |
< |
c.signalAll(); |
742 |
< |
sync.release(1); |
743 |
< |
Thread.sleep(SHORT_DELAY_MS); |
744 |
< |
sync.acquire(1); |
745 |
< |
assertFalse(sync.hasWaiters(c)); |
746 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
747 |
< |
sync.release(1); |
748 |
< |
t1.join(SHORT_DELAY_MS); |
749 |
< |
t2.join(SHORT_DELAY_MS); |
750 |
< |
assertFalse(t1.isAlive()); |
862 |
< |
assertFalse(t2.isAlive()); |
863 |
< |
} |
864 |
< |
catch (Exception ex) { |
865 |
< |
unexpectedException(); |
866 |
< |
} |
733 |
> |
t1.start(); |
734 |
> |
Thread.sleep(SHORT_DELAY_MS); |
735 |
> |
t2.start(); |
736 |
> |
Thread.sleep(SHORT_DELAY_MS); |
737 |
> |
sync.acquire(1); |
738 |
> |
assertTrue(sync.hasWaiters(c)); |
739 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
740 |
> |
c.signalAll(); |
741 |
> |
sync.release(1); |
742 |
> |
Thread.sleep(SHORT_DELAY_MS); |
743 |
> |
sync.acquire(1); |
744 |
> |
assertFalse(sync.hasWaiters(c)); |
745 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
746 |
> |
sync.release(1); |
747 |
> |
t1.join(SHORT_DELAY_MS); |
748 |
> |
t2.join(SHORT_DELAY_MS); |
749 |
> |
assertFalse(t1.isAlive()); |
750 |
> |
assertFalse(t2.isAlive()); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
|
* getWaitingThreads returns only and all waiting threads |
755 |
|
*/ |
756 |
< |
public void testGetWaitingThreads() { |
756 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
757 |
|
final Mutex sync = new Mutex(); |
758 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
759 |
|
Thread t1 = new Thread(new Runnable() { |
784 |
|
} |
785 |
|
}); |
786 |
|
|
787 |
< |
try { |
788 |
< |
sync.acquire(1); |
789 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
790 |
< |
sync.release(1); |
791 |
< |
t1.start(); |
792 |
< |
Thread.sleep(SHORT_DELAY_MS); |
793 |
< |
t2.start(); |
794 |
< |
Thread.sleep(SHORT_DELAY_MS); |
795 |
< |
sync.acquire(1); |
796 |
< |
assertTrue(sync.hasWaiters(c)); |
797 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
798 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
799 |
< |
c.signalAll(); |
800 |
< |
sync.release(1); |
801 |
< |
Thread.sleep(SHORT_DELAY_MS); |
802 |
< |
sync.acquire(1); |
803 |
< |
assertFalse(sync.hasWaiters(c)); |
804 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
805 |
< |
sync.release(1); |
806 |
< |
t1.join(SHORT_DELAY_MS); |
807 |
< |
t2.join(SHORT_DELAY_MS); |
808 |
< |
assertFalse(t1.isAlive()); |
925 |
< |
assertFalse(t2.isAlive()); |
926 |
< |
} |
927 |
< |
catch (Exception ex) { |
928 |
< |
unexpectedException(); |
929 |
< |
} |
787 |
> |
sync.acquire(1); |
788 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
789 |
> |
sync.release(1); |
790 |
> |
t1.start(); |
791 |
> |
Thread.sleep(SHORT_DELAY_MS); |
792 |
> |
t2.start(); |
793 |
> |
Thread.sleep(SHORT_DELAY_MS); |
794 |
> |
sync.acquire(1); |
795 |
> |
assertTrue(sync.hasWaiters(c)); |
796 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
797 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
798 |
> |
c.signalAll(); |
799 |
> |
sync.release(1); |
800 |
> |
Thread.sleep(SHORT_DELAY_MS); |
801 |
> |
sync.acquire(1); |
802 |
> |
assertFalse(sync.hasWaiters(c)); |
803 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
804 |
> |
sync.release(1); |
805 |
> |
t1.join(SHORT_DELAY_MS); |
806 |
> |
t2.join(SHORT_DELAY_MS); |
807 |
> |
assertFalse(t1.isAlive()); |
808 |
> |
assertFalse(t2.isAlive()); |
809 |
|
} |
810 |
|
|
811 |
|
|
813 |
|
/** |
814 |
|
* awaitUninterruptibly doesn't abort on interrupt |
815 |
|
*/ |
816 |
< |
public void testAwaitUninterruptibly() { |
816 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
817 |
|
final Mutex sync = new Mutex(); |
818 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
819 |
|
Thread t = new Thread(new Runnable() { |
824 |
|
} |
825 |
|
}); |
826 |
|
|
827 |
< |
try { |
828 |
< |
t.start(); |
829 |
< |
Thread.sleep(SHORT_DELAY_MS); |
830 |
< |
t.interrupt(); |
831 |
< |
sync.acquire(1); |
832 |
< |
c.signal(); |
833 |
< |
sync.release(1); |
834 |
< |
t.join(SHORT_DELAY_MS); |
956 |
< |
assertFalse(t.isAlive()); |
957 |
< |
} |
958 |
< |
catch (Exception ex) { |
959 |
< |
unexpectedException(); |
960 |
< |
} |
827 |
> |
t.start(); |
828 |
> |
Thread.sleep(SHORT_DELAY_MS); |
829 |
> |
t.interrupt(); |
830 |
> |
sync.acquire(1); |
831 |
> |
c.signal(); |
832 |
> |
sync.release(1); |
833 |
> |
t.join(SHORT_DELAY_MS); |
834 |
> |
assertFalse(t.isAlive()); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
|
* await is interruptible |
839 |
|
*/ |
840 |
< |
public void testAwait_Interrupt() { |
840 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
841 |
|
final Mutex sync = new Mutex(); |
842 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
843 |
|
Thread t = new Thread(new Runnable() { |
853 |
|
} |
854 |
|
}); |
855 |
|
|
856 |
< |
try { |
857 |
< |
t.start(); |
858 |
< |
Thread.sleep(SHORT_DELAY_MS); |
859 |
< |
t.interrupt(); |
860 |
< |
t.join(SHORT_DELAY_MS); |
987 |
< |
assertFalse(t.isAlive()); |
988 |
< |
} |
989 |
< |
catch (Exception ex) { |
990 |
< |
unexpectedException(); |
991 |
< |
} |
856 |
> |
t.start(); |
857 |
> |
Thread.sleep(SHORT_DELAY_MS); |
858 |
> |
t.interrupt(); |
859 |
> |
t.join(SHORT_DELAY_MS); |
860 |
> |
assertFalse(t.isAlive()); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
864 |
|
* awaitNanos is interruptible |
865 |
|
*/ |
866 |
< |
public void testAwaitNanos_Interrupt() { |
866 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
867 |
|
final Mutex sync = new Mutex(); |
868 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
869 |
|
Thread t = new Thread(new Runnable() { |
879 |
|
} |
880 |
|
}); |
881 |
|
|
882 |
< |
try { |
883 |
< |
t.start(); |
884 |
< |
Thread.sleep(SHORT_DELAY_MS); |
885 |
< |
t.interrupt(); |
886 |
< |
t.join(SHORT_DELAY_MS); |
1018 |
< |
assertFalse(t.isAlive()); |
1019 |
< |
} |
1020 |
< |
catch (Exception ex) { |
1021 |
< |
unexpectedException(); |
1022 |
< |
} |
882 |
> |
t.start(); |
883 |
> |
Thread.sleep(SHORT_DELAY_MS); |
884 |
> |
t.interrupt(); |
885 |
> |
t.join(SHORT_DELAY_MS); |
886 |
> |
assertFalse(t.isAlive()); |
887 |
|
} |
888 |
|
|
889 |
|
/** |
890 |
|
* awaitUntil is interruptible |
891 |
|
*/ |
892 |
< |
public void testAwaitUntil_Interrupt() { |
892 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
893 |
|
final Mutex sync = new Mutex(); |
894 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
895 |
|
Thread t = new Thread(new Runnable() { |
906 |
|
} |
907 |
|
}); |
908 |
|
|
909 |
< |
try { |
910 |
< |
t.start(); |
911 |
< |
Thread.sleep(SHORT_DELAY_MS); |
912 |
< |
t.interrupt(); |
913 |
< |
t.join(SHORT_DELAY_MS); |
1050 |
< |
assertFalse(t.isAlive()); |
1051 |
< |
} |
1052 |
< |
catch (Exception ex) { |
1053 |
< |
unexpectedException(); |
1054 |
< |
} |
909 |
> |
t.start(); |
910 |
> |
Thread.sleep(SHORT_DELAY_MS); |
911 |
> |
t.interrupt(); |
912 |
> |
t.join(SHORT_DELAY_MS); |
913 |
> |
assertFalse(t.isAlive()); |
914 |
|
} |
915 |
|
|
916 |
|
/** |
917 |
|
* signalAll wakes up all threads |
918 |
|
*/ |
919 |
< |
public void testSignalAll() { |
919 |
> |
public void testSignalAll() throws InterruptedException { |
920 |
|
final Mutex sync = new Mutex(); |
921 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
922 |
|
Thread t1 = new Thread(new Runnable() { |
945 |
|
} |
946 |
|
}); |
947 |
|
|
948 |
< |
try { |
949 |
< |
t1.start(); |
950 |
< |
t2.start(); |
951 |
< |
Thread.sleep(SHORT_DELAY_MS); |
952 |
< |
sync.acquire(1); |
953 |
< |
c.signalAll(); |
954 |
< |
sync.release(1); |
955 |
< |
t1.join(SHORT_DELAY_MS); |
956 |
< |
t2.join(SHORT_DELAY_MS); |
957 |
< |
assertFalse(t1.isAlive()); |
1099 |
< |
assertFalse(t2.isAlive()); |
1100 |
< |
} |
1101 |
< |
catch (Exception ex) { |
1102 |
< |
unexpectedException(); |
1103 |
< |
} |
948 |
> |
t1.start(); |
949 |
> |
t2.start(); |
950 |
> |
Thread.sleep(SHORT_DELAY_MS); |
951 |
> |
sync.acquire(1); |
952 |
> |
c.signalAll(); |
953 |
> |
sync.release(1); |
954 |
> |
t1.join(SHORT_DELAY_MS); |
955 |
> |
t2.join(SHORT_DELAY_MS); |
956 |
> |
assertFalse(t1.isAlive()); |
957 |
> |
assertFalse(t2.isAlive()); |
958 |
|
} |
959 |
|
|
960 |
|
|
973 |
|
/** |
974 |
|
* A serialized AQS deserializes with current state |
975 |
|
*/ |
976 |
< |
public void testSerialization() { |
976 |
> |
public void testSerialization() throws Exception { |
977 |
|
Mutex l = new Mutex(); |
978 |
|
l.acquire(1); |
979 |
|
assertTrue(l.isHeldExclusively()); |
980 |
|
|
981 |
< |
try { |
982 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
983 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
984 |
< |
out.writeObject(l); |
985 |
< |
out.close(); |
986 |
< |
|
987 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
988 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
989 |
< |
Mutex r = (Mutex) in.readObject(); |
1136 |
< |
assertTrue(r.isHeldExclusively()); |
1137 |
< |
} catch (Exception e) { |
1138 |
< |
e.printStackTrace(); |
1139 |
< |
unexpectedException(); |
1140 |
< |
} |
981 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
982 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
983 |
> |
out.writeObject(l); |
984 |
> |
out.close(); |
985 |
> |
|
986 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
987 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
988 |
> |
Mutex r = (Mutex) in.readObject(); |
989 |
> |
assertTrue(r.isHeldExclusively()); |
990 |
|
} |
991 |
|
|
992 |
|
|
1015 |
|
/** |
1016 |
|
* acquireSharedInterruptibly returns after release, but not before |
1017 |
|
*/ |
1018 |
< |
public void testAcquireSharedInterruptibly() { |
1018 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
1019 |
|
final BooleanLatch l = new BooleanLatch(); |
1020 |
|
|
1021 |
|
Thread t = new Thread(new Runnable() { |
1029 |
|
} |
1030 |
|
} |
1031 |
|
}); |
1032 |
< |
try { |
1033 |
< |
t.start(); |
1034 |
< |
assertFalse(l.isSignalled()); |
1035 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1036 |
< |
l.releaseShared(0); |
1037 |
< |
assertTrue(l.isSignalled()); |
1038 |
< |
t.join(); |
1190 |
< |
} catch (InterruptedException e) { |
1191 |
< |
unexpectedException(); |
1192 |
< |
} |
1032 |
> |
|
1033 |
> |
t.start(); |
1034 |
> |
assertFalse(l.isSignalled()); |
1035 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1036 |
> |
l.releaseShared(0); |
1037 |
> |
assertTrue(l.isSignalled()); |
1038 |
> |
t.join(); |
1039 |
|
} |
1040 |
|
|
1041 |
|
|
1042 |
|
/** |
1043 |
|
* acquireSharedTimed returns after release |
1044 |
|
*/ |
1045 |
< |
public void testAsquireSharedTimed() { |
1045 |
> |
public void testAsquireSharedTimed() throws InterruptedException { |
1046 |
|
final BooleanLatch l = new BooleanLatch(); |
1047 |
|
|
1048 |
|
Thread t = new Thread(new Runnable() { |
1057 |
|
} |
1058 |
|
} |
1059 |
|
}); |
1060 |
< |
try { |
1061 |
< |
t.start(); |
1062 |
< |
assertFalse(l.isSignalled()); |
1063 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1064 |
< |
l.releaseShared(0); |
1065 |
< |
assertTrue(l.isSignalled()); |
1066 |
< |
t.join(); |
1221 |
< |
} catch (InterruptedException e) { |
1222 |
< |
unexpectedException(); |
1223 |
< |
} |
1060 |
> |
|
1061 |
> |
t.start(); |
1062 |
> |
assertFalse(l.isSignalled()); |
1063 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1064 |
> |
l.releaseShared(0); |
1065 |
> |
assertTrue(l.isSignalled()); |
1066 |
> |
t.join(); |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1070 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1071 |
|
*/ |
1072 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
1072 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
1073 |
|
final BooleanLatch l = new BooleanLatch(); |
1074 |
|
Thread t = new Thread(new Runnable() { |
1075 |
|
public void run() { |
1081 |
|
} |
1082 |
|
}); |
1083 |
|
t.start(); |
1084 |
< |
try { |
1085 |
< |
assertFalse(l.isSignalled()); |
1086 |
< |
t.interrupt(); |
1244 |
< |
t.join(); |
1245 |
< |
} catch (InterruptedException e) { |
1246 |
< |
unexpectedException(); |
1247 |
< |
} |
1084 |
> |
assertFalse(l.isSignalled()); |
1085 |
> |
t.interrupt(); |
1086 |
> |
t.join(); |
1087 |
|
} |
1088 |
|
|
1089 |
|
/** |
1090 |
|
* acquireSharedTimed throws IE if interrupted before released |
1091 |
|
*/ |
1092 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
1092 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
1093 |
|
final BooleanLatch l = new BooleanLatch(); |
1094 |
|
Thread t = new Thread(new Runnable() { |
1095 |
|
public void run() { |
1101 |
|
} |
1102 |
|
}); |
1103 |
|
t.start(); |
1104 |
< |
try { |
1105 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1106 |
< |
assertFalse(l.isSignalled()); |
1107 |
< |
t.interrupt(); |
1269 |
< |
t.join(); |
1270 |
< |
} catch (InterruptedException e) { |
1271 |
< |
unexpectedException(); |
1272 |
< |
} |
1104 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1105 |
> |
assertFalse(l.isSignalled()); |
1106 |
> |
t.interrupt(); |
1107 |
> |
t.join(); |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1111 |
|
* acquireSharedTimed times out if not released before timeout |
1112 |
|
*/ |
1113 |
< |
public void testAcquireSharedNanos_Timeout() { |
1113 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1114 |
|
final BooleanLatch l = new BooleanLatch(); |
1115 |
|
Thread t = new Thread(new Runnable() { |
1116 |
|
public void run() { |
1123 |
|
} |
1124 |
|
}); |
1125 |
|
t.start(); |
1126 |
< |
try { |
1127 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1128 |
< |
assertFalse(l.isSignalled()); |
1294 |
< |
t.join(); |
1295 |
< |
} catch (InterruptedException e) { |
1296 |
< |
unexpectedException(); |
1297 |
< |
} |
1126 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1127 |
> |
assertFalse(l.isSignalled()); |
1128 |
> |
t.join(); |
1129 |
|
} |
1130 |
|
|
1131 |
|
|