84 |
|
rl.writeLock().lock(); |
85 |
|
assertTrue(rl.isWriteLocked()); |
86 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
87 |
+ |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
88 |
|
assertEquals(0, rl.getReadLockCount()); |
89 |
|
rl.writeLock().unlock(); |
90 |
|
assertFalse(rl.isWriteLocked()); |
91 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
92 |
+ |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
93 |
|
assertEquals(0, rl.getReadLockCount()); |
94 |
|
rl.readLock().lock(); |
95 |
|
assertFalse(rl.isWriteLocked()); |
110 |
|
rl.writeLock().lock(); |
111 |
|
assertTrue(rl.isWriteLocked()); |
112 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
113 |
+ |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
114 |
|
assertEquals(0, rl.getReadLockCount()); |
115 |
|
rl.writeLock().unlock(); |
116 |
|
assertFalse(rl.isWriteLocked()); |
117 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
118 |
+ |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
119 |
|
assertEquals(0, rl.getReadLockCount()); |
120 |
|
rl.readLock().lock(); |
121 |
|
assertFalse(rl.isWriteLocked()); |
130 |
|
/** |
131 |
|
* getWriteHoldCount returns number of recursive holds |
132 |
|
*/ |
133 |
< |
public void testGetHoldCount() { |
133 |
> |
public void testGetWriteHoldCount() { |
134 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
135 |
|
for(int i = 1; i <= SIZE; i++) { |
136 |
|
lock.writeLock().lock(); |
141 |
|
assertEquals(i-1,lock.getWriteHoldCount()); |
142 |
|
} |
143 |
|
} |
144 |
+ |
|
145 |
+ |
/** |
146 |
+ |
* WriteLock.getHoldCount returns number of recursive holds |
147 |
+ |
*/ |
148 |
+ |
public void testGetHoldCount() { |
149 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
150 |
+ |
for(int i = 1; i <= SIZE; i++) { |
151 |
+ |
lock.writeLock().lock(); |
152 |
+ |
assertEquals(i,lock.writeLock().getHoldCount()); |
153 |
+ |
} |
154 |
+ |
for(int i = SIZE; i > 0; i--) { |
155 |
+ |
lock.writeLock().unlock(); |
156 |
+ |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
157 |
+ |
} |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
/** |
161 |
+ |
* getReadHoldCount returns number of recursive holds |
162 |
+ |
*/ |
163 |
+ |
public void testGetReadHoldCount() { |
164 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
165 |
+ |
for(int i = 1; i <= SIZE; i++) { |
166 |
+ |
lock.readLock().lock(); |
167 |
+ |
assertEquals(i,lock.getReadHoldCount()); |
168 |
+ |
} |
169 |
+ |
for(int i = SIZE; i > 0; i--) { |
170 |
+ |
lock.readLock().unlock(); |
171 |
+ |
assertEquals(i-1,lock.getReadHoldCount()); |
172 |
+ |
} |
173 |
+ |
} |
174 |
|
|
175 |
|
|
176 |
|
/** |
423 |
|
|
424 |
|
/** |
425 |
|
* Read lock succeeds if write locked by current thread even if |
426 |
< |
* other threads are waiting |
426 |
> |
* other threads are waiting for readlock |
427 |
|
*/ |
428 |
|
public void testReadHoldingWriteLock2() { |
429 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
+ |
* Read lock succeeds if write locked by current thread even if |
465 |
+ |
* other threads are waiting for writelock |
466 |
+ |
*/ |
467 |
+ |
public void testReadHoldingWriteLock3() { |
468 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
469 |
+ |
lock.writeLock().lock(); |
470 |
+ |
Thread t1 = new Thread(new Runnable() { |
471 |
+ |
public void run() { |
472 |
+ |
lock.writeLock().lock(); |
473 |
+ |
lock.writeLock().unlock(); |
474 |
+ |
} |
475 |
+ |
}); |
476 |
+ |
Thread t2 = new Thread(new Runnable() { |
477 |
+ |
public void run() { |
478 |
+ |
lock.writeLock().lock(); |
479 |
+ |
lock.writeLock().unlock(); |
480 |
+ |
} |
481 |
+ |
}); |
482 |
+ |
|
483 |
+ |
try { |
484 |
+ |
t1.start(); |
485 |
+ |
t2.start(); |
486 |
+ |
lock.readLock().lock(); |
487 |
+ |
lock.readLock().unlock(); |
488 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
489 |
+ |
lock.readLock().lock(); |
490 |
+ |
lock.readLock().unlock(); |
491 |
+ |
lock.writeLock().unlock(); |
492 |
+ |
t1.join(MEDIUM_DELAY_MS); |
493 |
+ |
t2.join(MEDIUM_DELAY_MS); |
494 |
+ |
assertTrue(!t1.isAlive()); |
495 |
+ |
assertTrue(!t2.isAlive()); |
496 |
+ |
|
497 |
+ |
} catch(Exception e){ |
498 |
+ |
unexpectedException(); |
499 |
+ |
} |
500 |
+ |
} |
501 |
+ |
|
502 |
+ |
|
503 |
+ |
/** |
504 |
+ |
* Write lock succeeds if write locked by current thread even if |
505 |
+ |
* other threads are waiting for writelock |
506 |
+ |
*/ |
507 |
+ |
public void testWriteHoldingWriteLock4() { |
508 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
509 |
+ |
lock.writeLock().lock(); |
510 |
+ |
Thread t1 = new Thread(new Runnable() { |
511 |
+ |
public void run() { |
512 |
+ |
lock.writeLock().lock(); |
513 |
+ |
lock.writeLock().unlock(); |
514 |
+ |
} |
515 |
+ |
}); |
516 |
+ |
Thread t2 = new Thread(new Runnable() { |
517 |
+ |
public void run() { |
518 |
+ |
lock.writeLock().lock(); |
519 |
+ |
lock.writeLock().unlock(); |
520 |
+ |
} |
521 |
+ |
}); |
522 |
+ |
|
523 |
+ |
try { |
524 |
+ |
t1.start(); |
525 |
+ |
t2.start(); |
526 |
+ |
lock.writeLock().lock(); |
527 |
+ |
lock.writeLock().unlock(); |
528 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
529 |
+ |
lock.writeLock().lock(); |
530 |
+ |
lock.writeLock().unlock(); |
531 |
+ |
lock.writeLock().unlock(); |
532 |
+ |
t1.join(MEDIUM_DELAY_MS); |
533 |
+ |
t2.join(MEDIUM_DELAY_MS); |
534 |
+ |
assertTrue(!t1.isAlive()); |
535 |
+ |
assertTrue(!t2.isAlive()); |
536 |
+ |
|
537 |
+ |
} catch(Exception e){ |
538 |
+ |
unexpectedException(); |
539 |
+ |
} |
540 |
+ |
} |
541 |
+ |
|
542 |
+ |
|
543 |
+ |
/** |
544 |
|
* Fair Read trylock succeeds if write locked by current thread |
545 |
|
*/ |
546 |
|
public void testReadHoldingWriteLockFair() { |
553 |
|
|
554 |
|
/** |
555 |
|
* Fair Read lock succeeds if write locked by current thread even if |
556 |
< |
* other threads are waiting |
556 |
> |
* other threads are waiting for readlock |
557 |
|
*/ |
558 |
|
public void testReadHoldingWriteLockFair2() { |
559 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
592 |
|
|
593 |
|
|
594 |
|
/** |
595 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
596 |
+ |
* other threads are waiting for writelock |
597 |
+ |
*/ |
598 |
+ |
public void testReadHoldingWriteLockFair3() { |
599 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
600 |
+ |
lock.writeLock().lock(); |
601 |
+ |
Thread t1 = new Thread(new Runnable() { |
602 |
+ |
public void run() { |
603 |
+ |
lock.writeLock().lock(); |
604 |
+ |
lock.writeLock().unlock(); |
605 |
+ |
} |
606 |
+ |
}); |
607 |
+ |
Thread t2 = new Thread(new Runnable() { |
608 |
+ |
public void run() { |
609 |
+ |
lock.writeLock().lock(); |
610 |
+ |
lock.writeLock().unlock(); |
611 |
+ |
} |
612 |
+ |
}); |
613 |
+ |
|
614 |
+ |
try { |
615 |
+ |
t1.start(); |
616 |
+ |
t2.start(); |
617 |
+ |
lock.readLock().lock(); |
618 |
+ |
lock.readLock().unlock(); |
619 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
620 |
+ |
lock.readLock().lock(); |
621 |
+ |
lock.readLock().unlock(); |
622 |
+ |
lock.writeLock().unlock(); |
623 |
+ |
t1.join(MEDIUM_DELAY_MS); |
624 |
+ |
t2.join(MEDIUM_DELAY_MS); |
625 |
+ |
assertTrue(!t1.isAlive()); |
626 |
+ |
assertTrue(!t2.isAlive()); |
627 |
+ |
|
628 |
+ |
} catch(Exception e){ |
629 |
+ |
unexpectedException(); |
630 |
+ |
} |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
|
634 |
+ |
/** |
635 |
+ |
* Fair Write lock succeeds if write locked by current thread even if |
636 |
+ |
* other threads are waiting for writelock |
637 |
+ |
*/ |
638 |
+ |
public void testWriteHoldingWriteLockFair4() { |
639 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
640 |
+ |
lock.writeLock().lock(); |
641 |
+ |
Thread t1 = new Thread(new Runnable() { |
642 |
+ |
public void run() { |
643 |
+ |
lock.writeLock().lock(); |
644 |
+ |
lock.writeLock().unlock(); |
645 |
+ |
} |
646 |
+ |
}); |
647 |
+ |
Thread t2 = new Thread(new Runnable() { |
648 |
+ |
public void run() { |
649 |
+ |
lock.writeLock().lock(); |
650 |
+ |
lock.writeLock().unlock(); |
651 |
+ |
} |
652 |
+ |
}); |
653 |
+ |
|
654 |
+ |
try { |
655 |
+ |
t1.start(); |
656 |
+ |
t2.start(); |
657 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
658 |
+ |
assertTrue(lock.isWriteLockedByCurrentThread()); |
659 |
+ |
assertTrue(lock.getWriteHoldCount() == 1); |
660 |
+ |
lock.writeLock().lock(); |
661 |
+ |
assertTrue(lock.getWriteHoldCount() == 2); |
662 |
+ |
lock.writeLock().unlock(); |
663 |
+ |
lock.writeLock().lock(); |
664 |
+ |
lock.writeLock().unlock(); |
665 |
+ |
lock.writeLock().unlock(); |
666 |
+ |
t1.join(MEDIUM_DELAY_MS); |
667 |
+ |
t2.join(MEDIUM_DELAY_MS); |
668 |
+ |
assertTrue(!t1.isAlive()); |
669 |
+ |
assertTrue(!t2.isAlive()); |
670 |
+ |
|
671 |
+ |
} catch(Exception e){ |
672 |
+ |
unexpectedException(); |
673 |
+ |
} |
674 |
+ |
} |
675 |
+ |
|
676 |
+ |
|
677 |
+ |
/** |
678 |
|
* Read tryLock succeeds if readlocked but not writelocked |
679 |
|
*/ |
680 |
|
public void testTryLockWhenReadLocked() { |
705 |
|
lock.readLock().lock(); |
706 |
|
Thread t = new Thread(new Runnable() { |
707 |
|
public void run() { |
708 |
+ |
threadAssertFalse(lock.writeLock().tryLock()); |
709 |
+ |
} |
710 |
+ |
}); |
711 |
+ |
try { |
712 |
+ |
t.start(); |
713 |
+ |
t.join(); |
714 |
+ |
lock.readLock().unlock(); |
715 |
+ |
} catch(Exception e){ |
716 |
+ |
unexpectedException(); |
717 |
+ |
} |
718 |
+ |
} |
719 |
+ |
|
720 |
+ |
|
721 |
+ |
/** |
722 |
+ |
* Fair Read tryLock succeeds if readlocked but not writelocked |
723 |
+ |
*/ |
724 |
+ |
public void testTryLockWhenReadLockedFair() { |
725 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
726 |
+ |
lock.readLock().lock(); |
727 |
+ |
Thread t = new Thread(new Runnable() { |
728 |
+ |
public void run() { |
729 |
+ |
threadAssertTrue(lock.readLock().tryLock()); |
730 |
+ |
lock.readLock().unlock(); |
731 |
+ |
} |
732 |
+ |
}); |
733 |
+ |
try { |
734 |
+ |
t.start(); |
735 |
+ |
t.join(); |
736 |
+ |
lock.readLock().unlock(); |
737 |
+ |
} catch(Exception e){ |
738 |
+ |
unexpectedException(); |
739 |
+ |
} |
740 |
+ |
} |
741 |
+ |
|
742 |
+ |
|
743 |
+ |
|
744 |
+ |
/** |
745 |
+ |
* Fair write tryLock fails when readlocked |
746 |
+ |
*/ |
747 |
+ |
public void testWriteTryLockWhenReadLockedFair() { |
748 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
749 |
+ |
lock.readLock().lock(); |
750 |
+ |
Thread t = new Thread(new Runnable() { |
751 |
+ |
public void run() { |
752 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
753 |
|
} |
754 |
|
}); |