1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
15 |
|
|
16 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
+ |
* Releases lock, checking that it had a hold count of 1. |
63 |
+ |
*/ |
64 |
+ |
void releaseLock(ReentrantReadWriteLock.WriteLock lock) { |
65 |
+ |
assertTrue(lock.isHeldByCurrentThread()); |
66 |
+ |
lock.unlock(); |
67 |
+ |
assertFalse(lock.isHeldByCurrentThread()); |
68 |
+ |
} |
69 |
+ |
|
70 |
+ |
/** |
71 |
|
* Constructor sets given fairness, and is in unlocked state |
72 |
|
*/ |
73 |
|
public void testConstructor() { |
199 |
|
*/ |
200 |
|
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
201 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
202 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
202 |
> |
lock.writeLock().lock(); |
203 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
204 |
|
public void realRun() throws InterruptedException { |
205 |
|
lock.writeLock().lockInterruptibly(); |
196 |
– |
lock.writeLock().unlock(); |
197 |
– |
lock.writeLock().lockInterruptibly(); |
198 |
– |
lock.writeLock().unlock(); |
206 |
|
}}); |
207 |
|
|
201 |
– |
lock.writeLock().lock(); |
202 |
– |
t.start(); |
208 |
|
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
t.interrupt(); |
205 |
– |
Thread.sleep(SHORT_DELAY_MS); |
206 |
– |
lock.writeLock().unlock(); |
210 |
|
t.join(); |
211 |
+ |
releaseLock(lock.writeLock()); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
217 |
|
public void testWriteTryLock_Interrupted() throws InterruptedException { |
218 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
219 |
|
lock.writeLock().lock(); |
220 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
220 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
221 |
|
public void realRun() throws InterruptedException { |
222 |
< |
lock.writeLock().tryLock(1000,MILLISECONDS); |
222 |
> |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
223 |
|
}}); |
224 |
|
|
225 |
< |
t.start(); |
225 |
> |
Thread.sleep(SHORT_DELAY_MS); |
226 |
|
t.interrupt(); |
223 |
– |
lock.writeLock().unlock(); |
227 |
|
t.join(); |
228 |
+ |
releaseLock(lock.writeLock()); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
234 |
|
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
235 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
236 |
|
lock.writeLock().lock(); |
237 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
237 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
238 |
|
public void realRun() throws InterruptedException { |
239 |
|
lock.readLock().lockInterruptibly(); |
240 |
|
}}); |
241 |
|
|
238 |
– |
t.start(); |
242 |
|
Thread.sleep(SHORT_DELAY_MS); |
243 |
|
t.interrupt(); |
241 |
– |
Thread.sleep(SHORT_DELAY_MS); |
242 |
– |
lock.writeLock().unlock(); |
244 |
|
t.join(); |
245 |
+ |
releaseLock(lock.writeLock()); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
251 |
|
public void testReadTryLock_Interrupted() throws InterruptedException { |
252 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
253 |
|
lock.writeLock().lock(); |
254 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
255 |
|
public void realRun() throws InterruptedException { |
256 |
< |
lock.readLock().tryLock(1000,MILLISECONDS); |
256 |
> |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
257 |
|
}}); |
258 |
|
|
259 |
< |
t.start(); |
259 |
> |
Thread.sleep(SHORT_DELAY_MS); |
260 |
|
t.interrupt(); |
261 |
|
t.join(); |
262 |
+ |
releaseLock(lock.writeLock()); |
263 |
|
} |
264 |
|
|
265 |
|
|
269 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
270 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
271 |
|
lock.writeLock().lock(); |
272 |
< |
Thread t = new Thread(new CheckedRunnable() { |
272 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
273 |
|
public void realRun() { |
274 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
274 |
> |
assertFalse(lock.writeLock().tryLock()); |
275 |
|
}}); |
276 |
|
|
274 |
– |
t.start(); |
277 |
|
t.join(); |
278 |
|
lock.writeLock().unlock(); |
279 |
|
} |
284 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
285 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
|
lock.writeLock().lock(); |
287 |
< |
Thread t = new Thread(new CheckedRunnable() { |
287 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
288 |
|
public void realRun() { |
289 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
289 |
> |
assertFalse(lock.readLock().tryLock()); |
290 |
|
}}); |
291 |
|
|
290 |
– |
t.start(); |
292 |
|
t.join(); |
293 |
|
lock.writeLock().unlock(); |
294 |
|
} |
299 |
|
public void testMultipleReadLocks() throws InterruptedException { |
300 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
|
lock.readLock().lock(); |
302 |
< |
Thread t = new Thread(new CheckedRunnable() { |
302 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
303 |
|
public void realRun() { |
304 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
304 |
> |
assertTrue(lock.readLock().tryLock()); |
305 |
|
lock.readLock().unlock(); |
306 |
|
}}); |
307 |
|
|
307 |
– |
t.start(); |
308 |
|
t.join(); |
309 |
|
lock.readLock().unlock(); |
310 |
|
} |
315 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
316 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
317 |
|
lock.readLock().lock(); |
318 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
318 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
319 |
|
public void realRun() { |
320 |
|
lock.readLock().lock(); |
321 |
|
lock.readLock().unlock(); |
322 |
|
}}); |
323 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
323 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
324 |
|
public void realRun() { |
325 |
|
lock.writeLock().lock(); |
326 |
|
lock.writeLock().unlock(); |
327 |
|
}}); |
328 |
|
|
329 |
– |
t1.start(); |
330 |
– |
t2.start(); |
329 |
|
Thread.sleep(SHORT_DELAY_MS); |
330 |
|
lock.readLock().unlock(); |
331 |
|
t1.join(MEDIUM_DELAY_MS); |
340 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
341 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
342 |
|
lock.writeLock().lock(); |
343 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
343 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
344 |
|
public void realRun() { |
345 |
|
lock.readLock().lock(); |
346 |
|
lock.readLock().unlock(); |
347 |
|
}}); |
348 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
348 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
349 |
|
public void realRun() { |
350 |
|
lock.readLock().lock(); |
351 |
|
lock.readLock().unlock(); |
352 |
|
}}); |
353 |
|
|
356 |
– |
t1.start(); |
357 |
– |
t2.start(); |
354 |
|
Thread.sleep(SHORT_DELAY_MS); |
355 |
|
lock.writeLock().unlock(); |
356 |
|
t1.join(MEDIUM_DELAY_MS); |
377 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
378 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
379 |
|
lock.writeLock().lock(); |
380 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
380 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
381 |
|
public void realRun() { |
382 |
|
lock.readLock().lock(); |
383 |
|
lock.readLock().unlock(); |
384 |
|
}}); |
385 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
385 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
386 |
|
public void realRun() { |
387 |
|
lock.readLock().lock(); |
388 |
|
lock.readLock().unlock(); |
389 |
|
}}); |
390 |
|
|
395 |
– |
t1.start(); |
396 |
– |
t2.start(); |
391 |
|
lock.readLock().lock(); |
392 |
|
lock.readLock().unlock(); |
393 |
|
Thread.sleep(SHORT_DELAY_MS); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
< |
* Read lock succeeds if write locked by current thread even if |
404 |
> |
* Read lock succeeds if write locked by current thread even if |
405 |
|
* other threads are waiting for writelock |
406 |
|
*/ |
407 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
408 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
409 |
|
lock.writeLock().lock(); |
410 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
410 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
411 |
|
public void realRun() { |
412 |
|
lock.writeLock().lock(); |
413 |
|
lock.writeLock().unlock(); |
414 |
|
}}); |
415 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
415 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
416 |
|
public void realRun() { |
417 |
|
lock.writeLock().lock(); |
418 |
|
lock.writeLock().unlock(); |
419 |
|
}}); |
420 |
|
|
427 |
– |
t1.start(); |
428 |
– |
t2.start(); |
421 |
|
lock.readLock().lock(); |
422 |
|
lock.readLock().unlock(); |
423 |
|
Thread.sleep(SHORT_DELAY_MS); |
432 |
|
|
433 |
|
|
434 |
|
/** |
435 |
< |
* Write lock succeeds if write locked by current thread even if |
435 |
> |
* Write lock succeeds if write locked by current thread even if |
436 |
|
* other threads are waiting for writelock |
437 |
|
*/ |
438 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
439 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
440 |
|
lock.writeLock().lock(); |
441 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
441 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() { |
443 |
|
lock.writeLock().lock(); |
444 |
|
lock.writeLock().unlock(); |
445 |
|
}}); |
446 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
446 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
447 |
|
public void realRun() { |
448 |
|
lock.writeLock().lock(); |
449 |
|
lock.writeLock().unlock(); |
450 |
|
}}); |
451 |
|
|
460 |
– |
t1.start(); |
461 |
– |
t2.start(); |
452 |
|
lock.writeLock().lock(); |
453 |
|
lock.writeLock().unlock(); |
454 |
|
Thread.sleep(SHORT_DELAY_MS); |
480 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
481 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
482 |
|
lock.writeLock().lock(); |
483 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
483 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
484 |
|
public void realRun() { |
485 |
|
lock.readLock().lock(); |
486 |
|
lock.readLock().unlock(); |
487 |
|
}}); |
488 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
488 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
489 |
|
public void realRun() { |
490 |
|
lock.readLock().lock(); |
491 |
|
lock.readLock().unlock(); |
492 |
|
}}); |
493 |
|
|
504 |
– |
t1.start(); |
505 |
– |
t2.start(); |
494 |
|
lock.readLock().lock(); |
495 |
|
lock.readLock().unlock(); |
496 |
|
Thread.sleep(SHORT_DELAY_MS); |
511 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
512 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
513 |
|
lock.writeLock().lock(); |
514 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
514 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
515 |
|
public void realRun() { |
516 |
|
lock.writeLock().lock(); |
517 |
|
lock.writeLock().unlock(); |
518 |
|
}}); |
519 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
519 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
520 |
|
public void realRun() { |
521 |
|
lock.writeLock().lock(); |
522 |
|
lock.writeLock().unlock(); |
523 |
|
}}); |
524 |
|
|
537 |
– |
t1.start(); |
538 |
– |
t2.start(); |
525 |
|
lock.readLock().lock(); |
526 |
|
lock.readLock().unlock(); |
527 |
|
Thread.sleep(SHORT_DELAY_MS); |
542 |
|
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
543 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
544 |
|
lock.writeLock().lock(); |
545 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
545 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
546 |
|
public void realRun() { |
547 |
|
lock.writeLock().lock(); |
548 |
|
lock.writeLock().unlock(); |
549 |
|
}}); |
550 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
550 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
551 |
|
public void realRun() { |
552 |
|
lock.writeLock().lock(); |
553 |
|
lock.writeLock().unlock(); |
554 |
|
}}); |
555 |
|
|
570 |
– |
t1.start(); |
571 |
– |
t2.start(); |
556 |
|
Thread.sleep(SHORT_DELAY_MS); |
557 |
|
assertTrue(lock.isWriteLockedByCurrentThread()); |
558 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
558 |
> |
assertEquals(1, lock.getWriteHoldCount()); |
559 |
|
lock.writeLock().lock(); |
560 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
560 |
> |
assertEquals(2, lock.getWriteHoldCount()); |
561 |
|
lock.writeLock().unlock(); |
562 |
|
lock.writeLock().lock(); |
563 |
|
lock.writeLock().unlock(); |
575 |
|
public void testTryLockWhenReadLocked() throws InterruptedException { |
576 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
577 |
|
lock.readLock().lock(); |
578 |
< |
Thread t = new Thread(new CheckedRunnable() { |
578 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
579 |
|
public void realRun() { |
580 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
580 |
> |
assertTrue(lock.readLock().tryLock()); |
581 |
|
lock.readLock().unlock(); |
582 |
|
}}); |
583 |
|
|
600 |
– |
t.start(); |
584 |
|
t.join(); |
585 |
|
lock.readLock().unlock(); |
586 |
|
} |
587 |
|
|
605 |
– |
|
606 |
– |
|
588 |
|
/** |
589 |
|
* write tryLock fails when readlocked |
590 |
|
*/ |
591 |
|
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
592 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
593 |
|
lock.readLock().lock(); |
594 |
< |
Thread t = new Thread(new CheckedRunnable() { |
594 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
595 |
|
public void realRun() { |
596 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
596 |
> |
assertFalse(lock.writeLock().tryLock()); |
597 |
|
}}); |
598 |
|
|
618 |
– |
t.start(); |
599 |
|
t.join(); |
600 |
|
lock.readLock().unlock(); |
601 |
|
} |
607 |
|
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
608 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
609 |
|
lock.readLock().lock(); |
610 |
< |
Thread t = new Thread(new CheckedRunnable() { |
610 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
611 |
|
public void realRun() { |
612 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
612 |
> |
assertTrue(lock.readLock().tryLock()); |
613 |
|
lock.readLock().unlock(); |
614 |
|
}}); |
615 |
|
|
636 |
– |
t.start(); |
616 |
|
t.join(); |
617 |
|
lock.readLock().unlock(); |
618 |
|
} |
625 |
|
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
626 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
627 |
|
lock.readLock().lock(); |
628 |
< |
Thread t = new Thread(new CheckedRunnable() { |
628 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
629 |
|
public void realRun() { |
630 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
630 |
> |
assertFalse(lock.writeLock().tryLock()); |
631 |
|
}}); |
632 |
|
|
654 |
– |
t.start(); |
633 |
|
t.join(); |
634 |
|
lock.readLock().unlock(); |
635 |
|
} |
642 |
|
public void testWriteTryLock_Timeout() throws InterruptedException { |
643 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
644 |
|
lock.writeLock().lock(); |
645 |
< |
Thread t = new Thread(new CheckedRunnable() { |
645 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
646 |
|
public void realRun() throws InterruptedException { |
647 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
647 |
> |
assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
648 |
|
}}); |
649 |
|
|
672 |
– |
t.start(); |
650 |
|
t.join(); |
651 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
652 |
|
lock.writeLock().unlock(); |
658 |
|
public void testReadTryLock_Timeout() throws InterruptedException { |
659 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
660 |
|
lock.writeLock().lock(); |
661 |
< |
Thread t = new Thread(new CheckedRunnable() { |
661 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
662 |
|
public void realRun() throws InterruptedException { |
663 |
< |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
663 |
> |
assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
664 |
|
}}); |
665 |
|
|
689 |
– |
t.start(); |
666 |
|
t.join(); |
667 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
668 |
|
lock.writeLock().unlock(); |
675 |
|
public void testWriteLockInterruptibly() throws InterruptedException { |
676 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
677 |
|
lock.writeLock().lockInterruptibly(); |
678 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
678 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
679 |
|
public void realRun() throws InterruptedException { |
680 |
|
lock.writeLock().lockInterruptibly(); |
681 |
|
}}); |
682 |
|
|
707 |
– |
t.start(); |
683 |
|
Thread.sleep(SHORT_DELAY_MS); |
684 |
|
t.interrupt(); |
710 |
– |
Thread.sleep(SHORT_DELAY_MS); |
685 |
|
t.join(); |
686 |
< |
lock.writeLock().unlock(); |
686 |
> |
releaseLock(lock.writeLock()); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
690 |
< |
* read lockInterruptibly succeeds if lock free else is interruptible |
690 |
> |
* read lockInterruptibly succeeds if lock free else is interruptible |
691 |
|
*/ |
692 |
|
public void testReadLockInterruptibly() throws InterruptedException { |
693 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
694 |
|
lock.writeLock().lockInterruptibly(); |
695 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
695 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
696 |
|
public void realRun() throws InterruptedException { |
697 |
|
lock.readLock().lockInterruptibly(); |
698 |
|
}}); |
699 |
|
|
726 |
– |
t.start(); |
700 |
|
Thread.sleep(SHORT_DELAY_MS); |
701 |
|
t.interrupt(); |
702 |
|
t.join(); |
703 |
< |
lock.writeLock().unlock(); |
703 |
> |
releaseLock(lock.writeLock()); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
742 |
|
|
743 |
|
|
744 |
|
/** |
745 |
< |
* timed await without a signal times out |
745 |
> |
* timed await without a signal times out |
746 |
|
*/ |
747 |
|
public void testAwait_Timeout() throws InterruptedException { |
748 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
770 |
|
public void testAwait() throws InterruptedException { |
771 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
772 |
|
final Condition c = lock.writeLock().newCondition(); |
773 |
< |
Thread t = new Thread(new CheckedRunnable() { |
773 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
774 |
|
public void realRun() throws InterruptedException { |
775 |
|
lock.writeLock().lock(); |
776 |
|
c.await(); |
777 |
|
lock.writeLock().unlock(); |
778 |
|
}}); |
779 |
|
|
807 |
– |
t.start(); |
780 |
|
Thread.sleep(SHORT_DELAY_MS); |
781 |
|
lock.writeLock().lock(); |
782 |
|
c.signal(); |
846 |
|
public void testAwait_Interrupt() throws InterruptedException { |
847 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
848 |
|
final Condition c = lock.writeLock().newCondition(); |
849 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
849 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
850 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
851 |
|
public void realRun() throws InterruptedException { |
852 |
|
lock.writeLock().lock(); |
853 |
< |
c.await(); |
854 |
< |
lock.writeLock().unlock(); |
853 |
> |
assertTrue(lock.isWriteLocked()); |
854 |
> |
locked.countDown(); |
855 |
> |
try { c.await(); } |
856 |
> |
finally { lock.writeLock().unlock(); } |
857 |
|
}}); |
858 |
|
|
859 |
< |
t.start(); |
860 |
< |
Thread.sleep(SHORT_DELAY_MS); |
859 |
> |
locked.await(); |
860 |
> |
while (lock.isWriteLocked()) |
861 |
> |
Thread.yield(); |
862 |
|
t.interrupt(); |
863 |
< |
t.join(SHORT_DELAY_MS); |
864 |
< |
assertFalse(t.isAlive()); |
863 |
> |
awaitTermination(t, LONG_DELAY_MS); |
864 |
> |
assertFalse(lock.isWriteLocked()); |
865 |
|
} |
866 |
|
|
867 |
|
/** |
870 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
871 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
872 |
|
final Condition c = lock.writeLock().newCondition(); |
873 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
873 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
874 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
875 |
|
public void realRun() throws InterruptedException { |
876 |
|
lock.writeLock().lock(); |
877 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
878 |
< |
lock.writeLock().unlock(); |
877 |
> |
assertTrue(lock.isWriteLocked()); |
878 |
> |
locked.countDown(); |
879 |
> |
try { c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); } |
880 |
> |
finally { lock.writeLock().unlock(); } |
881 |
|
}}); |
882 |
|
|
883 |
< |
t.start(); |
884 |
< |
Thread.sleep(SHORT_DELAY_MS); |
883 |
> |
locked.await(); |
884 |
> |
while (lock.isWriteLocked()) |
885 |
> |
Thread.yield(); |
886 |
|
t.interrupt(); |
887 |
< |
t.join(SHORT_DELAY_MS); |
888 |
< |
assertFalse(t.isAlive()); |
887 |
> |
awaitTermination(t, LONG_DELAY_MS); |
888 |
> |
assertFalse(lock.isWriteLocked()); |
889 |
|
} |
890 |
|
|
891 |
|
/** |
894 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
895 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
896 |
|
final Condition c = lock.writeLock().newCondition(); |
897 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
897 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
898 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
899 |
|
public void realRun() throws InterruptedException { |
900 |
|
lock.writeLock().lock(); |
901 |
+ |
assertTrue(lock.isWriteLocked()); |
902 |
+ |
locked.countDown(); |
903 |
|
java.util.Date d = new java.util.Date(); |
904 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
905 |
< |
lock.writeLock().unlock(); |
904 |
> |
try { c.awaitUntil(new java.util.Date(d.getTime() + 10000)); } |
905 |
> |
finally { lock.writeLock().unlock(); } |
906 |
|
}}); |
907 |
|
|
908 |
< |
t.start(); |
909 |
< |
Thread.sleep(SHORT_DELAY_MS); |
908 |
> |
locked.await(); |
909 |
> |
while (lock.isWriteLocked()) |
910 |
> |
Thread.yield(); |
911 |
|
t.interrupt(); |
912 |
< |
t.join(SHORT_DELAY_MS); |
913 |
< |
assertFalse(t.isAlive()); |
912 |
> |
awaitTermination(t, LONG_DELAY_MS); |
913 |
> |
assertFalse(lock.isWriteLocked()); |
914 |
|
} |
915 |
|
|
916 |
|
/** |
919 |
|
public void testSignalAll() throws InterruptedException { |
920 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
921 |
|
final Condition c = lock.writeLock().newCondition(); |
922 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
922 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
923 |
|
public void realRun() throws InterruptedException { |
924 |
|
lock.writeLock().lock(); |
925 |
|
c.await(); |
926 |
|
lock.writeLock().unlock(); |
927 |
|
}}); |
928 |
|
|
929 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
929 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
930 |
|
public void realRun() throws InterruptedException { |
931 |
|
lock.writeLock().lock(); |
932 |
|
c.await(); |
933 |
|
lock.writeLock().unlock(); |
934 |
|
}}); |
935 |
|
|
952 |
– |
t1.start(); |
953 |
– |
t2.start(); |
936 |
|
Thread.sleep(SHORT_DELAY_MS); |
937 |
|
lock.writeLock().lock(); |
938 |
|
c.signalAll(); |
1201 |
|
public void testHasWaiters() throws InterruptedException { |
1202 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1203 |
|
final Condition c = lock.writeLock().newCondition(); |
1204 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1204 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1205 |
|
public void realRun() throws InterruptedException { |
1206 |
|
lock.writeLock().lock(); |
1207 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1208 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1207 |
> |
assertFalse(lock.hasWaiters(c)); |
1208 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1209 |
|
c.await(); |
1210 |
|
lock.writeLock().unlock(); |
1211 |
|
}}); |
1212 |
|
|
1231 |
– |
t.start(); |
1213 |
|
Thread.sleep(SHORT_DELAY_MS); |
1214 |
|
lock.writeLock().lock(); |
1215 |
|
assertTrue(lock.hasWaiters(c)); |
1231 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
1232 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1233 |
|
final Condition c = lock.writeLock().newCondition(); |
1234 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1234 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1235 |
|
public void realRun() throws InterruptedException { |
1236 |
|
lock.writeLock().lock(); |
1237 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1238 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1237 |
> |
assertFalse(lock.hasWaiters(c)); |
1238 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1239 |
|
c.await(); |
1240 |
|
lock.writeLock().unlock(); |
1241 |
|
}}); |
1242 |
|
|
1262 |
– |
t.start(); |
1243 |
|
Thread.sleep(SHORT_DELAY_MS); |
1244 |
|
lock.writeLock().lock(); |
1245 |
|
assertTrue(lock.hasWaiters(c)); |
1265 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
1266 |
|
public void realRun() throws InterruptedException { |
1267 |
|
lock.writeLock().lock(); |
1268 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1268 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1269 |
|
c.await(); |
1270 |
|
lock.writeLock().unlock(); |
1271 |
|
}}); |
1273 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
1274 |
|
public void realRun() throws InterruptedException { |
1275 |
|
lock.writeLock().lock(); |
1276 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1276 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
1277 |
|
c.await(); |
1278 |
|
lock.writeLock().unlock(); |
1279 |
|
}}); |