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 |
|
/** |
203 |
|
try { |
204 |
|
lock.writeLock().lock(); |
205 |
|
t.start(); |
206 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
207 |
|
t.interrupt(); |
208 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
lock.writeLock().unlock(); |
210 |
|
t.join(); |
211 |
|
} catch(Exception e){ |
223 |
|
public void run() { |
224 |
|
try { |
225 |
|
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
190 |
– |
threadShouldThrow(); |
226 |
|
} catch(InterruptedException success){} |
227 |
|
} |
228 |
|
}); |
251 |
|
}); |
252 |
|
try { |
253 |
|
t.start(); |
254 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
255 |
|
t.interrupt(); |
256 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
257 |
|
lock.writeLock().unlock(); |
258 |
|
t.join(); |
259 |
|
} catch(Exception e){ |
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
+ |
/** |
418 |
+ |
* Read trylock succeeds if write locked by current thread |
419 |
+ |
*/ |
420 |
+ |
public void testReadHoldingWriteLock() { |
421 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
422 |
+ |
lock.writeLock().lock(); |
423 |
+ |
assertTrue(lock.readLock().tryLock()); |
424 |
+ |
lock.readLock().unlock(); |
425 |
+ |
lock.writeLock().unlock(); |
426 |
+ |
} |
427 |
+ |
|
428 |
+ |
/** |
429 |
+ |
* Read lock succeeds if write locked by current thread even if |
430 |
+ |
* other threads are waiting for readlock |
431 |
+ |
*/ |
432 |
+ |
public void testReadHoldingWriteLock2() { |
433 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
434 |
+ |
lock.writeLock().lock(); |
435 |
+ |
Thread t1 = new Thread(new Runnable() { |
436 |
+ |
public void run() { |
437 |
+ |
lock.readLock().lock(); |
438 |
+ |
lock.readLock().unlock(); |
439 |
+ |
} |
440 |
+ |
}); |
441 |
+ |
Thread t2 = new Thread(new Runnable() { |
442 |
+ |
public void run() { |
443 |
+ |
lock.readLock().lock(); |
444 |
+ |
lock.readLock().unlock(); |
445 |
+ |
} |
446 |
+ |
}); |
447 |
+ |
|
448 |
+ |
try { |
449 |
+ |
t1.start(); |
450 |
+ |
t2.start(); |
451 |
+ |
lock.readLock().lock(); |
452 |
+ |
lock.readLock().unlock(); |
453 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
454 |
+ |
lock.readLock().lock(); |
455 |
+ |
lock.readLock().unlock(); |
456 |
+ |
lock.writeLock().unlock(); |
457 |
+ |
t1.join(MEDIUM_DELAY_MS); |
458 |
+ |
t2.join(MEDIUM_DELAY_MS); |
459 |
+ |
assertTrue(!t1.isAlive()); |
460 |
+ |
assertTrue(!t2.isAlive()); |
461 |
+ |
|
462 |
+ |
} catch(Exception e){ |
463 |
+ |
unexpectedException(); |
464 |
+ |
} |
465 |
+ |
} |
466 |
+ |
|
467 |
+ |
/** |
468 |
+ |
* Read lock succeeds if write locked by current thread even if |
469 |
+ |
* other threads are waiting for writelock |
470 |
+ |
*/ |
471 |
+ |
public void testReadHoldingWriteLock3() { |
472 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
473 |
+ |
lock.writeLock().lock(); |
474 |
+ |
Thread t1 = new Thread(new Runnable() { |
475 |
+ |
public void run() { |
476 |
+ |
lock.writeLock().lock(); |
477 |
+ |
lock.writeLock().unlock(); |
478 |
+ |
} |
479 |
+ |
}); |
480 |
+ |
Thread t2 = new Thread(new Runnable() { |
481 |
+ |
public void run() { |
482 |
+ |
lock.writeLock().lock(); |
483 |
+ |
lock.writeLock().unlock(); |
484 |
+ |
} |
485 |
+ |
}); |
486 |
+ |
|
487 |
+ |
try { |
488 |
+ |
t1.start(); |
489 |
+ |
t2.start(); |
490 |
+ |
lock.readLock().lock(); |
491 |
+ |
lock.readLock().unlock(); |
492 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
493 |
+ |
lock.readLock().lock(); |
494 |
+ |
lock.readLock().unlock(); |
495 |
+ |
lock.writeLock().unlock(); |
496 |
+ |
t1.join(MEDIUM_DELAY_MS); |
497 |
+ |
t2.join(MEDIUM_DELAY_MS); |
498 |
+ |
assertTrue(!t1.isAlive()); |
499 |
+ |
assertTrue(!t2.isAlive()); |
500 |
+ |
|
501 |
+ |
} catch(Exception e){ |
502 |
+ |
unexpectedException(); |
503 |
+ |
} |
504 |
+ |
} |
505 |
+ |
|
506 |
+ |
|
507 |
+ |
/** |
508 |
+ |
* Write lock succeeds if write locked by current thread even if |
509 |
+ |
* other threads are waiting for writelock |
510 |
+ |
*/ |
511 |
+ |
public void testWriteHoldingWriteLock4() { |
512 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
513 |
+ |
lock.writeLock().lock(); |
514 |
+ |
Thread t1 = new Thread(new Runnable() { |
515 |
+ |
public void run() { |
516 |
+ |
lock.writeLock().lock(); |
517 |
+ |
lock.writeLock().unlock(); |
518 |
+ |
} |
519 |
+ |
}); |
520 |
+ |
Thread t2 = new Thread(new Runnable() { |
521 |
+ |
public void run() { |
522 |
+ |
lock.writeLock().lock(); |
523 |
+ |
lock.writeLock().unlock(); |
524 |
+ |
} |
525 |
+ |
}); |
526 |
+ |
|
527 |
+ |
try { |
528 |
+ |
t1.start(); |
529 |
+ |
t2.start(); |
530 |
+ |
lock.writeLock().lock(); |
531 |
+ |
lock.writeLock().unlock(); |
532 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
533 |
+ |
lock.writeLock().lock(); |
534 |
+ |
lock.writeLock().unlock(); |
535 |
+ |
lock.writeLock().unlock(); |
536 |
+ |
t1.join(MEDIUM_DELAY_MS); |
537 |
+ |
t2.join(MEDIUM_DELAY_MS); |
538 |
+ |
assertTrue(!t1.isAlive()); |
539 |
+ |
assertTrue(!t2.isAlive()); |
540 |
+ |
|
541 |
+ |
} catch(Exception e){ |
542 |
+ |
unexpectedException(); |
543 |
+ |
} |
544 |
+ |
} |
545 |
+ |
|
546 |
+ |
|
547 |
+ |
/** |
548 |
+ |
* Fair Read trylock succeeds if write locked by current thread |
549 |
+ |
*/ |
550 |
+ |
public void testReadHoldingWriteLockFair() { |
551 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
552 |
+ |
lock.writeLock().lock(); |
553 |
+ |
assertTrue(lock.readLock().tryLock()); |
554 |
+ |
lock.readLock().unlock(); |
555 |
+ |
lock.writeLock().unlock(); |
556 |
+ |
} |
557 |
+ |
|
558 |
+ |
/** |
559 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
560 |
+ |
* other threads are waiting for readlock |
561 |
+ |
*/ |
562 |
+ |
public void testReadHoldingWriteLockFair2() { |
563 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
564 |
+ |
lock.writeLock().lock(); |
565 |
+ |
Thread t1 = new Thread(new Runnable() { |
566 |
+ |
public void run() { |
567 |
+ |
lock.readLock().lock(); |
568 |
+ |
lock.readLock().unlock(); |
569 |
+ |
} |
570 |
+ |
}); |
571 |
+ |
Thread t2 = new Thread(new Runnable() { |
572 |
+ |
public void run() { |
573 |
+ |
lock.readLock().lock(); |
574 |
+ |
lock.readLock().unlock(); |
575 |
+ |
} |
576 |
+ |
}); |
577 |
+ |
|
578 |
+ |
try { |
579 |
+ |
t1.start(); |
580 |
+ |
t2.start(); |
581 |
+ |
lock.readLock().lock(); |
582 |
+ |
lock.readLock().unlock(); |
583 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
584 |
+ |
lock.readLock().lock(); |
585 |
+ |
lock.readLock().unlock(); |
586 |
+ |
lock.writeLock().unlock(); |
587 |
+ |
t1.join(MEDIUM_DELAY_MS); |
588 |
+ |
t2.join(MEDIUM_DELAY_MS); |
589 |
+ |
assertTrue(!t1.isAlive()); |
590 |
+ |
assertTrue(!t2.isAlive()); |
591 |
+ |
|
592 |
+ |
} catch(Exception e){ |
593 |
+ |
unexpectedException(); |
594 |
+ |
} |
595 |
+ |
} |
596 |
+ |
|
597 |
+ |
|
598 |
+ |
/** |
599 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
600 |
+ |
* other threads are waiting for writelock |
601 |
+ |
*/ |
602 |
+ |
public void testReadHoldingWriteLockFair3() { |
603 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
604 |
+ |
lock.writeLock().lock(); |
605 |
+ |
Thread t1 = new Thread(new Runnable() { |
606 |
+ |
public void run() { |
607 |
+ |
lock.writeLock().lock(); |
608 |
+ |
lock.writeLock().unlock(); |
609 |
+ |
} |
610 |
+ |
}); |
611 |
+ |
Thread t2 = new Thread(new Runnable() { |
612 |
+ |
public void run() { |
613 |
+ |
lock.writeLock().lock(); |
614 |
+ |
lock.writeLock().unlock(); |
615 |
+ |
} |
616 |
+ |
}); |
617 |
+ |
|
618 |
+ |
try { |
619 |
+ |
t1.start(); |
620 |
+ |
t2.start(); |
621 |
+ |
lock.readLock().lock(); |
622 |
+ |
lock.readLock().unlock(); |
623 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
624 |
+ |
lock.readLock().lock(); |
625 |
+ |
lock.readLock().unlock(); |
626 |
+ |
lock.writeLock().unlock(); |
627 |
+ |
t1.join(MEDIUM_DELAY_MS); |
628 |
+ |
t2.join(MEDIUM_DELAY_MS); |
629 |
+ |
assertTrue(!t1.isAlive()); |
630 |
+ |
assertTrue(!t2.isAlive()); |
631 |
+ |
|
632 |
+ |
} catch(Exception e){ |
633 |
+ |
unexpectedException(); |
634 |
+ |
} |
635 |
+ |
} |
636 |
+ |
|
637 |
+ |
|
638 |
+ |
/** |
639 |
+ |
* Fair Write lock succeeds if write locked by current thread even if |
640 |
+ |
* other threads are waiting for writelock |
641 |
+ |
*/ |
642 |
+ |
public void testWriteHoldingWriteLockFair4() { |
643 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
644 |
+ |
lock.writeLock().lock(); |
645 |
+ |
Thread t1 = new Thread(new Runnable() { |
646 |
+ |
public void run() { |
647 |
+ |
lock.writeLock().lock(); |
648 |
+ |
lock.writeLock().unlock(); |
649 |
+ |
} |
650 |
+ |
}); |
651 |
+ |
Thread t2 = new Thread(new Runnable() { |
652 |
+ |
public void run() { |
653 |
+ |
lock.writeLock().lock(); |
654 |
+ |
lock.writeLock().unlock(); |
655 |
+ |
} |
656 |
+ |
}); |
657 |
+ |
|
658 |
+ |
try { |
659 |
+ |
t1.start(); |
660 |
+ |
t2.start(); |
661 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
662 |
+ |
assertTrue(lock.isWriteLockedByCurrentThread()); |
663 |
+ |
assertTrue(lock.getWriteHoldCount() == 1); |
664 |
+ |
lock.writeLock().lock(); |
665 |
+ |
assertTrue(lock.getWriteHoldCount() == 2); |
666 |
+ |
lock.writeLock().unlock(); |
667 |
+ |
lock.writeLock().lock(); |
668 |
+ |
lock.writeLock().unlock(); |
669 |
+ |
lock.writeLock().unlock(); |
670 |
+ |
t1.join(MEDIUM_DELAY_MS); |
671 |
+ |
t2.join(MEDIUM_DELAY_MS); |
672 |
+ |
assertTrue(!t1.isAlive()); |
673 |
+ |
assertTrue(!t2.isAlive()); |
674 |
+ |
|
675 |
+ |
} catch(Exception e){ |
676 |
+ |
unexpectedException(); |
677 |
+ |
} |
678 |
+ |
} |
679 |
+ |
|
680 |
|
|
681 |
|
/** |
682 |
|
* Read tryLock succeeds if readlocked but not writelocked |
721 |
|
} |
722 |
|
} |
723 |
|
|
724 |
+ |
|
725 |
+ |
/** |
726 |
+ |
* Fair Read tryLock succeeds if readlocked but not writelocked |
727 |
+ |
*/ |
728 |
+ |
public void testTryLockWhenReadLockedFair() { |
729 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
730 |
+ |
lock.readLock().lock(); |
731 |
+ |
Thread t = new Thread(new Runnable() { |
732 |
+ |
public void run() { |
733 |
+ |
threadAssertTrue(lock.readLock().tryLock()); |
734 |
+ |
lock.readLock().unlock(); |
735 |
+ |
} |
736 |
+ |
}); |
737 |
+ |
try { |
738 |
+ |
t.start(); |
739 |
+ |
t.join(); |
740 |
+ |
lock.readLock().unlock(); |
741 |
+ |
} catch(Exception e){ |
742 |
+ |
unexpectedException(); |
743 |
+ |
} |
744 |
+ |
} |
745 |
+ |
|
746 |
+ |
|
747 |
+ |
|
748 |
+ |
/** |
749 |
+ |
* Fair write tryLock fails when readlocked |
750 |
+ |
*/ |
751 |
+ |
public void testWriteTryLockWhenReadLockedFair() { |
752 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
753 |
+ |
lock.readLock().lock(); |
754 |
+ |
Thread t = new Thread(new Runnable() { |
755 |
+ |
public void run() { |
756 |
+ |
threadAssertFalse(lock.writeLock().tryLock()); |
757 |
+ |
} |
758 |
+ |
}); |
759 |
+ |
try { |
760 |
+ |
t.start(); |
761 |
+ |
t.join(); |
762 |
+ |
lock.readLock().unlock(); |
763 |
+ |
} catch(Exception e){ |
764 |
+ |
unexpectedException(); |
765 |
+ |
} |
766 |
+ |
} |
767 |
+ |
|
768 |
|
|
769 |
|
|
770 |
|
/** |
838 |
|
}); |
839 |
|
try { |
840 |
|
t.start(); |
841 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
842 |
|
t.interrupt(); |
843 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
844 |
|
t.join(); |
845 |
|
lock.writeLock().unlock(); |
846 |
|
} catch(Exception e){ |
870 |
|
}); |
871 |
|
try { |
872 |
|
t.start(); |
873 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
874 |
|
t.interrupt(); |
875 |
|
t.join(); |
876 |
|
lock.writeLock().unlock(); |
939 |
|
final Condition c = lock.writeLock().newCondition(); |
940 |
|
try { |
941 |
|
lock.writeLock().lock(); |
595 |
– |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
942 |
|
lock.writeLock().unlock(); |
943 |
|
} |
944 |
|
catch (Exception ex) { |
955 |
|
try { |
956 |
|
lock.writeLock().lock(); |
957 |
|
java.util.Date d = new java.util.Date(); |
612 |
– |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
958 |
|
lock.writeLock().unlock(); |
959 |
|
} |
960 |
|
catch (Exception ex) { |
995 |
|
} |
996 |
|
} |
997 |
|
|
998 |
+ |
/** A helper class for uninterruptible wait tests */ |
999 |
+ |
class UninterruptableThread extends Thread { |
1000 |
+ |
private Lock lock; |
1001 |
+ |
private Condition c; |
1002 |
+ |
|
1003 |
+ |
public volatile boolean canAwake = false; |
1004 |
+ |
public volatile boolean interrupted = false; |
1005 |
+ |
public volatile boolean lockStarted = false; |
1006 |
+ |
|
1007 |
+ |
public UninterruptableThread(Lock lock, Condition c) { |
1008 |
+ |
this.lock = lock; |
1009 |
+ |
this.c = c; |
1010 |
+ |
} |
1011 |
+ |
|
1012 |
+ |
public synchronized void run() { |
1013 |
+ |
lock.lock(); |
1014 |
+ |
lockStarted = true; |
1015 |
+ |
|
1016 |
+ |
while (!canAwake) { |
1017 |
+ |
c.awaitUninterruptibly(); |
1018 |
+ |
} |
1019 |
+ |
|
1020 |
+ |
interrupted = isInterrupted(); |
1021 |
+ |
lock.unlock(); |
1022 |
+ |
} |
1023 |
+ |
} |
1024 |
+ |
|
1025 |
|
/** |
1026 |
|
* awaitUninterruptibly doesn't abort on interrupt |
1027 |
|
*/ |
1028 |
|
public void testAwaitUninterruptibly() { |
1029 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1029 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1030 |
|
final Condition c = lock.writeLock().newCondition(); |
1031 |
< |
Thread t = new Thread(new Runnable() { |
660 |
< |
public void run() { |
661 |
< |
lock.writeLock().lock(); |
662 |
< |
c.awaitUninterruptibly(); |
663 |
< |
lock.writeLock().unlock(); |
664 |
< |
} |
665 |
< |
}); |
1031 |
> |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
1032 |
|
|
1033 |
|
try { |
1034 |
< |
t.start(); |
1035 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1036 |
< |
t.interrupt(); |
1034 |
> |
thread.start(); |
1035 |
> |
|
1036 |
> |
while (!thread.lockStarted) { |
1037 |
> |
Thread.sleep(100); |
1038 |
> |
} |
1039 |
> |
|
1040 |
|
lock.writeLock().lock(); |
1041 |
< |
c.signal(); |
1042 |
< |
lock.writeLock().unlock(); |
1043 |
< |
assert(t.isInterrupted()); |
1044 |
< |
t.join(SHORT_DELAY_MS); |
1045 |
< |
assertFalse(t.isAlive()); |
1046 |
< |
} |
1047 |
< |
catch (Exception ex) { |
1041 |
> |
try { |
1042 |
> |
thread.interrupt(); |
1043 |
> |
thread.canAwake = true; |
1044 |
> |
c.signal(); |
1045 |
> |
} finally { |
1046 |
> |
lock.writeLock().unlock(); |
1047 |
> |
} |
1048 |
> |
|
1049 |
> |
thread.join(); |
1050 |
> |
assertTrue(thread.interrupted); |
1051 |
> |
assertFalse(thread.isAlive()); |
1052 |
> |
} catch (Exception ex) { |
1053 |
|
unexpectedException(); |
1054 |
|
} |
1055 |
|
} |
1252 |
|
} |
1253 |
|
|
1254 |
|
/** |
1255 |
+ |
* hasQueuedThread(null) throws NPE |
1256 |
+ |
*/ |
1257 |
+ |
public void testHasQueuedThreadNPE() { |
1258 |
+ |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1259 |
+ |
try { |
1260 |
+ |
sync.hasQueuedThread(null); |
1261 |
+ |
shouldThrow(); |
1262 |
+ |
} catch (NullPointerException success) { |
1263 |
+ |
} |
1264 |
+ |
} |
1265 |
+ |
|
1266 |
+ |
/** |
1267 |
+ |
* hasQueuedThread reports whether a thread is queued. |
1268 |
+ |
*/ |
1269 |
+ |
public void testHasQueuedThread() { |
1270 |
+ |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1271 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1272 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1273 |
+ |
try { |
1274 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1275 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
1276 |
+ |
sync.writeLock().lock(); |
1277 |
+ |
t1.start(); |
1278 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1279 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
1280 |
+ |
t2.start(); |
1281 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1282 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
1283 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
1284 |
+ |
t1.interrupt(); |
1285 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1286 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1287 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
1288 |
+ |
sync.writeLock().unlock(); |
1289 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1290 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1291 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1292 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
1293 |
+ |
t1.join(); |
1294 |
+ |
t2.join(); |
1295 |
+ |
} catch(Exception e){ |
1296 |
+ |
unexpectedException(); |
1297 |
+ |
} |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
|
1301 |
+ |
/** |
1302 |
|
* getQueueLength reports number of waiting threads |
1303 |
|
*/ |
1304 |
|
public void testGetQueueLength() { |
1646 |
|
} |
1647 |
|
} |
1648 |
|
|
1649 |
+ |
/** |
1650 |
+ |
* toString indicates current lock state |
1651 |
+ |
*/ |
1652 |
+ |
public void testToString() { |
1653 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1654 |
+ |
String us = lock.toString(); |
1655 |
+ |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1656 |
+ |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1657 |
+ |
lock.writeLock().lock(); |
1658 |
+ |
String ws = lock.toString(); |
1659 |
+ |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1660 |
+ |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1661 |
+ |
lock.writeLock().unlock(); |
1662 |
+ |
lock.readLock().lock(); |
1663 |
+ |
lock.readLock().lock(); |
1664 |
+ |
String rs = lock.toString(); |
1665 |
+ |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1666 |
+ |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1667 |
+ |
} |
1668 |
+ |
|
1669 |
+ |
/** |
1670 |
+ |
* readLock.toString indicates current lock state |
1671 |
+ |
*/ |
1672 |
+ |
public void testReadLockToString() { |
1673 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1674 |
+ |
String us = lock.readLock().toString(); |
1675 |
+ |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1676 |
+ |
lock.readLock().lock(); |
1677 |
+ |
lock.readLock().lock(); |
1678 |
+ |
String rs = lock.readLock().toString(); |
1679 |
+ |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1680 |
+ |
} |
1681 |
+ |
|
1682 |
+ |
/** |
1683 |
+ |
* writeLock.toString indicates current lock state |
1684 |
+ |
*/ |
1685 |
+ |
public void testWriteLockToString() { |
1686 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1687 |
+ |
String us = lock.writeLock().toString(); |
1688 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1689 |
+ |
lock.writeLock().lock(); |
1690 |
+ |
String ls = lock.writeLock().toString(); |
1691 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1692 |
+ |
} |
1693 |
+ |
|
1694 |
|
} |