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 |
|
|
67 |
< |
/* |
67 |
> |
/** |
68 |
> |
* Constructor sets given fairness |
69 |
> |
*/ |
70 |
> |
public void testConstructor() { |
71 |
> |
ReentrantLock rl = new ReentrantLock(); |
72 |
> |
assertFalse(rl.isFair()); |
73 |
> |
ReentrantLock r2 = new ReentrantLock(true); |
74 |
> |
assertTrue(r2.isFair()); |
75 |
> |
} |
76 |
> |
|
77 |
> |
/** |
78 |
|
* locking an unlocked lock succeeds |
79 |
|
*/ |
80 |
|
public void testLock() { |
84 |
|
rl.unlock(); |
85 |
|
} |
86 |
|
|
87 |
< |
/* |
87 |
> |
/** |
88 |
|
* locking an unlocked fair lock succeeds |
89 |
|
*/ |
90 |
|
public void testFairLock() { |
94 |
|
rl.unlock(); |
95 |
|
} |
96 |
|
|
97 |
< |
/* |
97 |
> |
/** |
98 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
99 |
|
*/ |
100 |
|
public void testUnlock_IllegalMonitorStateException() { |
106 |
|
} catch(IllegalMonitorStateException success){} |
107 |
|
} |
108 |
|
|
109 |
< |
/* |
110 |
< |
* trylock on an unlocked lock succeeds |
109 |
> |
/** |
110 |
> |
* tryLock on an unlocked lock succeeds |
111 |
|
*/ |
112 |
|
public void testTryLock() { |
113 |
|
ReentrantLock rl = new ReentrantLock(); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
/* |
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() { |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
< |
/* |
178 |
> |
/** |
179 |
|
* getQueuedThreads includes waiting threads |
180 |
|
*/ |
181 |
|
public void testGetQueuedThreads() { |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
< |
/* |
212 |
< |
* timed trylock is interruptible. |
211 |
> |
/** |
212 |
> |
* timed tryLock is interruptible. |
213 |
|
*/ |
214 |
|
public void testInterruptedException2() { |
215 |
|
final ReentrantLock lock = new ReentrantLock(); |
232 |
|
|
233 |
|
|
234 |
|
/** |
235 |
< |
* Trylock on a locked lock fails |
235 |
> |
* TryLock on a locked lock fails |
236 |
|
*/ |
237 |
|
public void testTryLockWhenLocked() { |
238 |
|
final ReentrantLock lock = new ReentrantLock(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* Timed trylock on a locked lock times out |
255 |
> |
* Timed tryLock on a locked lock times out |
256 |
|
*/ |
257 |
|
public void testTryLock_Timeout() { |
258 |
|
final ReentrantLock lock = new ReentrantLock(); |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
< |
/* |
327 |
> |
/** |
328 |
|
* lockInterruptibly is interruptible. |
329 |
|
*/ |
330 |
|
public void testLockInterruptibly1() { |
452 |
|
*/ |
453 |
|
public void testAwait() { |
454 |
|
final ReentrantLock lock = new ReentrantLock(); |
455 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
455 |
> |
final Condition c = lock.newCondition(); |
456 |
|
Thread t = new Thread(new Runnable() { |
457 |
|
public void run() { |
458 |
|
try { |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
+ |
* hasWaiters throws NPE if null |
485 |
+ |
*/ |
486 |
+ |
public void testHasWaitersNPE() { |
487 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
488 |
+ |
try { |
489 |
+ |
lock.hasWaiters(null); |
490 |
+ |
shouldThrow(); |
491 |
+ |
} catch (NullPointerException success) { |
492 |
+ |
} catch (Exception ex) { |
493 |
+ |
unexpectedException(); |
494 |
+ |
} |
495 |
+ |
} |
496 |
+ |
|
497 |
+ |
/** |
498 |
+ |
* getWaitQueueLength throws NPE if null |
499 |
+ |
*/ |
500 |
+ |
public void testGetWaitQueueLengthNPE() { |
501 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
502 |
+ |
try { |
503 |
+ |
lock.getWaitQueueLength(null); |
504 |
+ |
shouldThrow(); |
505 |
+ |
} catch (NullPointerException success) { |
506 |
+ |
} catch (Exception ex) { |
507 |
+ |
unexpectedException(); |
508 |
+ |
} |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
|
512 |
+ |
/** |
513 |
+ |
* getWaitingThreads throws NPE if null |
514 |
+ |
*/ |
515 |
+ |
public void testGetWaitingThreadsNPE() { |
516 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
517 |
+ |
try { |
518 |
+ |
lock.getWaitingThreads(null); |
519 |
+ |
shouldThrow(); |
520 |
+ |
} catch (NullPointerException success) { |
521 |
+ |
} catch (Exception ex) { |
522 |
+ |
unexpectedException(); |
523 |
+ |
} |
524 |
+ |
} |
525 |
+ |
|
526 |
+ |
|
527 |
+ |
/** |
528 |
+ |
* hasWaiters throws IAE if not owned |
529 |
+ |
*/ |
530 |
+ |
public void testHasWaitersIAE() { |
531 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
532 |
+ |
final Condition c = (lock.newCondition()); |
533 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
534 |
+ |
try { |
535 |
+ |
lock2.hasWaiters(c); |
536 |
+ |
shouldThrow(); |
537 |
+ |
} catch (IllegalArgumentException success) { |
538 |
+ |
} catch (Exception ex) { |
539 |
+ |
unexpectedException(); |
540 |
+ |
} |
541 |
+ |
} |
542 |
+ |
|
543 |
+ |
/** |
544 |
+ |
* hasWaiters throws IMSE if not locked |
545 |
+ |
*/ |
546 |
+ |
public void testHasWaitersIMSE() { |
547 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
548 |
+ |
final Condition c = (lock.newCondition()); |
549 |
+ |
try { |
550 |
+ |
lock.hasWaiters(c); |
551 |
+ |
shouldThrow(); |
552 |
+ |
} catch (IllegalMonitorStateException success) { |
553 |
+ |
} catch (Exception ex) { |
554 |
+ |
unexpectedException(); |
555 |
+ |
} |
556 |
+ |
} |
557 |
+ |
|
558 |
+ |
|
559 |
+ |
/** |
560 |
+ |
* getWaitQueueLength throws IAE if not owned |
561 |
+ |
*/ |
562 |
+ |
public void testGetWaitQueueLengthIAE() { |
563 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
564 |
+ |
final Condition c = (lock.newCondition()); |
565 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
566 |
+ |
try { |
567 |
+ |
lock2.getWaitQueueLength(c); |
568 |
+ |
shouldThrow(); |
569 |
+ |
} catch (IllegalArgumentException success) { |
570 |
+ |
} catch (Exception ex) { |
571 |
+ |
unexpectedException(); |
572 |
+ |
} |
573 |
+ |
} |
574 |
+ |
|
575 |
+ |
/** |
576 |
+ |
* getWaitQueueLength throws IMSE if not locked |
577 |
+ |
*/ |
578 |
+ |
public void testGetWaitQueueLengthIMSE() { |
579 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
580 |
+ |
final Condition c = (lock.newCondition()); |
581 |
+ |
try { |
582 |
+ |
lock.getWaitQueueLength(c); |
583 |
+ |
shouldThrow(); |
584 |
+ |
} catch (IllegalMonitorStateException success) { |
585 |
+ |
} catch (Exception ex) { |
586 |
+ |
unexpectedException(); |
587 |
+ |
} |
588 |
+ |
} |
589 |
+ |
|
590 |
+ |
|
591 |
+ |
/** |
592 |
+ |
* getWaitingThreads throws IAE if not owned |
593 |
+ |
*/ |
594 |
+ |
public void testGetWaitingThreadsIAE() { |
595 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
596 |
+ |
final Condition c = (lock.newCondition()); |
597 |
+ |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
598 |
+ |
try { |
599 |
+ |
lock2.getWaitingThreads(c); |
600 |
+ |
shouldThrow(); |
601 |
+ |
} catch (IllegalArgumentException success) { |
602 |
+ |
} catch (Exception ex) { |
603 |
+ |
unexpectedException(); |
604 |
+ |
} |
605 |
+ |
} |
606 |
+ |
|
607 |
+ |
/** |
608 |
+ |
* getWaitingThreads throws IMSE if not locked |
609 |
+ |
*/ |
610 |
+ |
public void testGetWaitingThreadsIMSE() { |
611 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
612 |
+ |
final Condition c = (lock.newCondition()); |
613 |
+ |
try { |
614 |
+ |
lock.getWaitingThreads(c); |
615 |
+ |
shouldThrow(); |
616 |
+ |
} catch (IllegalMonitorStateException success) { |
617 |
+ |
} catch (Exception ex) { |
618 |
+ |
unexpectedException(); |
619 |
+ |
} |
620 |
+ |
} |
621 |
+ |
|
622 |
+ |
|
623 |
+ |
|
624 |
+ |
/** |
625 |
|
* hasWaiters returns true when a thread is waiting, else false |
626 |
|
*/ |
627 |
|
public void testHasWaiters() { |
628 |
|
final ReentrantLock lock = new ReentrantLock(); |
629 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
629 |
> |
final Condition c = lock.newCondition(); |
630 |
|
Thread t = new Thread(new Runnable() { |
631 |
|
public void run() { |
632 |
|
try { |
633 |
|
lock.lock(); |
634 |
< |
threadAssertFalse(c.hasWaiters()); |
635 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
634 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
635 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
636 |
|
c.await(); |
637 |
|
lock.unlock(); |
638 |
|
} |
646 |
|
t.start(); |
647 |
|
Thread.sleep(SHORT_DELAY_MS); |
648 |
|
lock.lock(); |
649 |
< |
assertTrue(c.hasWaiters()); |
650 |
< |
assertEquals(1, c.getWaitQueueLength()); |
649 |
> |
assertTrue(lock.hasWaiters(c)); |
650 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
651 |
|
c.signal(); |
652 |
|
lock.unlock(); |
653 |
|
Thread.sleep(SHORT_DELAY_MS); |
654 |
|
lock.lock(); |
655 |
< |
assertFalse(c.hasWaiters()); |
656 |
< |
assertEquals(0, c.getWaitQueueLength()); |
655 |
> |
assertFalse(lock.hasWaiters(c)); |
656 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
657 |
|
lock.unlock(); |
658 |
|
t.join(SHORT_DELAY_MS); |
659 |
|
assertFalse(t.isAlive()); |
668 |
|
*/ |
669 |
|
public void testGetWaitQueueLength() { |
670 |
|
final ReentrantLock lock = new ReentrantLock(); |
671 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
671 |
> |
final Condition c = lock.newCondition(); |
672 |
|
Thread t1 = new Thread(new Runnable() { |
673 |
|
public void run() { |
674 |
|
try { |
675 |
|
lock.lock(); |
676 |
< |
threadAssertFalse(c.hasWaiters()); |
677 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
676 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
677 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
678 |
|
c.await(); |
679 |
|
lock.unlock(); |
680 |
|
} |
688 |
|
public void run() { |
689 |
|
try { |
690 |
|
lock.lock(); |
691 |
< |
threadAssertTrue(c.hasWaiters()); |
692 |
< |
threadAssertEquals(1, c.getWaitQueueLength()); |
691 |
> |
threadAssertTrue(lock.hasWaiters(c)); |
692 |
> |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
693 |
|
c.await(); |
694 |
|
lock.unlock(); |
695 |
|
} |
705 |
|
t2.start(); |
706 |
|
Thread.sleep(SHORT_DELAY_MS); |
707 |
|
lock.lock(); |
708 |
< |
assertTrue(c.hasWaiters()); |
709 |
< |
assertEquals(2, c.getWaitQueueLength()); |
708 |
> |
assertTrue(lock.hasWaiters(c)); |
709 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
710 |
|
c.signalAll(); |
711 |
|
lock.unlock(); |
712 |
|
Thread.sleep(SHORT_DELAY_MS); |
713 |
|
lock.lock(); |
714 |
< |
assertFalse(c.hasWaiters()); |
715 |
< |
assertEquals(0, c.getWaitQueueLength()); |
714 |
> |
assertFalse(lock.hasWaiters(c)); |
715 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
716 |
|
lock.unlock(); |
717 |
|
t1.join(SHORT_DELAY_MS); |
718 |
|
t2.join(SHORT_DELAY_MS); |
729 |
|
*/ |
730 |
|
public void testGetWaitingThreads() { |
731 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
732 |
< |
final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition(); |
732 |
> |
final Condition c = lock.newCondition(); |
733 |
|
Thread t1 = new Thread(new Runnable() { |
734 |
|
public void run() { |
735 |
|
try { |
736 |
|
lock.lock(); |
737 |
< |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
737 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
738 |
|
c.await(); |
739 |
|
lock.unlock(); |
740 |
|
} |
748 |
|
public void run() { |
749 |
|
try { |
750 |
|
lock.lock(); |
751 |
< |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
751 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
752 |
|
c.await(); |
753 |
|
lock.unlock(); |
754 |
|
} |
760 |
|
|
761 |
|
try { |
762 |
|
lock.lock(); |
763 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
763 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
764 |
|
lock.unlock(); |
765 |
|
t1.start(); |
766 |
|
Thread.sleep(SHORT_DELAY_MS); |
767 |
|
t2.start(); |
768 |
|
Thread.sleep(SHORT_DELAY_MS); |
769 |
|
lock.lock(); |
770 |
< |
assertTrue(c.hasWaiters()); |
771 |
< |
assertTrue(c.getWaitingThreads().contains(t1)); |
772 |
< |
assertTrue(c.getWaitingThreads().contains(t2)); |
770 |
> |
assertTrue(lock.hasWaiters(c)); |
771 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
772 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
773 |
|
c.signalAll(); |
774 |
|
lock.unlock(); |
775 |
|
Thread.sleep(SHORT_DELAY_MS); |
776 |
|
lock.lock(); |
777 |
< |
assertFalse(c.hasWaiters()); |
778 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
777 |
> |
assertFalse(lock.hasWaiters(c)); |
778 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
779 |
|
lock.unlock(); |
780 |
|
t1.join(SHORT_DELAY_MS); |
781 |
|
t2.join(SHORT_DELAY_MS); |
787 |
|
} |
788 |
|
} |
789 |
|
|
790 |
+ |
|
791 |
+ |
|
792 |
|
/** |
793 |
|
* awaitUninterruptibly doesn't abort on interrupt |
794 |
|
*/ |