1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
57 |
|
public Collection<Thread> getQueuedThreads() { |
58 |
|
return super.getQueuedThreads(); |
59 |
|
} |
60 |
< |
public ConditionObject newCondition() { |
61 |
< |
return new PublicCondition(this); |
60 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
61 |
> |
return super.getWaitingThreads(c); |
62 |
|
} |
63 |
|
|
63 |
– |
static class PublicCondition extends ReentrantLock.ConditionObject { |
64 |
– |
PublicCondition(PublicReentrantLock l) { super(l); } |
65 |
– |
public Collection<Thread> getWaitingThreads() { |
66 |
– |
return super.getWaitingThreads(); |
67 |
– |
} |
68 |
– |
} |
64 |
|
|
65 |
|
} |
66 |
|
|
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* trylock on an unlocked lock succeeds |
110 |
> |
* tryLock on an unlocked lock succeeds |
111 |
|
*/ |
112 |
|
public void testTryLock() { |
113 |
|
ReentrantLock rl = new ReentrantLock(); |
118 |
|
|
119 |
|
|
120 |
|
/** |
121 |
+ |
* hasQueuedThreads reports whether there are waiting threads |
122 |
+ |
*/ |
123 |
+ |
public void testhasQueuedThreads() { |
124 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
125 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
126 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
127 |
+ |
try { |
128 |
+ |
assertFalse(lock.hasQueuedThreads()); |
129 |
+ |
lock.lock(); |
130 |
+ |
t1.start(); |
131 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
132 |
+ |
assertTrue(lock.hasQueuedThreads()); |
133 |
+ |
t2.start(); |
134 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
135 |
+ |
assertTrue(lock.hasQueuedThreads()); |
136 |
+ |
t1.interrupt(); |
137 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
138 |
+ |
assertTrue(lock.hasQueuedThreads()); |
139 |
+ |
lock.unlock(); |
140 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
141 |
+ |
assertFalse(lock.hasQueuedThreads()); |
142 |
+ |
t1.join(); |
143 |
+ |
t2.join(); |
144 |
+ |
} catch(Exception e){ |
145 |
+ |
unexpectedException(); |
146 |
+ |
} |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
/** |
150 |
|
* getQueueLength reports number of waiting threads |
151 |
|
*/ |
152 |
|
public void testGetQueueLength() { |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
+ |
* getQueueLength reports number of waiting threads |
180 |
+ |
*/ |
181 |
+ |
public void testGetQueueLength_fair() { |
182 |
+ |
final ReentrantLock lock = new ReentrantLock(true); |
183 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
184 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
185 |
+ |
try { |
186 |
+ |
assertEquals(0, lock.getQueueLength()); |
187 |
+ |
lock.lock(); |
188 |
+ |
t1.start(); |
189 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
190 |
+ |
assertEquals(1, lock.getQueueLength()); |
191 |
+ |
t2.start(); |
192 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
193 |
+ |
assertEquals(2, lock.getQueueLength()); |
194 |
+ |
t1.interrupt(); |
195 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
196 |
+ |
assertEquals(1, lock.getQueueLength()); |
197 |
+ |
lock.unlock(); |
198 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
199 |
+ |
assertEquals(0, lock.getQueueLength()); |
200 |
+ |
t1.join(); |
201 |
+ |
t2.join(); |
202 |
+ |
} catch(Exception e){ |
203 |
+ |
unexpectedException(); |
204 |
+ |
} |
205 |
+ |
} |
206 |
+ |
|
207 |
+ |
/** |
208 |
|
* getQueuedThreads includes waiting threads |
209 |
|
*/ |
210 |
|
public void testGetQueuedThreads() { |
238 |
|
|
239 |
|
|
240 |
|
/** |
241 |
< |
* timed trylock is interruptible. |
241 |
> |
* timed tryLock is interruptible. |
242 |
|
*/ |
243 |
|
public void testInterruptedException2() { |
244 |
|
final ReentrantLock lock = new ReentrantLock(); |
261 |
|
|
262 |
|
|
263 |
|
/** |
264 |
< |
* Trylock on a locked lock fails |
264 |
> |
* TryLock on a locked lock fails |
265 |
|
*/ |
266 |
|
public void testTryLockWhenLocked() { |
267 |
|
final ReentrantLock lock = new ReentrantLock(); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* Timed trylock on a locked lock times out |
284 |
> |
* Timed tryLock on a locked lock times out |
285 |
|
*/ |
286 |
|
public void testTryLock_Timeout() { |
287 |
|
final ReentrantLock lock = new ReentrantLock(); |
481 |
|
*/ |
482 |
|
public void testAwait() { |
483 |
|
final ReentrantLock lock = new ReentrantLock(); |
484 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
484 |
> |
final Condition c = lock.newCondition(); |
485 |
|
Thread t = new Thread(new Runnable() { |
486 |
|
public void run() { |
487 |
|
try { |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
+ |
* hasWaiters throws NPE if null |
514 |
+ |
*/ |
515 |
+ |
public void testHasWaitersNPE() { |
516 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
517 |
+ |
try { |
518 |
+ |
lock.hasWaiters(null); |
519 |
+ |
shouldThrow(); |
520 |
+ |
} catch (NullPointerException success) { |
521 |
+ |
} catch (Exception ex) { |
522 |
+ |
unexpectedException(); |
523 |
+ |
} |
524 |
+ |
} |
525 |
+ |
|
526 |
+ |
/** |
527 |
+ |
* getWaitQueueLength throws NPE if null |
528 |
+ |
*/ |
529 |
+ |
public void testGetWaitQueueLengthNPE() { |
530 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
531 |
+ |
try { |
532 |
+ |
lock.getWaitQueueLength(null); |
533 |
+ |
shouldThrow(); |
534 |
+ |
} catch (NullPointerException success) { |
535 |
+ |
} catch (Exception ex) { |
536 |
+ |
unexpectedException(); |
537 |
+ |
} |
538 |
+ |
} |
539 |
+ |
|
540 |
+ |
|
541 |
+ |
/** |
542 |
+ |
* getWaitingThreads throws NPE if null |
543 |
+ |
*/ |
544 |
+ |
public void testGetWaitingThreadsNPE() { |
545 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
546 |
+ |
try { |
547 |
+ |
lock.getWaitingThreads(null); |
548 |
+ |
shouldThrow(); |
549 |
+ |
} catch (NullPointerException success) { |
550 |
+ |
} catch (Exception ex) { |
551 |
+ |
unexpectedException(); |
552 |
+ |
} |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
|
556 |
+ |
/** |
557 |
+ |
* hasWaiters throws IAE if not owned |
558 |
+ |
*/ |
559 |
+ |
public void testHasWaitersIAE() { |
560 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
561 |
+ |
final Condition c = (lock.newCondition()); |
562 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
563 |
+ |
try { |
564 |
+ |
lock2.hasWaiters(c); |
565 |
+ |
shouldThrow(); |
566 |
+ |
} catch (IllegalArgumentException success) { |
567 |
+ |
} catch (Exception ex) { |
568 |
+ |
unexpectedException(); |
569 |
+ |
} |
570 |
+ |
} |
571 |
+ |
|
572 |
+ |
/** |
573 |
+ |
* hasWaiters throws IMSE if not locked |
574 |
+ |
*/ |
575 |
+ |
public void testHasWaitersIMSE() { |
576 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
577 |
+ |
final Condition c = (lock.newCondition()); |
578 |
+ |
try { |
579 |
+ |
lock.hasWaiters(c); |
580 |
+ |
shouldThrow(); |
581 |
+ |
} catch (IllegalMonitorStateException success) { |
582 |
+ |
} catch (Exception ex) { |
583 |
+ |
unexpectedException(); |
584 |
+ |
} |
585 |
+ |
} |
586 |
+ |
|
587 |
+ |
|
588 |
+ |
/** |
589 |
+ |
* getWaitQueueLength throws IAE if not owned |
590 |
+ |
*/ |
591 |
+ |
public void testGetWaitQueueLengthIAE() { |
592 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
593 |
+ |
final Condition c = (lock.newCondition()); |
594 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
595 |
+ |
try { |
596 |
+ |
lock2.getWaitQueueLength(c); |
597 |
+ |
shouldThrow(); |
598 |
+ |
} catch (IllegalArgumentException success) { |
599 |
+ |
} catch (Exception ex) { |
600 |
+ |
unexpectedException(); |
601 |
+ |
} |
602 |
+ |
} |
603 |
+ |
|
604 |
+ |
/** |
605 |
+ |
* getWaitQueueLength throws IMSE if not locked |
606 |
+ |
*/ |
607 |
+ |
public void testGetWaitQueueLengthIMSE() { |
608 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
609 |
+ |
final Condition c = (lock.newCondition()); |
610 |
+ |
try { |
611 |
+ |
lock.getWaitQueueLength(c); |
612 |
+ |
shouldThrow(); |
613 |
+ |
} catch (IllegalMonitorStateException success) { |
614 |
+ |
} catch (Exception ex) { |
615 |
+ |
unexpectedException(); |
616 |
+ |
} |
617 |
+ |
} |
618 |
+ |
|
619 |
+ |
|
620 |
+ |
/** |
621 |
+ |
* getWaitingThreads throws IAE if not owned |
622 |
+ |
*/ |
623 |
+ |
public void testGetWaitingThreadsIAE() { |
624 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
625 |
+ |
final Condition c = (lock.newCondition()); |
626 |
+ |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
627 |
+ |
try { |
628 |
+ |
lock2.getWaitingThreads(c); |
629 |
+ |
shouldThrow(); |
630 |
+ |
} catch (IllegalArgumentException success) { |
631 |
+ |
} catch (Exception ex) { |
632 |
+ |
unexpectedException(); |
633 |
+ |
} |
634 |
+ |
} |
635 |
+ |
|
636 |
+ |
/** |
637 |
+ |
* getWaitingThreads throws IMSE if not locked |
638 |
+ |
*/ |
639 |
+ |
public void testGetWaitingThreadsIMSE() { |
640 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
641 |
+ |
final Condition c = (lock.newCondition()); |
642 |
+ |
try { |
643 |
+ |
lock.getWaitingThreads(c); |
644 |
+ |
shouldThrow(); |
645 |
+ |
} catch (IllegalMonitorStateException success) { |
646 |
+ |
} catch (Exception ex) { |
647 |
+ |
unexpectedException(); |
648 |
+ |
} |
649 |
+ |
} |
650 |
+ |
|
651 |
+ |
|
652 |
+ |
|
653 |
+ |
/** |
654 |
|
* hasWaiters returns true when a thread is waiting, else false |
655 |
|
*/ |
656 |
|
public void testHasWaiters() { |
657 |
|
final ReentrantLock lock = new ReentrantLock(); |
658 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
658 |
> |
final Condition c = lock.newCondition(); |
659 |
|
Thread t = new Thread(new Runnable() { |
660 |
|
public void run() { |
661 |
|
try { |
662 |
|
lock.lock(); |
663 |
< |
threadAssertFalse(c.hasWaiters()); |
664 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
663 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
664 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
665 |
|
c.await(); |
666 |
|
lock.unlock(); |
667 |
|
} |
675 |
|
t.start(); |
676 |
|
Thread.sleep(SHORT_DELAY_MS); |
677 |
|
lock.lock(); |
678 |
< |
assertTrue(c.hasWaiters()); |
679 |
< |
assertEquals(1, c.getWaitQueueLength()); |
678 |
> |
assertTrue(lock.hasWaiters(c)); |
679 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
680 |
|
c.signal(); |
681 |
|
lock.unlock(); |
682 |
|
Thread.sleep(SHORT_DELAY_MS); |
683 |
|
lock.lock(); |
684 |
< |
assertFalse(c.hasWaiters()); |
685 |
< |
assertEquals(0, c.getWaitQueueLength()); |
684 |
> |
assertFalse(lock.hasWaiters(c)); |
685 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
686 |
|
lock.unlock(); |
687 |
|
t.join(SHORT_DELAY_MS); |
688 |
|
assertFalse(t.isAlive()); |
697 |
|
*/ |
698 |
|
public void testGetWaitQueueLength() { |
699 |
|
final ReentrantLock lock = new ReentrantLock(); |
700 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
700 |
> |
final Condition c = lock.newCondition(); |
701 |
|
Thread t1 = new Thread(new Runnable() { |
702 |
|
public void run() { |
703 |
|
try { |
704 |
|
lock.lock(); |
705 |
< |
threadAssertFalse(c.hasWaiters()); |
706 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
705 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
706 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
707 |
|
c.await(); |
708 |
|
lock.unlock(); |
709 |
|
} |
717 |
|
public void run() { |
718 |
|
try { |
719 |
|
lock.lock(); |
720 |
< |
threadAssertTrue(c.hasWaiters()); |
721 |
< |
threadAssertEquals(1, c.getWaitQueueLength()); |
720 |
> |
threadAssertTrue(lock.hasWaiters(c)); |
721 |
> |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
722 |
|
c.await(); |
723 |
|
lock.unlock(); |
724 |
|
} |
734 |
|
t2.start(); |
735 |
|
Thread.sleep(SHORT_DELAY_MS); |
736 |
|
lock.lock(); |
737 |
< |
assertTrue(c.hasWaiters()); |
738 |
< |
assertEquals(2, c.getWaitQueueLength()); |
737 |
> |
assertTrue(lock.hasWaiters(c)); |
738 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
739 |
|
c.signalAll(); |
740 |
|
lock.unlock(); |
741 |
|
Thread.sleep(SHORT_DELAY_MS); |
742 |
|
lock.lock(); |
743 |
< |
assertFalse(c.hasWaiters()); |
744 |
< |
assertEquals(0, c.getWaitQueueLength()); |
743 |
> |
assertFalse(lock.hasWaiters(c)); |
744 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
745 |
|
lock.unlock(); |
746 |
|
t1.join(SHORT_DELAY_MS); |
747 |
|
t2.join(SHORT_DELAY_MS); |
758 |
|
*/ |
759 |
|
public void testGetWaitingThreads() { |
760 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
761 |
< |
final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition(); |
761 |
> |
final Condition c = lock.newCondition(); |
762 |
|
Thread t1 = new Thread(new Runnable() { |
763 |
|
public void run() { |
764 |
|
try { |
765 |
|
lock.lock(); |
766 |
< |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
766 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
767 |
|
c.await(); |
768 |
|
lock.unlock(); |
769 |
|
} |
777 |
|
public void run() { |
778 |
|
try { |
779 |
|
lock.lock(); |
780 |
< |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
780 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
781 |
|
c.await(); |
782 |
|
lock.unlock(); |
783 |
|
} |
789 |
|
|
790 |
|
try { |
791 |
|
lock.lock(); |
792 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
792 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
793 |
|
lock.unlock(); |
794 |
|
t1.start(); |
795 |
|
Thread.sleep(SHORT_DELAY_MS); |
796 |
|
t2.start(); |
797 |
|
Thread.sleep(SHORT_DELAY_MS); |
798 |
|
lock.lock(); |
799 |
< |
assertTrue(c.hasWaiters()); |
800 |
< |
assertTrue(c.getWaitingThreads().contains(t1)); |
801 |
< |
assertTrue(c.getWaitingThreads().contains(t2)); |
799 |
> |
assertTrue(lock.hasWaiters(c)); |
800 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
801 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
802 |
|
c.signalAll(); |
803 |
|
lock.unlock(); |
804 |
|
Thread.sleep(SHORT_DELAY_MS); |
805 |
|
lock.lock(); |
806 |
< |
assertFalse(c.hasWaiters()); |
807 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
806 |
> |
assertFalse(lock.hasWaiters(c)); |
807 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
808 |
|
lock.unlock(); |
809 |
|
t1.join(SHORT_DELAY_MS); |
810 |
|
t2.join(SHORT_DELAY_MS); |
816 |
|
} |
817 |
|
} |
818 |
|
|
819 |
+ |
|
820 |
+ |
|
821 |
|
/** |
822 |
|
* awaitUninterruptibly doesn't abort on interrupt |
823 |
|
*/ |
889 |
|
public void run() { |
890 |
|
try { |
891 |
|
lock.lock(); |
892 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
892 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
893 |
|
lock.unlock(); |
894 |
|
threadShouldThrow(); |
895 |
|
} |
1016 |
|
} |
1017 |
|
} |
1018 |
|
|
1019 |
+ |
/** |
1020 |
+ |
* toString indicates current lock state |
1021 |
+ |
*/ |
1022 |
+ |
public void testToString() { |
1023 |
+ |
ReentrantLock lock = new ReentrantLock(); |
1024 |
+ |
String us = lock.toString(); |
1025 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1026 |
+ |
lock.lock(); |
1027 |
+ |
String ls = lock.toString(); |
1028 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1029 |
+ |
} |
1030 |
+ |
|
1031 |
|
} |