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(); |
60 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
61 |
> |
return super.getWaitingThreads(c); |
62 |
|
} |
63 |
|
|
63 |
– |
class PublicCondition extends ReentrantLock.ConditionObject { |
64 |
– |
PublicCondition() { } |
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 |
+ |
* hasQueuedThread(null) throws NPE |
209 |
+ |
*/ |
210 |
+ |
public void testHasQueuedThreadNPE() { |
211 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
212 |
+ |
try { |
213 |
+ |
sync.hasQueuedThread(null); |
214 |
+ |
shouldThrow(); |
215 |
+ |
} catch (NullPointerException success) { |
216 |
+ |
} |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
/** |
220 |
+ |
* hasQueuedThread reports whether a thread is queued. |
221 |
+ |
*/ |
222 |
+ |
public void testHasQueuedThread() { |
223 |
+ |
final ReentrantLock sync = new ReentrantLock(); |
224 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
225 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
226 |
+ |
try { |
227 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
228 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
229 |
+ |
sync.lock(); |
230 |
+ |
t1.start(); |
231 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
232 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
233 |
+ |
t2.start(); |
234 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
235 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
236 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
237 |
+ |
t1.interrupt(); |
238 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
239 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
240 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
241 |
+ |
sync.unlock(); |
242 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
243 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
244 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
245 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
246 |
+ |
t1.join(); |
247 |
+ |
t2.join(); |
248 |
+ |
} catch(Exception e){ |
249 |
+ |
unexpectedException(); |
250 |
+ |
} |
251 |
+ |
} |
252 |
+ |
|
253 |
+ |
|
254 |
+ |
/** |
255 |
|
* getQueuedThreads includes waiting threads |
256 |
|
*/ |
257 |
|
public void testGetQueuedThreads() { |
285 |
|
|
286 |
|
|
287 |
|
/** |
288 |
< |
* timed trylock is interruptible. |
288 |
> |
* timed tryLock is interruptible. |
289 |
|
*/ |
290 |
|
public void testInterruptedException2() { |
291 |
|
final ReentrantLock lock = new ReentrantLock(); |
308 |
|
|
309 |
|
|
310 |
|
/** |
311 |
< |
* Trylock on a locked lock fails |
311 |
> |
* TryLock on a locked lock fails |
312 |
|
*/ |
313 |
|
public void testTryLockWhenLocked() { |
314 |
|
final ReentrantLock lock = new ReentrantLock(); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Timed trylock on a locked lock times out |
331 |
> |
* Timed tryLock on a locked lock times out |
332 |
|
*/ |
333 |
|
public void testTryLock_Timeout() { |
334 |
|
final ReentrantLock lock = new ReentrantLock(); |
409 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
410 |
|
try { |
411 |
|
t.start(); |
412 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
413 |
|
t.interrupt(); |
414 |
|
lock.unlock(); |
415 |
|
t.join(); |
499 |
|
final Condition c = lock.newCondition(); |
500 |
|
try { |
501 |
|
lock.lock(); |
502 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
502 |
> |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
503 |
|
lock.unlock(); |
504 |
|
} |
505 |
|
catch (Exception ex) { |
516 |
|
try { |
517 |
|
lock.lock(); |
518 |
|
java.util.Date d = new java.util.Date(); |
519 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
519 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10)); |
520 |
|
lock.unlock(); |
521 |
|
} |
522 |
|
catch (Exception ex) { |
529 |
|
*/ |
530 |
|
public void testAwait() { |
531 |
|
final ReentrantLock lock = new ReentrantLock(); |
532 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
532 |
> |
final Condition c = lock.newCondition(); |
533 |
|
Thread t = new Thread(new Runnable() { |
534 |
|
public void run() { |
535 |
|
try { |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
+ |
* hasWaiters throws NPE if null |
562 |
+ |
*/ |
563 |
+ |
public void testHasWaitersNPE() { |
564 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
565 |
+ |
try { |
566 |
+ |
lock.hasWaiters(null); |
567 |
+ |
shouldThrow(); |
568 |
+ |
} catch (NullPointerException success) { |
569 |
+ |
} catch (Exception ex) { |
570 |
+ |
unexpectedException(); |
571 |
+ |
} |
572 |
+ |
} |
573 |
+ |
|
574 |
+ |
/** |
575 |
+ |
* getWaitQueueLength throws NPE if null |
576 |
+ |
*/ |
577 |
+ |
public void testGetWaitQueueLengthNPE() { |
578 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
579 |
+ |
try { |
580 |
+ |
lock.getWaitQueueLength(null); |
581 |
+ |
shouldThrow(); |
582 |
+ |
} catch (NullPointerException success) { |
583 |
+ |
} catch (Exception ex) { |
584 |
+ |
unexpectedException(); |
585 |
+ |
} |
586 |
+ |
} |
587 |
+ |
|
588 |
+ |
|
589 |
+ |
/** |
590 |
+ |
* getWaitingThreads throws NPE if null |
591 |
+ |
*/ |
592 |
+ |
public void testGetWaitingThreadsNPE() { |
593 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
594 |
+ |
try { |
595 |
+ |
lock.getWaitingThreads(null); |
596 |
+ |
shouldThrow(); |
597 |
+ |
} catch (NullPointerException success) { |
598 |
+ |
} catch (Exception ex) { |
599 |
+ |
unexpectedException(); |
600 |
+ |
} |
601 |
+ |
} |
602 |
+ |
|
603 |
+ |
|
604 |
+ |
/** |
605 |
+ |
* hasWaiters throws IAE if not owned |
606 |
+ |
*/ |
607 |
+ |
public void testHasWaitersIAE() { |
608 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
609 |
+ |
final Condition c = (lock.newCondition()); |
610 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
611 |
+ |
try { |
612 |
+ |
lock2.hasWaiters(c); |
613 |
+ |
shouldThrow(); |
614 |
+ |
} catch (IllegalArgumentException success) { |
615 |
+ |
} catch (Exception ex) { |
616 |
+ |
unexpectedException(); |
617 |
+ |
} |
618 |
+ |
} |
619 |
+ |
|
620 |
+ |
/** |
621 |
+ |
* hasWaiters throws IMSE if not locked |
622 |
+ |
*/ |
623 |
+ |
public void testHasWaitersIMSE() { |
624 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
625 |
+ |
final Condition c = (lock.newCondition()); |
626 |
+ |
try { |
627 |
+ |
lock.hasWaiters(c); |
628 |
+ |
shouldThrow(); |
629 |
+ |
} catch (IllegalMonitorStateException success) { |
630 |
+ |
} catch (Exception ex) { |
631 |
+ |
unexpectedException(); |
632 |
+ |
} |
633 |
+ |
} |
634 |
+ |
|
635 |
+ |
|
636 |
+ |
/** |
637 |
+ |
* getWaitQueueLength throws IAE if not owned |
638 |
+ |
*/ |
639 |
+ |
public void testGetWaitQueueLengthIAE() { |
640 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
641 |
+ |
final Condition c = (lock.newCondition()); |
642 |
+ |
final ReentrantLock lock2 = new ReentrantLock(); |
643 |
+ |
try { |
644 |
+ |
lock2.getWaitQueueLength(c); |
645 |
+ |
shouldThrow(); |
646 |
+ |
} catch (IllegalArgumentException success) { |
647 |
+ |
} catch (Exception ex) { |
648 |
+ |
unexpectedException(); |
649 |
+ |
} |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* getWaitQueueLength throws IMSE if not locked |
654 |
+ |
*/ |
655 |
+ |
public void testGetWaitQueueLengthIMSE() { |
656 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
657 |
+ |
final Condition c = (lock.newCondition()); |
658 |
+ |
try { |
659 |
+ |
lock.getWaitQueueLength(c); |
660 |
+ |
shouldThrow(); |
661 |
+ |
} catch (IllegalMonitorStateException success) { |
662 |
+ |
} catch (Exception ex) { |
663 |
+ |
unexpectedException(); |
664 |
+ |
} |
665 |
+ |
} |
666 |
+ |
|
667 |
+ |
|
668 |
+ |
/** |
669 |
+ |
* getWaitingThreads throws IAE if not owned |
670 |
+ |
*/ |
671 |
+ |
public void testGetWaitingThreadsIAE() { |
672 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
673 |
+ |
final Condition c = (lock.newCondition()); |
674 |
+ |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
675 |
+ |
try { |
676 |
+ |
lock2.getWaitingThreads(c); |
677 |
+ |
shouldThrow(); |
678 |
+ |
} catch (IllegalArgumentException success) { |
679 |
+ |
} catch (Exception ex) { |
680 |
+ |
unexpectedException(); |
681 |
+ |
} |
682 |
+ |
} |
683 |
+ |
|
684 |
+ |
/** |
685 |
+ |
* getWaitingThreads throws IMSE if not locked |
686 |
+ |
*/ |
687 |
+ |
public void testGetWaitingThreadsIMSE() { |
688 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
689 |
+ |
final Condition c = (lock.newCondition()); |
690 |
+ |
try { |
691 |
+ |
lock.getWaitingThreads(c); |
692 |
+ |
shouldThrow(); |
693 |
+ |
} catch (IllegalMonitorStateException success) { |
694 |
+ |
} catch (Exception ex) { |
695 |
+ |
unexpectedException(); |
696 |
+ |
} |
697 |
+ |
} |
698 |
+ |
|
699 |
+ |
|
700 |
+ |
|
701 |
+ |
/** |
702 |
|
* hasWaiters returns true when a thread is waiting, else false |
703 |
|
*/ |
704 |
|
public void testHasWaiters() { |
705 |
|
final ReentrantLock lock = new ReentrantLock(); |
706 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
706 |
> |
final Condition c = lock.newCondition(); |
707 |
|
Thread t = new Thread(new Runnable() { |
708 |
|
public void run() { |
709 |
|
try { |
710 |
|
lock.lock(); |
711 |
< |
threadAssertFalse(c.hasWaiters()); |
712 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
711 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
712 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
713 |
|
c.await(); |
714 |
|
lock.unlock(); |
715 |
|
} |
723 |
|
t.start(); |
724 |
|
Thread.sleep(SHORT_DELAY_MS); |
725 |
|
lock.lock(); |
726 |
< |
assertTrue(c.hasWaiters()); |
727 |
< |
assertEquals(1, c.getWaitQueueLength()); |
726 |
> |
assertTrue(lock.hasWaiters(c)); |
727 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
728 |
|
c.signal(); |
729 |
|
lock.unlock(); |
730 |
|
Thread.sleep(SHORT_DELAY_MS); |
731 |
|
lock.lock(); |
732 |
< |
assertFalse(c.hasWaiters()); |
733 |
< |
assertEquals(0, c.getWaitQueueLength()); |
732 |
> |
assertFalse(lock.hasWaiters(c)); |
733 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
734 |
|
lock.unlock(); |
735 |
|
t.join(SHORT_DELAY_MS); |
736 |
|
assertFalse(t.isAlive()); |
745 |
|
*/ |
746 |
|
public void testGetWaitQueueLength() { |
747 |
|
final ReentrantLock lock = new ReentrantLock(); |
748 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
748 |
> |
final Condition c = lock.newCondition(); |
749 |
|
Thread t1 = new Thread(new Runnable() { |
750 |
|
public void run() { |
751 |
|
try { |
752 |
|
lock.lock(); |
753 |
< |
threadAssertFalse(c.hasWaiters()); |
754 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
753 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
754 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
755 |
|
c.await(); |
756 |
|
lock.unlock(); |
757 |
|
} |
765 |
|
public void run() { |
766 |
|
try { |
767 |
|
lock.lock(); |
768 |
< |
threadAssertTrue(c.hasWaiters()); |
769 |
< |
threadAssertEquals(1, c.getWaitQueueLength()); |
768 |
> |
threadAssertTrue(lock.hasWaiters(c)); |
769 |
> |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
770 |
|
c.await(); |
771 |
|
lock.unlock(); |
772 |
|
} |
782 |
|
t2.start(); |
783 |
|
Thread.sleep(SHORT_DELAY_MS); |
784 |
|
lock.lock(); |
785 |
< |
assertTrue(c.hasWaiters()); |
786 |
< |
assertEquals(2, c.getWaitQueueLength()); |
785 |
> |
assertTrue(lock.hasWaiters(c)); |
786 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
787 |
|
c.signalAll(); |
788 |
|
lock.unlock(); |
789 |
|
Thread.sleep(SHORT_DELAY_MS); |
790 |
|
lock.lock(); |
791 |
< |
assertFalse(c.hasWaiters()); |
792 |
< |
assertEquals(0, c.getWaitQueueLength()); |
791 |
> |
assertFalse(lock.hasWaiters(c)); |
792 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
793 |
|
lock.unlock(); |
794 |
|
t1.join(SHORT_DELAY_MS); |
795 |
|
t2.join(SHORT_DELAY_MS); |
806 |
|
*/ |
807 |
|
public void testGetWaitingThreads() { |
808 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
809 |
< |
final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition(); |
809 |
> |
final Condition c = lock.newCondition(); |
810 |
|
Thread t1 = new Thread(new Runnable() { |
811 |
|
public void run() { |
812 |
|
try { |
813 |
|
lock.lock(); |
814 |
< |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
814 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
815 |
|
c.await(); |
816 |
|
lock.unlock(); |
817 |
|
} |
825 |
|
public void run() { |
826 |
|
try { |
827 |
|
lock.lock(); |
828 |
< |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
828 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
829 |
|
c.await(); |
830 |
|
lock.unlock(); |
831 |
|
} |
837 |
|
|
838 |
|
try { |
839 |
|
lock.lock(); |
840 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
840 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
841 |
|
lock.unlock(); |
842 |
|
t1.start(); |
843 |
|
Thread.sleep(SHORT_DELAY_MS); |
844 |
|
t2.start(); |
845 |
|
Thread.sleep(SHORT_DELAY_MS); |
846 |
|
lock.lock(); |
847 |
< |
assertTrue(c.hasWaiters()); |
848 |
< |
assertTrue(c.getWaitingThreads().contains(t1)); |
849 |
< |
assertTrue(c.getWaitingThreads().contains(t2)); |
847 |
> |
assertTrue(lock.hasWaiters(c)); |
848 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
849 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
850 |
|
c.signalAll(); |
851 |
|
lock.unlock(); |
852 |
|
Thread.sleep(SHORT_DELAY_MS); |
853 |
|
lock.lock(); |
854 |
< |
assertFalse(c.hasWaiters()); |
855 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
854 |
> |
assertFalse(lock.hasWaiters(c)); |
855 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
856 |
|
lock.unlock(); |
857 |
|
t1.join(SHORT_DELAY_MS); |
858 |
|
t2.join(SHORT_DELAY_MS); |
864 |
|
} |
865 |
|
} |
866 |
|
|
867 |
+ |
/** A helper class for uninterruptible wait tests */ |
868 |
+ |
class UninterruptableThread extends Thread { |
869 |
+ |
private ReentrantLock lock; |
870 |
+ |
private Condition c; |
871 |
+ |
|
872 |
+ |
public volatile boolean canAwake = false; |
873 |
+ |
public volatile boolean interrupted = false; |
874 |
+ |
public volatile boolean lockStarted = false; |
875 |
+ |
|
876 |
+ |
public UninterruptableThread(ReentrantLock lock, Condition c) { |
877 |
+ |
this.lock = lock; |
878 |
+ |
this.c = c; |
879 |
+ |
} |
880 |
+ |
|
881 |
+ |
public synchronized void run() { |
882 |
+ |
lock.lock(); |
883 |
+ |
lockStarted = true; |
884 |
+ |
|
885 |
+ |
while (!canAwake) { |
886 |
+ |
c.awaitUninterruptibly(); |
887 |
+ |
} |
888 |
+ |
|
889 |
+ |
interrupted = isInterrupted(); |
890 |
+ |
lock.unlock(); |
891 |
+ |
} |
892 |
+ |
} |
893 |
+ |
|
894 |
|
/** |
895 |
|
* awaitUninterruptibly doesn't abort on interrupt |
896 |
|
*/ |
897 |
|
public void testAwaitUninterruptibly() { |
898 |
< |
final ReentrantLock lock = new ReentrantLock(); |
898 |
> |
final ReentrantLock lock = new ReentrantLock(); |
899 |
|
final Condition c = lock.newCondition(); |
900 |
< |
Thread t = new Thread(new Runnable() { |
632 |
< |
public void run() { |
633 |
< |
lock.lock(); |
634 |
< |
c.awaitUninterruptibly(); |
635 |
< |
lock.unlock(); |
636 |
< |
} |
637 |
< |
}); |
900 |
> |
UninterruptableThread thread = new UninterruptableThread(lock, c); |
901 |
|
|
902 |
|
try { |
903 |
< |
t.start(); |
904 |
< |
Thread.sleep(SHORT_DELAY_MS); |
905 |
< |
t.interrupt(); |
903 |
> |
thread.start(); |
904 |
> |
|
905 |
> |
while (!thread.lockStarted) { |
906 |
> |
Thread.sleep(100); |
907 |
> |
} |
908 |
> |
|
909 |
|
lock.lock(); |
910 |
< |
c.signal(); |
911 |
< |
lock.unlock(); |
912 |
< |
assert(t.isInterrupted()); |
913 |
< |
t.join(SHORT_DELAY_MS); |
914 |
< |
assertFalse(t.isAlive()); |
915 |
< |
} |
916 |
< |
catch (Exception ex) { |
910 |
> |
try { |
911 |
> |
thread.interrupt(); |
912 |
> |
thread.canAwake = true; |
913 |
> |
c.signal(); |
914 |
> |
} finally { |
915 |
> |
lock.unlock(); |
916 |
> |
} |
917 |
> |
|
918 |
> |
thread.join(); |
919 |
> |
assertTrue(thread.interrupted); |
920 |
> |
assertFalse(thread.isAlive()); |
921 |
> |
} catch (Exception ex) { |
922 |
|
unexpectedException(); |
923 |
|
} |
924 |
|
} |
964 |
|
public void run() { |
965 |
|
try { |
966 |
|
lock.lock(); |
967 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
967 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
968 |
|
lock.unlock(); |
969 |
|
threadShouldThrow(); |
970 |
|
} |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1070 |
+ |
* await after multiple reentrant locking preserves lock count |
1071 |
+ |
*/ |
1072 |
+ |
public void testAwaitLockCount() { |
1073 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
1074 |
+ |
final Condition c = lock.newCondition(); |
1075 |
+ |
Thread t1 = new Thread(new Runnable() { |
1076 |
+ |
public void run() { |
1077 |
+ |
try { |
1078 |
+ |
lock.lock(); |
1079 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1080 |
+ |
c.await(); |
1081 |
+ |
threadAssertEquals(1, lock.getHoldCount()); |
1082 |
+ |
lock.unlock(); |
1083 |
+ |
} |
1084 |
+ |
catch(InterruptedException e) { |
1085 |
+ |
threadUnexpectedException(); |
1086 |
+ |
} |
1087 |
+ |
} |
1088 |
+ |
}); |
1089 |
+ |
|
1090 |
+ |
Thread t2 = new Thread(new Runnable() { |
1091 |
+ |
public void run() { |
1092 |
+ |
try { |
1093 |
+ |
lock.lock(); |
1094 |
+ |
lock.lock(); |
1095 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1096 |
+ |
c.await(); |
1097 |
+ |
threadAssertEquals(2, lock.getHoldCount()); |
1098 |
+ |
lock.unlock(); |
1099 |
+ |
lock.unlock(); |
1100 |
+ |
} |
1101 |
+ |
catch(InterruptedException e) { |
1102 |
+ |
threadUnexpectedException(); |
1103 |
+ |
} |
1104 |
+ |
} |
1105 |
+ |
}); |
1106 |
+ |
|
1107 |
+ |
try { |
1108 |
+ |
t1.start(); |
1109 |
+ |
t2.start(); |
1110 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1111 |
+ |
lock.lock(); |
1112 |
+ |
c.signalAll(); |
1113 |
+ |
lock.unlock(); |
1114 |
+ |
t1.join(SHORT_DELAY_MS); |
1115 |
+ |
t2.join(SHORT_DELAY_MS); |
1116 |
+ |
assertFalse(t1.isAlive()); |
1117 |
+ |
assertFalse(t2.isAlive()); |
1118 |
+ |
} |
1119 |
+ |
catch (Exception ex) { |
1120 |
+ |
unexpectedException(); |
1121 |
+ |
} |
1122 |
+ |
} |
1123 |
+ |
|
1124 |
+ |
/** |
1125 |
|
* A serialized lock deserializes as unlocked |
1126 |
|
*/ |
1127 |
|
public void testSerialization() { |
1146 |
|
} |
1147 |
|
} |
1148 |
|
|
1149 |
+ |
/** |
1150 |
+ |
* toString indicates current lock state |
1151 |
+ |
*/ |
1152 |
+ |
public void testToString() { |
1153 |
+ |
ReentrantLock lock = new ReentrantLock(); |
1154 |
+ |
String us = lock.toString(); |
1155 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1156 |
+ |
lock.lock(); |
1157 |
+ |
String ls = lock.toString(); |
1158 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1159 |
+ |
} |
1160 |
+ |
|
1161 |
|
} |