1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
120 |
|
assertFalse(lock.hasQueuedThreads()); |
121 |
|
lock.lock(); |
122 |
|
t1.start(); |
123 |
< |
Thread.sleep(SHORT_DELAY_MS); |
123 |
> |
delay(SHORT_DELAY_MS); |
124 |
|
assertTrue(lock.hasQueuedThreads()); |
125 |
|
t2.start(); |
126 |
< |
Thread.sleep(SHORT_DELAY_MS); |
126 |
> |
delay(SHORT_DELAY_MS); |
127 |
|
assertTrue(lock.hasQueuedThreads()); |
128 |
|
t1.interrupt(); |
129 |
< |
Thread.sleep(SHORT_DELAY_MS); |
129 |
> |
delay(SHORT_DELAY_MS); |
130 |
|
assertTrue(lock.hasQueuedThreads()); |
131 |
|
lock.unlock(); |
132 |
< |
Thread.sleep(SHORT_DELAY_MS); |
132 |
> |
delay(SHORT_DELAY_MS); |
133 |
|
assertFalse(lock.hasQueuedThreads()); |
134 |
|
t1.join(); |
135 |
|
t2.join(); |
145 |
|
assertEquals(0, lock.getQueueLength()); |
146 |
|
lock.lock(); |
147 |
|
t1.start(); |
148 |
< |
Thread.sleep(SHORT_DELAY_MS); |
148 |
> |
delay(SHORT_DELAY_MS); |
149 |
|
assertEquals(1, lock.getQueueLength()); |
150 |
|
t2.start(); |
151 |
< |
Thread.sleep(SHORT_DELAY_MS); |
151 |
> |
delay(SHORT_DELAY_MS); |
152 |
|
assertEquals(2, lock.getQueueLength()); |
153 |
|
t1.interrupt(); |
154 |
< |
Thread.sleep(SHORT_DELAY_MS); |
154 |
> |
delay(SHORT_DELAY_MS); |
155 |
|
assertEquals(1, lock.getQueueLength()); |
156 |
|
lock.unlock(); |
157 |
< |
Thread.sleep(SHORT_DELAY_MS); |
157 |
> |
delay(SHORT_DELAY_MS); |
158 |
|
assertEquals(0, lock.getQueueLength()); |
159 |
|
t1.join(); |
160 |
|
t2.join(); |
170 |
|
assertEquals(0, lock.getQueueLength()); |
171 |
|
lock.lock(); |
172 |
|
t1.start(); |
173 |
< |
Thread.sleep(SHORT_DELAY_MS); |
173 |
> |
delay(SHORT_DELAY_MS); |
174 |
|
assertEquals(1, lock.getQueueLength()); |
175 |
|
t2.start(); |
176 |
< |
Thread.sleep(SHORT_DELAY_MS); |
176 |
> |
delay(SHORT_DELAY_MS); |
177 |
|
assertEquals(2, lock.getQueueLength()); |
178 |
|
t1.interrupt(); |
179 |
< |
Thread.sleep(SHORT_DELAY_MS); |
179 |
> |
delay(SHORT_DELAY_MS); |
180 |
|
assertEquals(1, lock.getQueueLength()); |
181 |
|
lock.unlock(); |
182 |
< |
Thread.sleep(SHORT_DELAY_MS); |
182 |
> |
delay(SHORT_DELAY_MS); |
183 |
|
assertEquals(0, lock.getQueueLength()); |
184 |
|
t1.join(); |
185 |
|
t2.join(); |
207 |
|
assertFalse(sync.hasQueuedThread(t2)); |
208 |
|
sync.lock(); |
209 |
|
t1.start(); |
210 |
< |
Thread.sleep(SHORT_DELAY_MS); |
210 |
> |
delay(SHORT_DELAY_MS); |
211 |
|
assertTrue(sync.hasQueuedThread(t1)); |
212 |
|
t2.start(); |
213 |
< |
Thread.sleep(SHORT_DELAY_MS); |
213 |
> |
delay(SHORT_DELAY_MS); |
214 |
|
assertTrue(sync.hasQueuedThread(t1)); |
215 |
|
assertTrue(sync.hasQueuedThread(t2)); |
216 |
|
t1.interrupt(); |
217 |
< |
Thread.sleep(SHORT_DELAY_MS); |
217 |
> |
delay(SHORT_DELAY_MS); |
218 |
|
assertFalse(sync.hasQueuedThread(t1)); |
219 |
|
assertTrue(sync.hasQueuedThread(t2)); |
220 |
|
sync.unlock(); |
221 |
< |
Thread.sleep(SHORT_DELAY_MS); |
221 |
> |
delay(SHORT_DELAY_MS); |
222 |
|
assertFalse(sync.hasQueuedThread(t1)); |
223 |
< |
Thread.sleep(SHORT_DELAY_MS); |
223 |
> |
delay(SHORT_DELAY_MS); |
224 |
|
assertFalse(sync.hasQueuedThread(t2)); |
225 |
|
t1.join(); |
226 |
|
t2.join(); |
238 |
|
lock.lock(); |
239 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
240 |
|
t1.start(); |
241 |
< |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
delay(SHORT_DELAY_MS); |
242 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
243 |
|
t2.start(); |
244 |
< |
Thread.sleep(SHORT_DELAY_MS); |
244 |
> |
delay(SHORT_DELAY_MS); |
245 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
246 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
247 |
|
t1.interrupt(); |
248 |
< |
Thread.sleep(SHORT_DELAY_MS); |
248 |
> |
delay(SHORT_DELAY_MS); |
249 |
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
250 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
251 |
|
lock.unlock(); |
252 |
< |
Thread.sleep(SHORT_DELAY_MS); |
252 |
> |
delay(SHORT_DELAY_MS); |
253 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
254 |
|
t1.join(); |
255 |
|
t2.join(); |
268 |
|
}}); |
269 |
|
|
270 |
|
t.start(); |
271 |
< |
Thread.sleep(SHORT_DELAY_MS); |
271 |
> |
delay(SHORT_DELAY_MS); |
272 |
|
t.interrupt(); |
273 |
|
t.join(); |
274 |
|
} |
282 |
|
lock.lock(); |
283 |
|
Thread t = new Thread(new CheckedRunnable() { |
284 |
|
public void realRun() { |
285 |
< |
threadAssertFalse(lock.tryLock()); |
285 |
> |
assertFalse(lock.tryLock()); |
286 |
|
}}); |
287 |
|
|
288 |
|
t.start(); |
298 |
|
lock.lock(); |
299 |
|
Thread t = new Thread(new CheckedRunnable() { |
300 |
|
public void realRun() throws InterruptedException { |
301 |
< |
threadAssertFalse(lock.tryLock(1, MILLISECONDS)); |
301 |
> |
assertFalse(lock.tryLock(1, MILLISECONDS)); |
302 |
|
}}); |
303 |
|
|
304 |
|
t.start(); |
334 |
|
Thread t = new Thread(new CheckedRunnable() { |
335 |
|
public void realRun() throws InterruptedException { |
336 |
|
lock.lock(); |
337 |
< |
Thread.sleep(SMALL_DELAY_MS); |
337 |
> |
delay(SMALL_DELAY_MS); |
338 |
|
lock.unlock(); |
339 |
|
}}); |
340 |
|
|
341 |
|
t.start(); |
342 |
< |
Thread.sleep(SHORT_DELAY_MS); |
342 |
> |
delay(SHORT_DELAY_MS); |
343 |
|
assertTrue(lock.isLocked()); |
344 |
|
t.join(); |
345 |
|
assertFalse(lock.isLocked()); |
354 |
|
lock.lock(); |
355 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
356 |
|
t.start(); |
357 |
< |
Thread.sleep(SHORT_DELAY_MS); |
357 |
> |
delay(SHORT_DELAY_MS); |
358 |
|
t.interrupt(); |
359 |
< |
Thread.sleep(SHORT_DELAY_MS); |
359 |
> |
delay(SHORT_DELAY_MS); |
360 |
|
lock.unlock(); |
361 |
|
t.join(); |
362 |
|
} |
369 |
|
lock.lockInterruptibly(); |
370 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
371 |
|
t.start(); |
372 |
< |
Thread.sleep(SHORT_DELAY_MS); |
372 |
> |
delay(SHORT_DELAY_MS); |
373 |
|
t.interrupt(); |
374 |
|
assertTrue(lock.isLocked()); |
375 |
|
assertTrue(lock.isHeldByCurrentThread()); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* timed await without a signal times out |
416 |
> |
* timed await without a signal times out |
417 |
|
*/ |
418 |
|
public void testAwait_Timeout() throws InterruptedException { |
419 |
|
final ReentrantLock lock = new ReentrantLock(); |
449 |
|
}}); |
450 |
|
|
451 |
|
t.start(); |
452 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
> |
delay(SHORT_DELAY_MS); |
453 |
|
lock.lock(); |
454 |
|
c.signal(); |
455 |
|
lock.unlock(); |
579 |
|
Thread t = new Thread(new CheckedRunnable() { |
580 |
|
public void realRun() throws InterruptedException { |
581 |
|
lock.lock(); |
582 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
583 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
582 |
> |
assertFalse(lock.hasWaiters(c)); |
583 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
584 |
|
c.await(); |
585 |
|
lock.unlock(); |
586 |
|
}}); |
587 |
|
|
588 |
|
t.start(); |
589 |
< |
Thread.sleep(SHORT_DELAY_MS); |
589 |
> |
delay(SHORT_DELAY_MS); |
590 |
|
lock.lock(); |
591 |
|
assertTrue(lock.hasWaiters(c)); |
592 |
|
assertEquals(1, lock.getWaitQueueLength(c)); |
593 |
|
c.signal(); |
594 |
|
lock.unlock(); |
595 |
< |
Thread.sleep(SHORT_DELAY_MS); |
595 |
> |
delay(SHORT_DELAY_MS); |
596 |
|
lock.lock(); |
597 |
|
assertFalse(lock.hasWaiters(c)); |
598 |
|
assertEquals(0, lock.getWaitQueueLength(c)); |
610 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
611 |
|
public void realRun() throws InterruptedException { |
612 |
|
lock.lock(); |
613 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
614 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
613 |
> |
assertFalse(lock.hasWaiters(c)); |
614 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
615 |
|
c.await(); |
616 |
|
lock.unlock(); |
617 |
|
}}); |
619 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
620 |
|
public void realRun() throws InterruptedException { |
621 |
|
lock.lock(); |
622 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
623 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
622 |
> |
assertTrue(lock.hasWaiters(c)); |
623 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
624 |
|
c.await(); |
625 |
|
lock.unlock(); |
626 |
|
}}); |
627 |
|
|
628 |
|
t1.start(); |
629 |
< |
Thread.sleep(SHORT_DELAY_MS); |
629 |
> |
delay(SHORT_DELAY_MS); |
630 |
|
t2.start(); |
631 |
< |
Thread.sleep(SHORT_DELAY_MS); |
631 |
> |
delay(SHORT_DELAY_MS); |
632 |
|
lock.lock(); |
633 |
|
assertTrue(lock.hasWaiters(c)); |
634 |
|
assertEquals(2, lock.getWaitQueueLength(c)); |
635 |
|
c.signalAll(); |
636 |
|
lock.unlock(); |
637 |
< |
Thread.sleep(SHORT_DELAY_MS); |
637 |
> |
delay(SHORT_DELAY_MS); |
638 |
|
lock.lock(); |
639 |
|
assertFalse(lock.hasWaiters(c)); |
640 |
|
assertEquals(0, lock.getWaitQueueLength(c)); |
654 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
655 |
|
public void realRun() throws InterruptedException { |
656 |
|
lock.lock(); |
657 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
657 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
658 |
|
c.await(); |
659 |
|
lock.unlock(); |
660 |
|
}}); |
662 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
663 |
|
public void realRun() throws InterruptedException { |
664 |
|
lock.lock(); |
665 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
665 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
666 |
|
c.await(); |
667 |
|
lock.unlock(); |
668 |
|
}}); |
671 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
672 |
|
lock.unlock(); |
673 |
|
t1.start(); |
674 |
< |
Thread.sleep(SHORT_DELAY_MS); |
674 |
> |
delay(SHORT_DELAY_MS); |
675 |
|
t2.start(); |
676 |
< |
Thread.sleep(SHORT_DELAY_MS); |
676 |
> |
delay(SHORT_DELAY_MS); |
677 |
|
lock.lock(); |
678 |
|
assertTrue(lock.hasWaiters(c)); |
679 |
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
680 |
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
681 |
|
c.signalAll(); |
682 |
|
lock.unlock(); |
683 |
< |
Thread.sleep(SHORT_DELAY_MS); |
683 |
> |
delay(SHORT_DELAY_MS); |
684 |
|
lock.lock(); |
685 |
|
assertFalse(lock.hasWaiters(c)); |
686 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
729 |
|
thread.start(); |
730 |
|
|
731 |
|
while (!thread.lockStarted) { |
732 |
< |
Thread.sleep(100); |
732 |
> |
delay(100); |
733 |
|
} |
734 |
|
|
735 |
|
lock.lock(); |
759 |
|
}}); |
760 |
|
|
761 |
|
t.start(); |
762 |
< |
Thread.sleep(SHORT_DELAY_MS); |
762 |
> |
delay(SHORT_DELAY_MS); |
763 |
|
t.interrupt(); |
764 |
|
t.join(SHORT_DELAY_MS); |
765 |
|
assertFalse(t.isAlive()); |
778 |
|
}}); |
779 |
|
|
780 |
|
t.start(); |
781 |
< |
Thread.sleep(SHORT_DELAY_MS); |
781 |
> |
delay(SHORT_DELAY_MS); |
782 |
|
t.interrupt(); |
783 |
|
t.join(SHORT_DELAY_MS); |
784 |
|
assertFalse(t.isAlive()); |
798 |
|
}}); |
799 |
|
|
800 |
|
t.start(); |
801 |
< |
Thread.sleep(SHORT_DELAY_MS); |
801 |
> |
delay(SHORT_DELAY_MS); |
802 |
|
t.interrupt(); |
803 |
|
t.join(SHORT_DELAY_MS); |
804 |
|
assertFalse(t.isAlive()); |
826 |
|
|
827 |
|
t1.start(); |
828 |
|
t2.start(); |
829 |
< |
Thread.sleep(SHORT_DELAY_MS); |
829 |
> |
delay(SHORT_DELAY_MS); |
830 |
|
lock.lock(); |
831 |
|
c.signalAll(); |
832 |
|
lock.unlock(); |
845 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
846 |
|
public void realRun() throws InterruptedException { |
847 |
|
lock.lock(); |
848 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
848 |
> |
assertEquals(1, lock.getHoldCount()); |
849 |
|
c.await(); |
850 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
850 |
> |
assertEquals(1, lock.getHoldCount()); |
851 |
|
lock.unlock(); |
852 |
|
}}); |
853 |
|
|
855 |
|
public void realRun() throws InterruptedException { |
856 |
|
lock.lock(); |
857 |
|
lock.lock(); |
858 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
858 |
> |
assertEquals(2, lock.getHoldCount()); |
859 |
|
c.await(); |
860 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
860 |
> |
assertEquals(2, lock.getHoldCount()); |
861 |
|
lock.unlock(); |
862 |
|
lock.unlock(); |
863 |
|
}}); |
864 |
|
|
865 |
|
t1.start(); |
866 |
|
t2.start(); |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
867 |
> |
delay(SHORT_DELAY_MS); |
868 |
|
lock.lock(); |
869 |
|
c.signalAll(); |
870 |
|
lock.unlock(); |