23 |
|
/** |
24 |
|
* A runnable calling lockInterruptibly |
25 |
|
*/ |
26 |
< |
class InterruptibleLockRunnable implements Runnable { |
26 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
27 |
|
final ReentrantReadWriteLock lock; |
28 |
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
29 |
< |
public void run() { |
30 |
< |
try { |
31 |
< |
lock.writeLock().lockInterruptibly(); |
32 |
< |
} catch (InterruptedException success) {} |
29 |
> |
public void realRun() throws InterruptedException { |
30 |
> |
lock.writeLock().lockInterruptibly(); |
31 |
|
} |
32 |
|
} |
33 |
|
|
36 |
|
* A runnable calling lockInterruptibly that expects to be |
37 |
|
* interrupted |
38 |
|
*/ |
39 |
< |
class InterruptedLockRunnable implements Runnable { |
39 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
40 |
|
final ReentrantReadWriteLock lock; |
41 |
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
42 |
< |
public void run() { |
43 |
< |
try { |
46 |
< |
lock.writeLock().lockInterruptibly(); |
47 |
< |
threadShouldThrow(); |
48 |
< |
} catch (InterruptedException success) {} |
42 |
> |
public void realRun() throws InterruptedException { |
43 |
> |
lock.writeLock().lockInterruptibly(); |
44 |
|
} |
45 |
|
} |
46 |
|
|
69 |
|
assertTrue(r2.isFair()); |
70 |
|
assertFalse(r2.isWriteLocked()); |
71 |
|
assertEquals(0, r2.getReadLockCount()); |
72 |
+ |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
73 |
+ |
assertFalse(r3.isFair()); |
74 |
+ |
assertFalse(r3.isWriteLocked()); |
75 |
+ |
assertEquals(0, r3.getReadLockCount()); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
187 |
|
/** |
188 |
|
* write-lockInterruptibly is interruptible |
189 |
|
*/ |
190 |
< |
public void testWriteLockInterruptibly_Interrupted() { |
190 |
> |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
191 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
192 |
|
Thread t = new Thread(new Runnable() { |
193 |
|
public void run() { |
199 |
|
} catch (InterruptedException success) {} |
200 |
|
} |
201 |
|
}); |
202 |
< |
try { |
203 |
< |
lock.writeLock().lock(); |
204 |
< |
t.start(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
t.interrupt(); |
207 |
< |
Thread.sleep(SHORT_DELAY_MS); |
208 |
< |
lock.writeLock().unlock(); |
209 |
< |
t.join(); |
211 |
< |
} catch (Exception e) { |
212 |
< |
unexpectedException(); |
213 |
< |
} |
202 |
> |
|
203 |
> |
lock.writeLock().lock(); |
204 |
> |
t.start(); |
205 |
> |
Thread.sleep(SHORT_DELAY_MS); |
206 |
> |
t.interrupt(); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
lock.writeLock().unlock(); |
209 |
> |
t.join(); |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* timed write-tryLock is interruptible |
214 |
|
*/ |
215 |
< |
public void testWriteTryLock_Interrupted() { |
215 |
> |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
216 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
217 |
|
lock.writeLock().lock(); |
218 |
|
Thread t = new Thread(new Runnable() { |
222 |
|
} catch (InterruptedException success) {} |
223 |
|
} |
224 |
|
}); |
225 |
< |
try { |
226 |
< |
t.start(); |
227 |
< |
t.interrupt(); |
228 |
< |
lock.writeLock().unlock(); |
229 |
< |
t.join(); |
234 |
< |
} catch (Exception e) { |
235 |
< |
unexpectedException(); |
236 |
< |
} |
225 |
> |
|
226 |
> |
t.start(); |
227 |
> |
t.interrupt(); |
228 |
> |
lock.writeLock().unlock(); |
229 |
> |
t.join(); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
|
* read-lockInterruptibly is interruptible |
234 |
|
*/ |
235 |
< |
public void testReadLockInterruptibly_Interrupted() { |
235 |
> |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
236 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
237 |
|
lock.writeLock().lock(); |
238 |
|
Thread t = new Thread(new Runnable() { |
242 |
|
} catch (InterruptedException success) {} |
243 |
|
} |
244 |
|
}); |
245 |
< |
try { |
246 |
< |
t.start(); |
247 |
< |
Thread.sleep(SHORT_DELAY_MS); |
248 |
< |
t.interrupt(); |
249 |
< |
Thread.sleep(SHORT_DELAY_MS); |
250 |
< |
lock.writeLock().unlock(); |
251 |
< |
t.join(); |
259 |
< |
} catch (Exception e) { |
260 |
< |
unexpectedException(); |
261 |
< |
} |
245 |
> |
|
246 |
> |
t.start(); |
247 |
> |
Thread.sleep(SHORT_DELAY_MS); |
248 |
> |
t.interrupt(); |
249 |
> |
Thread.sleep(SHORT_DELAY_MS); |
250 |
> |
lock.writeLock().unlock(); |
251 |
> |
t.join(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
|
* timed read-tryLock is interruptible |
256 |
|
*/ |
257 |
< |
public void testReadTryLock_Interrupted() { |
257 |
> |
public void testReadTryLock_Interrupted() throws InterruptedException { |
258 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
259 |
|
lock.writeLock().lock(); |
260 |
|
Thread t = new Thread(new Runnable() { |
265 |
|
} catch (InterruptedException success) {} |
266 |
|
} |
267 |
|
}); |
268 |
< |
try { |
269 |
< |
t.start(); |
270 |
< |
t.interrupt(); |
271 |
< |
t.join(); |
282 |
< |
} catch (Exception e) { |
283 |
< |
unexpectedException(); |
284 |
< |
} |
268 |
> |
|
269 |
> |
t.start(); |
270 |
> |
t.interrupt(); |
271 |
> |
t.join(); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
/** |
276 |
|
* write-tryLock fails if locked |
277 |
|
*/ |
278 |
< |
public void testWriteTryLockWhenLocked() { |
278 |
> |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
279 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
280 |
|
lock.writeLock().lock(); |
281 |
|
Thread t = new Thread(new Runnable() { |
283 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
284 |
|
} |
285 |
|
}); |
286 |
< |
try { |
287 |
< |
t.start(); |
288 |
< |
t.join(); |
289 |
< |
lock.writeLock().unlock(); |
303 |
< |
} catch (Exception e) { |
304 |
< |
unexpectedException(); |
305 |
< |
} |
286 |
> |
|
287 |
> |
t.start(); |
288 |
> |
t.join(); |
289 |
> |
lock.writeLock().unlock(); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
|
* read-tryLock fails if locked |
294 |
|
*/ |
295 |
< |
public void testReadTryLockWhenLocked() { |
295 |
> |
public void testReadTryLockWhenLocked() throws InterruptedException { |
296 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
297 |
|
lock.writeLock().lock(); |
298 |
|
Thread t = new Thread(new Runnable() { |
300 |
|
threadAssertFalse(lock.readLock().tryLock()); |
301 |
|
} |
302 |
|
}); |
303 |
< |
try { |
304 |
< |
t.start(); |
305 |
< |
t.join(); |
306 |
< |
lock.writeLock().unlock(); |
323 |
< |
} catch (Exception e) { |
324 |
< |
unexpectedException(); |
325 |
< |
} |
303 |
> |
|
304 |
> |
t.start(); |
305 |
> |
t.join(); |
306 |
> |
lock.writeLock().unlock(); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
|
* Multiple threads can hold a read lock when not write-locked |
311 |
|
*/ |
312 |
< |
public void testMultipleReadLocks() { |
312 |
> |
public void testMultipleReadLocks() throws InterruptedException { |
313 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
314 |
|
lock.readLock().lock(); |
315 |
|
Thread t = new Thread(new Runnable() { |
318 |
|
lock.readLock().unlock(); |
319 |
|
} |
320 |
|
}); |
321 |
< |
try { |
322 |
< |
t.start(); |
323 |
< |
t.join(); |
324 |
< |
lock.readLock().unlock(); |
344 |
< |
} catch (Exception e) { |
345 |
< |
unexpectedException(); |
346 |
< |
} |
321 |
> |
|
322 |
> |
t.start(); |
323 |
> |
t.join(); |
324 |
> |
lock.readLock().unlock(); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
|
* A writelock succeeds after reading threads unlock |
329 |
|
*/ |
330 |
< |
public void testWriteAfterMultipleReadLocks() { |
330 |
> |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
331 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
332 |
|
lock.readLock().lock(); |
333 |
|
Thread t1 = new Thread(new Runnable() { |
343 |
|
} |
344 |
|
}); |
345 |
|
|
346 |
< |
try { |
347 |
< |
t1.start(); |
348 |
< |
t2.start(); |
349 |
< |
Thread.sleep(SHORT_DELAY_MS); |
350 |
< |
lock.readLock().unlock(); |
351 |
< |
t1.join(MEDIUM_DELAY_MS); |
352 |
< |
t2.join(MEDIUM_DELAY_MS); |
353 |
< |
assertTrue(!t1.isAlive()); |
376 |
< |
assertTrue(!t2.isAlive()); |
377 |
< |
|
378 |
< |
} catch (Exception e) { |
379 |
< |
unexpectedException(); |
380 |
< |
} |
346 |
> |
t1.start(); |
347 |
> |
t2.start(); |
348 |
> |
Thread.sleep(SHORT_DELAY_MS); |
349 |
> |
lock.readLock().unlock(); |
350 |
> |
t1.join(MEDIUM_DELAY_MS); |
351 |
> |
t2.join(MEDIUM_DELAY_MS); |
352 |
> |
assertTrue(!t1.isAlive()); |
353 |
> |
assertTrue(!t2.isAlive()); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* Readlocks succeed after a writing thread unlocks |
358 |
|
*/ |
359 |
< |
public void testReadAfterWriteLock() { |
359 |
> |
public void testReadAfterWriteLock() throws InterruptedException { |
360 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
361 |
|
lock.writeLock().lock(); |
362 |
|
Thread t1 = new Thread(new Runnable() { |
372 |
|
} |
373 |
|
}); |
374 |
|
|
375 |
< |
try { |
376 |
< |
t1.start(); |
377 |
< |
t2.start(); |
378 |
< |
Thread.sleep(SHORT_DELAY_MS); |
379 |
< |
lock.writeLock().unlock(); |
380 |
< |
t1.join(MEDIUM_DELAY_MS); |
381 |
< |
t2.join(MEDIUM_DELAY_MS); |
382 |
< |
assertTrue(!t1.isAlive()); |
410 |
< |
assertTrue(!t2.isAlive()); |
411 |
< |
|
412 |
< |
} catch (Exception e) { |
413 |
< |
unexpectedException(); |
414 |
< |
} |
375 |
> |
t1.start(); |
376 |
> |
t2.start(); |
377 |
> |
Thread.sleep(SHORT_DELAY_MS); |
378 |
> |
lock.writeLock().unlock(); |
379 |
> |
t1.join(MEDIUM_DELAY_MS); |
380 |
> |
t2.join(MEDIUM_DELAY_MS); |
381 |
> |
assertTrue(!t1.isAlive()); |
382 |
> |
assertTrue(!t2.isAlive()); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
397 |
|
* Read lock succeeds if write locked by current thread even if |
398 |
|
* other threads are waiting for readlock |
399 |
|
*/ |
400 |
< |
public void testReadHoldingWriteLock2() { |
400 |
> |
public void testReadHoldingWriteLock2() throws InterruptedException { |
401 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
402 |
|
lock.writeLock().lock(); |
403 |
|
Thread t1 = new Thread(new Runnable() { |
413 |
|
} |
414 |
|
}); |
415 |
|
|
416 |
< |
try { |
417 |
< |
t1.start(); |
418 |
< |
t2.start(); |
419 |
< |
lock.readLock().lock(); |
420 |
< |
lock.readLock().unlock(); |
421 |
< |
Thread.sleep(SHORT_DELAY_MS); |
422 |
< |
lock.readLock().lock(); |
423 |
< |
lock.readLock().unlock(); |
424 |
< |
lock.writeLock().unlock(); |
425 |
< |
t1.join(MEDIUM_DELAY_MS); |
426 |
< |
t2.join(MEDIUM_DELAY_MS); |
427 |
< |
assertTrue(!t1.isAlive()); |
460 |
< |
assertTrue(!t2.isAlive()); |
461 |
< |
|
462 |
< |
} catch (Exception e) { |
463 |
< |
unexpectedException(); |
464 |
< |
} |
416 |
> |
t1.start(); |
417 |
> |
t2.start(); |
418 |
> |
lock.readLock().lock(); |
419 |
> |
lock.readLock().unlock(); |
420 |
> |
Thread.sleep(SHORT_DELAY_MS); |
421 |
> |
lock.readLock().lock(); |
422 |
> |
lock.readLock().unlock(); |
423 |
> |
lock.writeLock().unlock(); |
424 |
> |
t1.join(MEDIUM_DELAY_MS); |
425 |
> |
t2.join(MEDIUM_DELAY_MS); |
426 |
> |
assertTrue(!t1.isAlive()); |
427 |
> |
assertTrue(!t2.isAlive()); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* Read lock succeeds if write locked by current thread even if |
432 |
|
* other threads are waiting for writelock |
433 |
|
*/ |
434 |
< |
public void testReadHoldingWriteLock3() { |
434 |
> |
public void testReadHoldingWriteLock3() throws InterruptedException { |
435 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
436 |
|
lock.writeLock().lock(); |
437 |
|
Thread t1 = new Thread(new Runnable() { |
447 |
|
} |
448 |
|
}); |
449 |
|
|
450 |
< |
try { |
451 |
< |
t1.start(); |
452 |
< |
t2.start(); |
453 |
< |
lock.readLock().lock(); |
454 |
< |
lock.readLock().unlock(); |
455 |
< |
Thread.sleep(SHORT_DELAY_MS); |
456 |
< |
lock.readLock().lock(); |
457 |
< |
lock.readLock().unlock(); |
458 |
< |
lock.writeLock().unlock(); |
459 |
< |
t1.join(MEDIUM_DELAY_MS); |
460 |
< |
t2.join(MEDIUM_DELAY_MS); |
461 |
< |
assertTrue(!t1.isAlive()); |
499 |
< |
assertTrue(!t2.isAlive()); |
500 |
< |
|
501 |
< |
} catch (Exception e) { |
502 |
< |
unexpectedException(); |
503 |
< |
} |
450 |
> |
t1.start(); |
451 |
> |
t2.start(); |
452 |
> |
lock.readLock().lock(); |
453 |
> |
lock.readLock().unlock(); |
454 |
> |
Thread.sleep(SHORT_DELAY_MS); |
455 |
> |
lock.readLock().lock(); |
456 |
> |
lock.readLock().unlock(); |
457 |
> |
lock.writeLock().unlock(); |
458 |
> |
t1.join(MEDIUM_DELAY_MS); |
459 |
> |
t2.join(MEDIUM_DELAY_MS); |
460 |
> |
assertTrue(!t1.isAlive()); |
461 |
> |
assertTrue(!t2.isAlive()); |
462 |
|
} |
463 |
|
|
464 |
|
|
466 |
|
* Write lock succeeds if write locked by current thread even if |
467 |
|
* other threads are waiting for writelock |
468 |
|
*/ |
469 |
< |
public void testWriteHoldingWriteLock4() { |
469 |
> |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
470 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
471 |
|
lock.writeLock().lock(); |
472 |
|
Thread t1 = new Thread(new Runnable() { |
482 |
|
} |
483 |
|
}); |
484 |
|
|
485 |
< |
try { |
486 |
< |
t1.start(); |
487 |
< |
t2.start(); |
488 |
< |
lock.writeLock().lock(); |
489 |
< |
lock.writeLock().unlock(); |
490 |
< |
Thread.sleep(SHORT_DELAY_MS); |
491 |
< |
lock.writeLock().lock(); |
492 |
< |
lock.writeLock().unlock(); |
493 |
< |
lock.writeLock().unlock(); |
494 |
< |
t1.join(MEDIUM_DELAY_MS); |
495 |
< |
t2.join(MEDIUM_DELAY_MS); |
496 |
< |
assertTrue(!t1.isAlive()); |
539 |
< |
assertTrue(!t2.isAlive()); |
540 |
< |
|
541 |
< |
} catch (Exception e) { |
542 |
< |
unexpectedException(); |
543 |
< |
} |
485 |
> |
t1.start(); |
486 |
> |
t2.start(); |
487 |
> |
lock.writeLock().lock(); |
488 |
> |
lock.writeLock().unlock(); |
489 |
> |
Thread.sleep(SHORT_DELAY_MS); |
490 |
> |
lock.writeLock().lock(); |
491 |
> |
lock.writeLock().unlock(); |
492 |
> |
lock.writeLock().unlock(); |
493 |
> |
t1.join(MEDIUM_DELAY_MS); |
494 |
> |
t2.join(MEDIUM_DELAY_MS); |
495 |
> |
assertTrue(!t1.isAlive()); |
496 |
> |
assertTrue(!t2.isAlive()); |
497 |
|
} |
498 |
|
|
499 |
|
|
512 |
|
* Fair Read lock succeeds if write locked by current thread even if |
513 |
|
* other threads are waiting for readlock |
514 |
|
*/ |
515 |
< |
public void testReadHoldingWriteLockFair2() { |
515 |
> |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
516 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
517 |
|
lock.writeLock().lock(); |
518 |
|
Thread t1 = new Thread(new Runnable() { |
528 |
|
} |
529 |
|
}); |
530 |
|
|
531 |
< |
try { |
532 |
< |
t1.start(); |
533 |
< |
t2.start(); |
534 |
< |
lock.readLock().lock(); |
535 |
< |
lock.readLock().unlock(); |
536 |
< |
Thread.sleep(SHORT_DELAY_MS); |
537 |
< |
lock.readLock().lock(); |
538 |
< |
lock.readLock().unlock(); |
539 |
< |
lock.writeLock().unlock(); |
540 |
< |
t1.join(MEDIUM_DELAY_MS); |
541 |
< |
t2.join(MEDIUM_DELAY_MS); |
542 |
< |
assertTrue(!t1.isAlive()); |
590 |
< |
assertTrue(!t2.isAlive()); |
591 |
< |
|
592 |
< |
} catch (Exception e) { |
593 |
< |
unexpectedException(); |
594 |
< |
} |
531 |
> |
t1.start(); |
532 |
> |
t2.start(); |
533 |
> |
lock.readLock().lock(); |
534 |
> |
lock.readLock().unlock(); |
535 |
> |
Thread.sleep(SHORT_DELAY_MS); |
536 |
> |
lock.readLock().lock(); |
537 |
> |
lock.readLock().unlock(); |
538 |
> |
lock.writeLock().unlock(); |
539 |
> |
t1.join(MEDIUM_DELAY_MS); |
540 |
> |
t2.join(MEDIUM_DELAY_MS); |
541 |
> |
assertTrue(!t1.isAlive()); |
542 |
> |
assertTrue(!t2.isAlive()); |
543 |
|
} |
544 |
|
|
545 |
|
|
547 |
|
* Fair Read lock succeeds if write locked by current thread even if |
548 |
|
* other threads are waiting for writelock |
549 |
|
*/ |
550 |
< |
public void testReadHoldingWriteLockFair3() { |
550 |
> |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
551 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
552 |
|
lock.writeLock().lock(); |
553 |
|
Thread t1 = new Thread(new Runnable() { |
563 |
|
} |
564 |
|
}); |
565 |
|
|
566 |
< |
try { |
567 |
< |
t1.start(); |
568 |
< |
t2.start(); |
569 |
< |
lock.readLock().lock(); |
570 |
< |
lock.readLock().unlock(); |
571 |
< |
Thread.sleep(SHORT_DELAY_MS); |
572 |
< |
lock.readLock().lock(); |
573 |
< |
lock.readLock().unlock(); |
574 |
< |
lock.writeLock().unlock(); |
575 |
< |
t1.join(MEDIUM_DELAY_MS); |
576 |
< |
t2.join(MEDIUM_DELAY_MS); |
577 |
< |
assertTrue(!t1.isAlive()); |
630 |
< |
assertTrue(!t2.isAlive()); |
631 |
< |
|
632 |
< |
} catch (Exception e) { |
633 |
< |
unexpectedException(); |
634 |
< |
} |
566 |
> |
t1.start(); |
567 |
> |
t2.start(); |
568 |
> |
lock.readLock().lock(); |
569 |
> |
lock.readLock().unlock(); |
570 |
> |
Thread.sleep(SHORT_DELAY_MS); |
571 |
> |
lock.readLock().lock(); |
572 |
> |
lock.readLock().unlock(); |
573 |
> |
lock.writeLock().unlock(); |
574 |
> |
t1.join(MEDIUM_DELAY_MS); |
575 |
> |
t2.join(MEDIUM_DELAY_MS); |
576 |
> |
assertTrue(!t1.isAlive()); |
577 |
> |
assertTrue(!t2.isAlive()); |
578 |
|
} |
579 |
|
|
580 |
|
|
582 |
|
* Fair Write lock succeeds if write locked by current thread even if |
583 |
|
* other threads are waiting for writelock |
584 |
|
*/ |
585 |
< |
public void testWriteHoldingWriteLockFair4() { |
585 |
> |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
586 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
587 |
|
lock.writeLock().lock(); |
588 |
|
Thread t1 = new Thread(new Runnable() { |
598 |
|
} |
599 |
|
}); |
600 |
|
|
601 |
< |
try { |
602 |
< |
t1.start(); |
603 |
< |
t2.start(); |
604 |
< |
Thread.sleep(SHORT_DELAY_MS); |
605 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
606 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
607 |
< |
lock.writeLock().lock(); |
608 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
609 |
< |
lock.writeLock().unlock(); |
610 |
< |
lock.writeLock().lock(); |
611 |
< |
lock.writeLock().unlock(); |
612 |
< |
lock.writeLock().unlock(); |
613 |
< |
t1.join(MEDIUM_DELAY_MS); |
614 |
< |
t2.join(MEDIUM_DELAY_MS); |
615 |
< |
assertTrue(!t1.isAlive()); |
673 |
< |
assertTrue(!t2.isAlive()); |
674 |
< |
|
675 |
< |
} catch (Exception e) { |
676 |
< |
unexpectedException(); |
677 |
< |
} |
601 |
> |
t1.start(); |
602 |
> |
t2.start(); |
603 |
> |
Thread.sleep(SHORT_DELAY_MS); |
604 |
> |
assertTrue(lock.isWriteLockedByCurrentThread()); |
605 |
> |
assertTrue(lock.getWriteHoldCount() == 1); |
606 |
> |
lock.writeLock().lock(); |
607 |
> |
assertTrue(lock.getWriteHoldCount() == 2); |
608 |
> |
lock.writeLock().unlock(); |
609 |
> |
lock.writeLock().lock(); |
610 |
> |
lock.writeLock().unlock(); |
611 |
> |
lock.writeLock().unlock(); |
612 |
> |
t1.join(MEDIUM_DELAY_MS); |
613 |
> |
t2.join(MEDIUM_DELAY_MS); |
614 |
> |
assertTrue(!t1.isAlive()); |
615 |
> |
assertTrue(!t2.isAlive()); |
616 |
|
} |
617 |
|
|
618 |
|
|
619 |
|
/** |
620 |
|
* Read tryLock succeeds if readlocked but not writelocked |
621 |
|
*/ |
622 |
< |
public void testTryLockWhenReadLocked() { |
622 |
> |
public void testTryLockWhenReadLocked() throws InterruptedException { |
623 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
624 |
|
lock.readLock().lock(); |
625 |
|
Thread t = new Thread(new Runnable() { |
628 |
|
lock.readLock().unlock(); |
629 |
|
} |
630 |
|
}); |
631 |
< |
try { |
632 |
< |
t.start(); |
633 |
< |
t.join(); |
634 |
< |
lock.readLock().unlock(); |
697 |
< |
} catch (Exception e) { |
698 |
< |
unexpectedException(); |
699 |
< |
} |
631 |
> |
|
632 |
> |
t.start(); |
633 |
> |
t.join(); |
634 |
> |
lock.readLock().unlock(); |
635 |
|
} |
636 |
|
|
637 |
|
|
639 |
|
/** |
640 |
|
* write tryLock fails when readlocked |
641 |
|
*/ |
642 |
< |
public void testWriteTryLockWhenReadLocked() { |
642 |
> |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
643 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
644 |
|
lock.readLock().lock(); |
645 |
|
Thread t = new Thread(new Runnable() { |
647 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
648 |
|
} |
649 |
|
}); |
650 |
< |
try { |
651 |
< |
t.start(); |
652 |
< |
t.join(); |
653 |
< |
lock.readLock().unlock(); |
719 |
< |
} catch (Exception e) { |
720 |
< |
unexpectedException(); |
721 |
< |
} |
650 |
> |
|
651 |
> |
t.start(); |
652 |
> |
t.join(); |
653 |
> |
lock.readLock().unlock(); |
654 |
|
} |
655 |
|
|
656 |
|
|
657 |
|
/** |
658 |
|
* Fair Read tryLock succeeds if readlocked but not writelocked |
659 |
|
*/ |
660 |
< |
public void testTryLockWhenReadLockedFair() { |
660 |
> |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
661 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
662 |
|
lock.readLock().lock(); |
663 |
|
Thread t = new Thread(new Runnable() { |
666 |
|
lock.readLock().unlock(); |
667 |
|
} |
668 |
|
}); |
669 |
< |
try { |
670 |
< |
t.start(); |
671 |
< |
t.join(); |
672 |
< |
lock.readLock().unlock(); |
741 |
< |
} catch (Exception e) { |
742 |
< |
unexpectedException(); |
743 |
< |
} |
669 |
> |
|
670 |
> |
t.start(); |
671 |
> |
t.join(); |
672 |
> |
lock.readLock().unlock(); |
673 |
|
} |
674 |
|
|
675 |
|
|
677 |
|
/** |
678 |
|
* Fair write tryLock fails when readlocked |
679 |
|
*/ |
680 |
< |
public void testWriteTryLockWhenReadLockedFair() { |
680 |
> |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
681 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
682 |
|
lock.readLock().lock(); |
683 |
|
Thread t = new Thread(new Runnable() { |
685 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
686 |
|
} |
687 |
|
}); |
688 |
< |
try { |
689 |
< |
t.start(); |
690 |
< |
t.join(); |
691 |
< |
lock.readLock().unlock(); |
763 |
< |
} catch (Exception e) { |
764 |
< |
unexpectedException(); |
765 |
< |
} |
688 |
> |
|
689 |
> |
t.start(); |
690 |
> |
t.join(); |
691 |
> |
lock.readLock().unlock(); |
692 |
|
} |
693 |
|
|
694 |
|
|
696 |
|
/** |
697 |
|
* write timed tryLock times out if locked |
698 |
|
*/ |
699 |
< |
public void testWriteTryLock_Timeout() { |
699 |
> |
public void testWriteTryLock_Timeout() throws InterruptedException { |
700 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
701 |
|
lock.writeLock().lock(); |
702 |
|
Thread t = new Thread(new Runnable() { |
708 |
|
} |
709 |
|
} |
710 |
|
}); |
711 |
< |
try { |
712 |
< |
t.start(); |
713 |
< |
t.join(); |
714 |
< |
lock.writeLock().unlock(); |
789 |
< |
} catch (Exception e) { |
790 |
< |
unexpectedException(); |
791 |
< |
} |
711 |
> |
|
712 |
> |
t.start(); |
713 |
> |
t.join(); |
714 |
> |
lock.writeLock().unlock(); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* read timed tryLock times out if write-locked |
719 |
|
*/ |
720 |
< |
public void testReadTryLock_Timeout() { |
720 |
> |
public void testReadTryLock_Timeout() throws InterruptedException { |
721 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
722 |
|
lock.writeLock().lock(); |
723 |
|
Thread t = new Thread(new Runnable() { |
729 |
|
} |
730 |
|
} |
731 |
|
}); |
732 |
< |
try { |
733 |
< |
t.start(); |
734 |
< |
t.join(); |
735 |
< |
lock.writeLock().unlock(); |
813 |
< |
} catch (Exception e) { |
814 |
< |
unexpectedException(); |
815 |
< |
} |
732 |
> |
|
733 |
> |
t.start(); |
734 |
> |
t.join(); |
735 |
> |
lock.writeLock().unlock(); |
736 |
|
} |
737 |
|
|
738 |
|
|
739 |
|
/** |
740 |
|
* write lockInterruptibly succeeds if lock free else is interruptible |
741 |
|
*/ |
742 |
< |
public void testWriteLockInterruptibly() { |
742 |
> |
public void testWriteLockInterruptibly() throws InterruptedException { |
743 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
744 |
< |
try { |
825 |
< |
lock.writeLock().lockInterruptibly(); |
826 |
< |
} catch (Exception e) { |
827 |
< |
unexpectedException(); |
828 |
< |
} |
744 |
> |
lock.writeLock().lockInterruptibly(); |
745 |
|
Thread t = new Thread(new Runnable() { |
746 |
|
public void run() { |
747 |
|
try { |
752 |
|
} |
753 |
|
} |
754 |
|
}); |
755 |
< |
try { |
756 |
< |
t.start(); |
757 |
< |
Thread.sleep(SHORT_DELAY_MS); |
758 |
< |
t.interrupt(); |
759 |
< |
Thread.sleep(SHORT_DELAY_MS); |
760 |
< |
t.join(); |
761 |
< |
lock.writeLock().unlock(); |
846 |
< |
} catch (Exception e) { |
847 |
< |
unexpectedException(); |
848 |
< |
} |
755 |
> |
|
756 |
> |
t.start(); |
757 |
> |
Thread.sleep(SHORT_DELAY_MS); |
758 |
> |
t.interrupt(); |
759 |
> |
Thread.sleep(SHORT_DELAY_MS); |
760 |
> |
t.join(); |
761 |
> |
lock.writeLock().unlock(); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
|
* read lockInterruptibly succeeds if lock free else is interruptible |
766 |
|
*/ |
767 |
< |
public void testReadLockInterruptibly() { |
767 |
> |
public void testReadLockInterruptibly() throws InterruptedException { |
768 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
769 |
< |
try { |
857 |
< |
lock.writeLock().lockInterruptibly(); |
858 |
< |
} catch (Exception e) { |
859 |
< |
unexpectedException(); |
860 |
< |
} |
769 |
> |
lock.writeLock().lockInterruptibly(); |
770 |
|
Thread t = new Thread(new Runnable() { |
771 |
|
public void run() { |
772 |
|
try { |
777 |
|
} |
778 |
|
} |
779 |
|
}); |
780 |
< |
try { |
781 |
< |
t.start(); |
782 |
< |
Thread.sleep(SHORT_DELAY_MS); |
783 |
< |
t.interrupt(); |
784 |
< |
t.join(); |
785 |
< |
lock.writeLock().unlock(); |
877 |
< |
} catch (Exception e) { |
878 |
< |
unexpectedException(); |
879 |
< |
} |
780 |
> |
|
781 |
> |
t.start(); |
782 |
> |
Thread.sleep(SHORT_DELAY_MS); |
783 |
> |
t.interrupt(); |
784 |
> |
t.join(); |
785 |
> |
lock.writeLock().unlock(); |
786 |
|
} |
787 |
|
|
788 |
|
/** |
789 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
790 |
|
*/ |
791 |
< |
public void testAwait_IllegalMonitor() { |
791 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
792 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
793 |
|
final Condition c = lock.writeLock().newCondition(); |
794 |
|
try { |
795 |
|
c.await(); |
796 |
|
shouldThrow(); |
797 |
< |
} |
892 |
< |
catch (IllegalMonitorStateException success) { |
893 |
< |
} |
894 |
< |
catch (Exception ex) { |
895 |
< |
shouldThrow(); |
896 |
< |
} |
797 |
> |
} catch (IllegalMonitorStateException success) {} |
798 |
|
} |
799 |
|
|
800 |
|
/** |
806 |
|
try { |
807 |
|
c.signal(); |
808 |
|
shouldThrow(); |
809 |
< |
} |
909 |
< |
catch (IllegalMonitorStateException success) { |
910 |
< |
} |
911 |
< |
catch (Exception ex) { |
912 |
< |
unexpectedException(); |
913 |
< |
} |
809 |
> |
} catch (IllegalMonitorStateException success) {} |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* awaitNanos without a signal times out |
814 |
|
*/ |
815 |
< |
public void testAwaitNanos_Timeout() { |
815 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
816 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
817 |
|
final Condition c = lock.writeLock().newCondition(); |
818 |
< |
try { |
819 |
< |
lock.writeLock().lock(); |
820 |
< |
long t = c.awaitNanos(100); |
821 |
< |
assertTrue(t <= 0); |
822 |
< |
lock.writeLock().unlock(); |
927 |
< |
} |
928 |
< |
catch (Exception ex) { |
929 |
< |
unexpectedException(); |
930 |
< |
} |
818 |
> |
|
819 |
> |
lock.writeLock().lock(); |
820 |
> |
long t = c.awaitNanos(100); |
821 |
> |
assertTrue(t <= 0); |
822 |
> |
lock.writeLock().unlock(); |
823 |
|
} |
824 |
|
|
825 |
|
|
829 |
|
public void testAwait_Timeout() { |
830 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
831 |
|
final Condition c = lock.writeLock().newCondition(); |
832 |
< |
try { |
833 |
< |
lock.writeLock().lock(); |
942 |
< |
lock.writeLock().unlock(); |
943 |
< |
} |
944 |
< |
catch (Exception ex) { |
945 |
< |
unexpectedException(); |
946 |
< |
} |
832 |
> |
lock.writeLock().lock(); |
833 |
> |
lock.writeLock().unlock(); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
839 |
|
public void testAwaitUntil_Timeout() { |
840 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
841 |
|
final Condition c = lock.writeLock().newCondition(); |
842 |
< |
try { |
843 |
< |
lock.writeLock().lock(); |
844 |
< |
java.util.Date d = new java.util.Date(); |
958 |
< |
lock.writeLock().unlock(); |
959 |
< |
} |
960 |
< |
catch (Exception ex) { |
961 |
< |
unexpectedException(); |
962 |
< |
} |
842 |
> |
lock.writeLock().lock(); |
843 |
> |
java.util.Date d = new java.util.Date(); |
844 |
> |
lock.writeLock().unlock(); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
848 |
|
* await returns when signalled |
849 |
|
*/ |
850 |
< |
public void testAwait() { |
850 |
> |
public void testAwait() throws InterruptedException { |
851 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
852 |
|
final Condition c = lock.writeLock().newCondition(); |
853 |
|
Thread t = new Thread(new Runnable() { |
863 |
|
} |
864 |
|
}); |
865 |
|
|
866 |
< |
try { |
867 |
< |
t.start(); |
868 |
< |
Thread.sleep(SHORT_DELAY_MS); |
869 |
< |
lock.writeLock().lock(); |
870 |
< |
c.signal(); |
871 |
< |
lock.writeLock().unlock(); |
872 |
< |
t.join(SHORT_DELAY_MS); |
991 |
< |
assertFalse(t.isAlive()); |
992 |
< |
} |
993 |
< |
catch (Exception ex) { |
994 |
< |
unexpectedException(); |
995 |
< |
} |
866 |
> |
t.start(); |
867 |
> |
Thread.sleep(SHORT_DELAY_MS); |
868 |
> |
lock.writeLock().lock(); |
869 |
> |
c.signal(); |
870 |
> |
lock.writeLock().unlock(); |
871 |
> |
t.join(SHORT_DELAY_MS); |
872 |
> |
assertFalse(t.isAlive()); |
873 |
|
} |
874 |
|
|
875 |
|
/** A helper class for uninterruptible wait tests */ |
902 |
|
/** |
903 |
|
* awaitUninterruptibly doesn't abort on interrupt |
904 |
|
*/ |
905 |
< |
public void testAwaitUninterruptibly() { |
905 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
906 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
907 |
|
final Condition c = lock.writeLock().newCondition(); |
908 |
|
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
909 |
|
|
910 |
< |
try { |
1034 |
< |
thread.start(); |
1035 |
< |
|
1036 |
< |
while (!thread.lockStarted) { |
1037 |
< |
Thread.sleep(100); |
1038 |
< |
} |
910 |
> |
thread.start(); |
911 |
|
|
912 |
< |
lock.writeLock().lock(); |
913 |
< |
try { |
914 |
< |
thread.interrupt(); |
1043 |
< |
thread.canAwake = true; |
1044 |
< |
c.signal(); |
1045 |
< |
} finally { |
1046 |
< |
lock.writeLock().unlock(); |
1047 |
< |
} |
912 |
> |
while (!thread.lockStarted) { |
913 |
> |
Thread.sleep(100); |
914 |
> |
} |
915 |
|
|
916 |
< |
thread.join(); |
917 |
< |
assertTrue(thread.interrupted); |
918 |
< |
assertFalse(thread.isAlive()); |
919 |
< |
} catch (Exception ex) { |
920 |
< |
unexpectedException(); |
916 |
> |
lock.writeLock().lock(); |
917 |
> |
try { |
918 |
> |
thread.interrupt(); |
919 |
> |
thread.canAwake = true; |
920 |
> |
c.signal(); |
921 |
> |
} finally { |
922 |
> |
lock.writeLock().unlock(); |
923 |
|
} |
924 |
+ |
|
925 |
+ |
thread.join(); |
926 |
+ |
assertTrue(thread.interrupted); |
927 |
+ |
assertFalse(thread.isAlive()); |
928 |
|
} |
929 |
|
|
930 |
|
/** |
931 |
|
* await is interruptible |
932 |
|
*/ |
933 |
< |
public void testAwait_Interrupt() { |
933 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
934 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
935 |
|
final Condition c = lock.writeLock().newCondition(); |
936 |
|
Thread t = new Thread(new Runnable() { |
946 |
|
} |
947 |
|
}); |
948 |
|
|
949 |
< |
try { |
950 |
< |
t.start(); |
951 |
< |
Thread.sleep(SHORT_DELAY_MS); |
952 |
< |
t.interrupt(); |
953 |
< |
t.join(SHORT_DELAY_MS); |
1081 |
< |
assertFalse(t.isAlive()); |
1082 |
< |
} |
1083 |
< |
catch (Exception ex) { |
1084 |
< |
unexpectedException(); |
1085 |
< |
} |
949 |
> |
t.start(); |
950 |
> |
Thread.sleep(SHORT_DELAY_MS); |
951 |
> |
t.interrupt(); |
952 |
> |
t.join(SHORT_DELAY_MS); |
953 |
> |
assertFalse(t.isAlive()); |
954 |
|
} |
955 |
|
|
956 |
|
/** |
957 |
|
* awaitNanos is interruptible |
958 |
|
*/ |
959 |
< |
public void testAwaitNanos_Interrupt() { |
959 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
960 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
961 |
|
final Condition c = lock.writeLock().newCondition(); |
962 |
|
Thread t = new Thread(new Runnable() { |
972 |
|
} |
973 |
|
}); |
974 |
|
|
975 |
< |
try { |
976 |
< |
t.start(); |
977 |
< |
Thread.sleep(SHORT_DELAY_MS); |
978 |
< |
t.interrupt(); |
979 |
< |
t.join(SHORT_DELAY_MS); |
1112 |
< |
assertFalse(t.isAlive()); |
1113 |
< |
} |
1114 |
< |
catch (Exception ex) { |
1115 |
< |
unexpectedException(); |
1116 |
< |
} |
975 |
> |
t.start(); |
976 |
> |
Thread.sleep(SHORT_DELAY_MS); |
977 |
> |
t.interrupt(); |
978 |
> |
t.join(SHORT_DELAY_MS); |
979 |
> |
assertFalse(t.isAlive()); |
980 |
|
} |
981 |
|
|
982 |
|
/** |
983 |
|
* awaitUntil is interruptible |
984 |
|
*/ |
985 |
< |
public void testAwaitUntil_Interrupt() { |
985 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
986 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
987 |
|
final Condition c = lock.writeLock().newCondition(); |
988 |
|
Thread t = new Thread(new Runnable() { |
999 |
|
} |
1000 |
|
}); |
1001 |
|
|
1002 |
< |
try { |
1003 |
< |
t.start(); |
1004 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1005 |
< |
t.interrupt(); |
1006 |
< |
t.join(SHORT_DELAY_MS); |
1144 |
< |
assertFalse(t.isAlive()); |
1145 |
< |
} |
1146 |
< |
catch (Exception ex) { |
1147 |
< |
unexpectedException(); |
1148 |
< |
} |
1002 |
> |
t.start(); |
1003 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1004 |
> |
t.interrupt(); |
1005 |
> |
t.join(SHORT_DELAY_MS); |
1006 |
> |
assertFalse(t.isAlive()); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1010 |
|
* signalAll wakes up all threads |
1011 |
|
*/ |
1012 |
< |
public void testSignalAll() { |
1012 |
> |
public void testSignalAll() throws InterruptedException { |
1013 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1014 |
|
final Condition c = lock.writeLock().newCondition(); |
1015 |
|
Thread t1 = new Thread(new Runnable() { |
1038 |
|
} |
1039 |
|
}); |
1040 |
|
|
1041 |
< |
try { |
1042 |
< |
t1.start(); |
1043 |
< |
t2.start(); |
1044 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1045 |
< |
lock.writeLock().lock(); |
1046 |
< |
c.signalAll(); |
1047 |
< |
lock.writeLock().unlock(); |
1048 |
< |
t1.join(SHORT_DELAY_MS); |
1049 |
< |
t2.join(SHORT_DELAY_MS); |
1050 |
< |
assertFalse(t1.isAlive()); |
1193 |
< |
assertFalse(t2.isAlive()); |
1194 |
< |
} |
1195 |
< |
catch (Exception ex) { |
1196 |
< |
unexpectedException(); |
1197 |
< |
} |
1041 |
> |
t1.start(); |
1042 |
> |
t2.start(); |
1043 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1044 |
> |
lock.writeLock().lock(); |
1045 |
> |
c.signalAll(); |
1046 |
> |
lock.writeLock().unlock(); |
1047 |
> |
t1.join(SHORT_DELAY_MS); |
1048 |
> |
t2.join(SHORT_DELAY_MS); |
1049 |
> |
assertFalse(t1.isAlive()); |
1050 |
> |
assertFalse(t2.isAlive()); |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1054 |
|
* A serialized lock deserializes as unlocked |
1055 |
|
*/ |
1056 |
< |
public void testSerialization() { |
1056 |
> |
public void testSerialization() throws Exception { |
1057 |
|
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
1058 |
|
l.readLock().lock(); |
1059 |
|
l.readLock().unlock(); |
1060 |
|
|
1061 |
< |
try { |
1062 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1063 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1064 |
< |
out.writeObject(l); |
1065 |
< |
out.close(); |
1066 |
< |
|
1067 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1068 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1069 |
< |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1070 |
< |
r.readLock().lock(); |
1218 |
< |
r.readLock().unlock(); |
1219 |
< |
} catch (Exception e) { |
1220 |
< |
e.printStackTrace(); |
1221 |
< |
unexpectedException(); |
1222 |
< |
} |
1061 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1062 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1063 |
> |
out.writeObject(l); |
1064 |
> |
out.close(); |
1065 |
> |
|
1066 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1067 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1068 |
> |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1069 |
> |
r.readLock().lock(); |
1070 |
> |
r.readLock().unlock(); |
1071 |
|
} |
1072 |
|
|
1073 |
|
/** |
1074 |
|
* hasQueuedThreads reports whether there are waiting threads |
1075 |
|
*/ |
1076 |
< |
public void testhasQueuedThreads() { |
1076 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
1077 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1078 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1079 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1080 |
< |
try { |
1081 |
< |
assertFalse(lock.hasQueuedThreads()); |
1082 |
< |
lock.writeLock().lock(); |
1083 |
< |
t1.start(); |
1084 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1085 |
< |
assertTrue(lock.hasQueuedThreads()); |
1086 |
< |
t2.start(); |
1087 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1088 |
< |
assertTrue(lock.hasQueuedThreads()); |
1089 |
< |
t1.interrupt(); |
1090 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1091 |
< |
assertTrue(lock.hasQueuedThreads()); |
1092 |
< |
lock.writeLock().unlock(); |
1093 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1094 |
< |
assertFalse(lock.hasQueuedThreads()); |
1095 |
< |
t1.join(); |
1248 |
< |
t2.join(); |
1249 |
< |
} catch (Exception e) { |
1250 |
< |
unexpectedException(); |
1251 |
< |
} |
1080 |
> |
assertFalse(lock.hasQueuedThreads()); |
1081 |
> |
lock.writeLock().lock(); |
1082 |
> |
t1.start(); |
1083 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1084 |
> |
assertTrue(lock.hasQueuedThreads()); |
1085 |
> |
t2.start(); |
1086 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1087 |
> |
assertTrue(lock.hasQueuedThreads()); |
1088 |
> |
t1.interrupt(); |
1089 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1090 |
> |
assertTrue(lock.hasQueuedThreads()); |
1091 |
> |
lock.writeLock().unlock(); |
1092 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1093 |
> |
assertFalse(lock.hasQueuedThreads()); |
1094 |
> |
t1.join(); |
1095 |
> |
t2.join(); |
1096 |
|
} |
1097 |
|
|
1098 |
|
/** |
1110 |
|
/** |
1111 |
|
* hasQueuedThread reports whether a thread is queued. |
1112 |
|
*/ |
1113 |
< |
public void testHasQueuedThread() { |
1113 |
> |
public void testHasQueuedThread() throws InterruptedException { |
1114 |
|
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1115 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1116 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1117 |
< |
try { |
1118 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1119 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1120 |
< |
sync.writeLock().lock(); |
1121 |
< |
t1.start(); |
1122 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1123 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1124 |
< |
t2.start(); |
1125 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1126 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1127 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1128 |
< |
t1.interrupt(); |
1129 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1130 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1131 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1132 |
< |
sync.writeLock().unlock(); |
1133 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1134 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1135 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1136 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1137 |
< |
t1.join(); |
1294 |
< |
t2.join(); |
1295 |
< |
} catch (Exception e) { |
1296 |
< |
unexpectedException(); |
1297 |
< |
} |
1117 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1118 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1119 |
> |
sync.writeLock().lock(); |
1120 |
> |
t1.start(); |
1121 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1122 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1123 |
> |
t2.start(); |
1124 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1125 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1126 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1127 |
> |
t1.interrupt(); |
1128 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1129 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1130 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1131 |
> |
sync.writeLock().unlock(); |
1132 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1133 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1134 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1135 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1136 |
> |
t1.join(); |
1137 |
> |
t2.join(); |
1138 |
|
} |
1139 |
|
|
1140 |
|
|
1141 |
|
/** |
1142 |
|
* getQueueLength reports number of waiting threads |
1143 |
|
*/ |
1144 |
< |
public void testGetQueueLength() { |
1144 |
> |
public void testGetQueueLength() throws InterruptedException { |
1145 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1146 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1147 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1148 |
< |
try { |
1149 |
< |
assertEquals(0, lock.getQueueLength()); |
1150 |
< |
lock.writeLock().lock(); |
1151 |
< |
t1.start(); |
1152 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1153 |
< |
assertEquals(1, lock.getQueueLength()); |
1154 |
< |
t2.start(); |
1155 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1156 |
< |
assertEquals(2, lock.getQueueLength()); |
1157 |
< |
t1.interrupt(); |
1158 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1159 |
< |
assertEquals(1, lock.getQueueLength()); |
1160 |
< |
lock.writeLock().unlock(); |
1161 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1162 |
< |
assertEquals(0, lock.getQueueLength()); |
1163 |
< |
t1.join(); |
1324 |
< |
t2.join(); |
1325 |
< |
} catch (Exception e) { |
1326 |
< |
unexpectedException(); |
1327 |
< |
} |
1148 |
> |
assertEquals(0, lock.getQueueLength()); |
1149 |
> |
lock.writeLock().lock(); |
1150 |
> |
t1.start(); |
1151 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1152 |
> |
assertEquals(1, lock.getQueueLength()); |
1153 |
> |
t2.start(); |
1154 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1155 |
> |
assertEquals(2, lock.getQueueLength()); |
1156 |
> |
t1.interrupt(); |
1157 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1158 |
> |
assertEquals(1, lock.getQueueLength()); |
1159 |
> |
lock.writeLock().unlock(); |
1160 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1161 |
> |
assertEquals(0, lock.getQueueLength()); |
1162 |
> |
t1.join(); |
1163 |
> |
t2.join(); |
1164 |
|
} |
1165 |
|
|
1166 |
|
/** |
1167 |
|
* getQueuedThreads includes waiting threads |
1168 |
|
*/ |
1169 |
< |
public void testGetQueuedThreads() { |
1169 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
1170 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1171 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1172 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1173 |
< |
try { |
1174 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1175 |
< |
lock.writeLock().lock(); |
1176 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1177 |
< |
t1.start(); |
1178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1179 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1180 |
< |
t2.start(); |
1181 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1182 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1183 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1184 |
< |
t1.interrupt(); |
1185 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1186 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1187 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1188 |
< |
lock.writeLock().unlock(); |
1189 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1190 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1191 |
< |
t1.join(); |
1356 |
< |
t2.join(); |
1357 |
< |
} catch (Exception e) { |
1358 |
< |
unexpectedException(); |
1359 |
< |
} |
1173 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1174 |
> |
lock.writeLock().lock(); |
1175 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1176 |
> |
t1.start(); |
1177 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1178 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1179 |
> |
t2.start(); |
1180 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1181 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1182 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1183 |
> |
t1.interrupt(); |
1184 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1185 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1186 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1187 |
> |
lock.writeLock().unlock(); |
1188 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1189 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1190 |
> |
t1.join(); |
1191 |
> |
t2.join(); |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1199 |
|
try { |
1200 |
|
lock.hasWaiters(null); |
1201 |
|
shouldThrow(); |
1202 |
< |
} catch (NullPointerException success) { |
1371 |
< |
} catch (Exception ex) { |
1372 |
< |
unexpectedException(); |
1373 |
< |
} |
1202 |
> |
} catch (NullPointerException success) {} |
1203 |
|
} |
1204 |
|
|
1205 |
|
/** |
1210 |
|
try { |
1211 |
|
lock.getWaitQueueLength(null); |
1212 |
|
shouldThrow(); |
1213 |
< |
} catch (NullPointerException success) { |
1385 |
< |
} catch (Exception ex) { |
1386 |
< |
unexpectedException(); |
1387 |
< |
} |
1213 |
> |
} catch (NullPointerException success) {} |
1214 |
|
} |
1215 |
|
|
1216 |
|
|
1222 |
|
try { |
1223 |
|
lock.getWaitingThreads(null); |
1224 |
|
shouldThrow(); |
1225 |
< |
} catch (NullPointerException success) { |
1400 |
< |
} catch (Exception ex) { |
1401 |
< |
unexpectedException(); |
1402 |
< |
} |
1225 |
> |
} catch (NullPointerException success) {} |
1226 |
|
} |
1227 |
|
|
1228 |
|
/** |
1235 |
|
try { |
1236 |
|
lock2.hasWaiters(c); |
1237 |
|
shouldThrow(); |
1238 |
< |
} catch (IllegalArgumentException success) { |
1416 |
< |
} catch (Exception ex) { |
1417 |
< |
unexpectedException(); |
1418 |
< |
} |
1238 |
> |
} catch (IllegalArgumentException success) {} |
1239 |
|
} |
1240 |
|
|
1241 |
|
/** |
1247 |
|
try { |
1248 |
|
lock.hasWaiters(c); |
1249 |
|
shouldThrow(); |
1250 |
< |
} catch (IllegalMonitorStateException success) { |
1431 |
< |
} catch (Exception ex) { |
1432 |
< |
unexpectedException(); |
1433 |
< |
} |
1250 |
> |
} catch (IllegalMonitorStateException success) {} |
1251 |
|
} |
1252 |
|
|
1253 |
|
|
1261 |
|
try { |
1262 |
|
lock2.getWaitQueueLength(c); |
1263 |
|
shouldThrow(); |
1264 |
< |
} catch (IllegalArgumentException success) { |
1448 |
< |
} catch (Exception ex) { |
1449 |
< |
unexpectedException(); |
1450 |
< |
} |
1264 |
> |
} catch (IllegalArgumentException success) {} |
1265 |
|
} |
1266 |
|
|
1267 |
|
/** |
1273 |
|
try { |
1274 |
|
lock.getWaitQueueLength(c); |
1275 |
|
shouldThrow(); |
1276 |
< |
} catch (IllegalMonitorStateException success) { |
1463 |
< |
} catch (Exception ex) { |
1464 |
< |
unexpectedException(); |
1465 |
< |
} |
1276 |
> |
} catch (IllegalMonitorStateException success) {} |
1277 |
|
} |
1278 |
|
|
1279 |
|
|
1287 |
|
try { |
1288 |
|
lock2.getWaitingThreads(c); |
1289 |
|
shouldThrow(); |
1290 |
< |
} catch (IllegalArgumentException success) { |
1480 |
< |
} catch (Exception ex) { |
1481 |
< |
unexpectedException(); |
1482 |
< |
} |
1290 |
> |
} catch (IllegalArgumentException success) {} |
1291 |
|
} |
1292 |
|
|
1293 |
|
/** |
1299 |
|
try { |
1300 |
|
lock.getWaitingThreads(c); |
1301 |
|
shouldThrow(); |
1302 |
< |
} catch (IllegalMonitorStateException success) { |
1495 |
< |
} catch (Exception ex) { |
1496 |
< |
unexpectedException(); |
1497 |
< |
} |
1302 |
> |
} catch (IllegalMonitorStateException success) {} |
1303 |
|
} |
1304 |
|
|
1305 |
|
|
1306 |
|
/** |
1307 |
|
* hasWaiters returns true when a thread is waiting, else false |
1308 |
|
*/ |
1309 |
< |
public void testHasWaiters() { |
1309 |
> |
public void testHasWaiters() throws InterruptedException { |
1310 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1311 |
|
final Condition c = lock.writeLock().newCondition(); |
1312 |
|
Thread t = new Thread(new Runnable() { |
1324 |
|
} |
1325 |
|
}); |
1326 |
|
|
1327 |
< |
try { |
1328 |
< |
t.start(); |
1329 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1330 |
< |
lock.writeLock().lock(); |
1331 |
< |
assertTrue(lock.hasWaiters(c)); |
1332 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1333 |
< |
c.signal(); |
1334 |
< |
lock.writeLock().unlock(); |
1335 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1336 |
< |
lock.writeLock().lock(); |
1337 |
< |
assertFalse(lock.hasWaiters(c)); |
1338 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1339 |
< |
lock.writeLock().unlock(); |
1340 |
< |
t.join(SHORT_DELAY_MS); |
1536 |
< |
assertFalse(t.isAlive()); |
1537 |
< |
} |
1538 |
< |
catch (Exception ex) { |
1539 |
< |
unexpectedException(); |
1540 |
< |
} |
1327 |
> |
t.start(); |
1328 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1329 |
> |
lock.writeLock().lock(); |
1330 |
> |
assertTrue(lock.hasWaiters(c)); |
1331 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1332 |
> |
c.signal(); |
1333 |
> |
lock.writeLock().unlock(); |
1334 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1335 |
> |
lock.writeLock().lock(); |
1336 |
> |
assertFalse(lock.hasWaiters(c)); |
1337 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1338 |
> |
lock.writeLock().unlock(); |
1339 |
> |
t.join(SHORT_DELAY_MS); |
1340 |
> |
assertFalse(t.isAlive()); |
1341 |
|
} |
1342 |
|
|
1343 |
|
/** |
1344 |
|
* getWaitQueueLength returns number of waiting threads |
1345 |
|
*/ |
1346 |
< |
public void testGetWaitQueueLength() { |
1346 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
1347 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1348 |
|
final Condition c = lock.writeLock().newCondition(); |
1349 |
|
Thread t = new Thread(new Runnable() { |
1361 |
|
} |
1362 |
|
}); |
1363 |
|
|
1364 |
< |
try { |
1365 |
< |
t.start(); |
1366 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1367 |
< |
lock.writeLock().lock(); |
1368 |
< |
assertTrue(lock.hasWaiters(c)); |
1369 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1370 |
< |
c.signal(); |
1371 |
< |
lock.writeLock().unlock(); |
1372 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1373 |
< |
lock.writeLock().lock(); |
1374 |
< |
assertFalse(lock.hasWaiters(c)); |
1375 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1376 |
< |
lock.writeLock().unlock(); |
1377 |
< |
t.join(SHORT_DELAY_MS); |
1578 |
< |
assertFalse(t.isAlive()); |
1579 |
< |
} |
1580 |
< |
catch (Exception ex) { |
1581 |
< |
unexpectedException(); |
1582 |
< |
} |
1364 |
> |
t.start(); |
1365 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1366 |
> |
lock.writeLock().lock(); |
1367 |
> |
assertTrue(lock.hasWaiters(c)); |
1368 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1369 |
> |
c.signal(); |
1370 |
> |
lock.writeLock().unlock(); |
1371 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1372 |
> |
lock.writeLock().lock(); |
1373 |
> |
assertFalse(lock.hasWaiters(c)); |
1374 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1375 |
> |
lock.writeLock().unlock(); |
1376 |
> |
t.join(SHORT_DELAY_MS); |
1377 |
> |
assertFalse(t.isAlive()); |
1378 |
|
} |
1379 |
|
|
1380 |
|
|
1381 |
|
/** |
1382 |
|
* getWaitingThreads returns only and all waiting threads |
1383 |
|
*/ |
1384 |
< |
public void testGetWaitingThreads() { |
1384 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
1385 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1386 |
|
final Condition c = lock.writeLock().newCondition(); |
1387 |
|
Thread t1 = new Thread(new Runnable() { |
1412 |
|
} |
1413 |
|
}); |
1414 |
|
|
1415 |
< |
try { |
1416 |
< |
lock.writeLock().lock(); |
1417 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1418 |
< |
lock.writeLock().unlock(); |
1419 |
< |
t1.start(); |
1420 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1421 |
< |
t2.start(); |
1422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1423 |
< |
lock.writeLock().lock(); |
1424 |
< |
assertTrue(lock.hasWaiters(c)); |
1425 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1426 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1427 |
< |
c.signalAll(); |
1428 |
< |
lock.writeLock().unlock(); |
1429 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1430 |
< |
lock.writeLock().lock(); |
1431 |
< |
assertFalse(lock.hasWaiters(c)); |
1432 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1433 |
< |
lock.writeLock().unlock(); |
1434 |
< |
t1.join(SHORT_DELAY_MS); |
1435 |
< |
t2.join(SHORT_DELAY_MS); |
1436 |
< |
assertFalse(t1.isAlive()); |
1642 |
< |
assertFalse(t2.isAlive()); |
1643 |
< |
} |
1644 |
< |
catch (Exception ex) { |
1645 |
< |
unexpectedException(); |
1646 |
< |
} |
1415 |
> |
lock.writeLock().lock(); |
1416 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1417 |
> |
lock.writeLock().unlock(); |
1418 |
> |
t1.start(); |
1419 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1420 |
> |
t2.start(); |
1421 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1422 |
> |
lock.writeLock().lock(); |
1423 |
> |
assertTrue(lock.hasWaiters(c)); |
1424 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1425 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1426 |
> |
c.signalAll(); |
1427 |
> |
lock.writeLock().unlock(); |
1428 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1429 |
> |
lock.writeLock().lock(); |
1430 |
> |
assertFalse(lock.hasWaiters(c)); |
1431 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1432 |
> |
lock.writeLock().unlock(); |
1433 |
> |
t1.join(SHORT_DELAY_MS); |
1434 |
> |
t2.join(SHORT_DELAY_MS); |
1435 |
> |
assertFalse(t1.isAlive()); |
1436 |
> |
assertFalse(t2.isAlive()); |
1437 |
|
} |
1438 |
|
|
1439 |
|
/** |