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 |
|
*/ |
15 |
|
|
16 |
|
public class ReentrantLockTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(ReentrantLockTest.class); |
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(); |
134 |
> |
awaitTermination(t1); |
135 |
> |
awaitTermination(t2); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
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(); |
159 |
> |
awaitTermination(t1); |
160 |
> |
awaitTermination(t2); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
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(); |
184 |
> |
awaitTermination(t1); |
185 |
> |
awaitTermination(t2); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
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(); |
225 |
> |
awaitTermination(t1); |
226 |
> |
awaitTermination(t2); |
227 |
|
} |
228 |
|
|
229 |
|
|
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(); |
254 |
> |
awaitTermination(t1); |
255 |
> |
awaitTermination(t2); |
256 |
|
} |
257 |
|
|
258 |
|
|
262 |
|
public void testInterruptedException2() throws InterruptedException { |
263 |
|
final ReentrantLock lock = new ReentrantLock(); |
264 |
|
lock.lock(); |
265 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
265 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
266 |
|
public void realRun() throws InterruptedException { |
267 |
|
lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); |
268 |
|
}}); |
269 |
|
|
270 |
< |
t.start(); |
271 |
< |
Thread.sleep(SHORT_DELAY_MS); |
270 |
> |
delay(SHORT_DELAY_MS); |
271 |
|
t.interrupt(); |
272 |
< |
t.join(); |
272 |
> |
awaitTermination(t); |
273 |
|
} |
274 |
|
|
275 |
|
|
279 |
|
public void testTryLockWhenLocked() throws InterruptedException { |
280 |
|
final ReentrantLock lock = new ReentrantLock(); |
281 |
|
lock.lock(); |
282 |
< |
Thread t = new Thread(new CheckedRunnable() { |
282 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
|
public void realRun() { |
284 |
< |
threadAssertFalse(lock.tryLock()); |
284 |
> |
assertFalse(lock.tryLock()); |
285 |
|
}}); |
286 |
|
|
287 |
< |
t.start(); |
289 |
< |
t.join(); |
287 |
> |
awaitTermination(t); |
288 |
|
lock.unlock(); |
289 |
|
} |
290 |
|
|
294 |
|
public void testTryLock_Timeout() throws InterruptedException { |
295 |
|
final ReentrantLock lock = new ReentrantLock(); |
296 |
|
lock.lock(); |
297 |
< |
Thread t = new Thread(new CheckedRunnable() { |
297 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
|
public void realRun() throws InterruptedException { |
299 |
< |
threadAssertFalse(lock.tryLock(1, MILLISECONDS)); |
299 |
> |
assertFalse(lock.tryLock(1, MILLISECONDS)); |
300 |
|
}}); |
301 |
|
|
302 |
< |
t.start(); |
305 |
< |
t.join(); |
302 |
> |
awaitTermination(t); |
303 |
|
lock.unlock(); |
304 |
|
} |
305 |
|
|
328 |
|
assertTrue(lock.isLocked()); |
329 |
|
lock.unlock(); |
330 |
|
assertFalse(lock.isLocked()); |
331 |
< |
Thread t = new Thread(new CheckedRunnable() { |
331 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
332 |
|
public void realRun() throws InterruptedException { |
333 |
|
lock.lock(); |
334 |
< |
Thread.sleep(SMALL_DELAY_MS); |
334 |
> |
delay(SMALL_DELAY_MS); |
335 |
|
lock.unlock(); |
336 |
|
}}); |
337 |
|
|
338 |
< |
t.start(); |
342 |
< |
Thread.sleep(SHORT_DELAY_MS); |
338 |
> |
delay(SHORT_DELAY_MS); |
339 |
|
assertTrue(lock.isLocked()); |
340 |
< |
t.join(); |
340 |
> |
awaitTermination(t); |
341 |
|
assertFalse(lock.isLocked()); |
342 |
|
} |
343 |
|
|
348 |
|
public void testLockInterruptibly1() throws InterruptedException { |
349 |
|
final ReentrantLock lock = new ReentrantLock(); |
350 |
|
lock.lock(); |
351 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
352 |
< |
t.start(); |
357 |
< |
Thread.sleep(SHORT_DELAY_MS); |
351 |
> |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
352 |
> |
delay(SHORT_DELAY_MS); |
353 |
|
t.interrupt(); |
354 |
< |
Thread.sleep(SHORT_DELAY_MS); |
354 |
> |
delay(SHORT_DELAY_MS); |
355 |
|
lock.unlock(); |
356 |
< |
t.join(); |
356 |
> |
awaitTermination(t); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
362 |
|
public void testLockInterruptibly2() throws InterruptedException { |
363 |
|
final ReentrantLock lock = new ReentrantLock(); |
364 |
|
lock.lockInterruptibly(); |
365 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
366 |
< |
t.start(); |
372 |
< |
Thread.sleep(SHORT_DELAY_MS); |
365 |
> |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
366 |
> |
delay(SHORT_DELAY_MS); |
367 |
|
t.interrupt(); |
368 |
|
assertTrue(lock.isLocked()); |
369 |
|
assertTrue(lock.isHeldByCurrentThread()); |
370 |
< |
t.join(); |
370 |
> |
awaitTermination(t); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* timed await without a signal times out |
410 |
> |
* timed await without a signal times out |
411 |
|
*/ |
412 |
|
public void testAwait_Timeout() throws InterruptedException { |
413 |
|
final ReentrantLock lock = new ReentrantLock(); |
435 |
|
public void testAwait() throws InterruptedException { |
436 |
|
final ReentrantLock lock = new ReentrantLock(); |
437 |
|
final Condition c = lock.newCondition(); |
438 |
< |
Thread t = new Thread(new CheckedRunnable() { |
438 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
439 |
|
public void realRun() throws InterruptedException { |
440 |
|
lock.lock(); |
441 |
|
c.await(); |
442 |
|
lock.unlock(); |
443 |
|
}}); |
444 |
|
|
445 |
< |
t.start(); |
452 |
< |
Thread.sleep(SHORT_DELAY_MS); |
445 |
> |
delay(SHORT_DELAY_MS); |
446 |
|
lock.lock(); |
447 |
|
c.signal(); |
448 |
|
lock.unlock(); |
449 |
< |
t.join(SHORT_DELAY_MS); |
457 |
< |
assertFalse(t.isAlive()); |
449 |
> |
awaitTermination(t); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
568 |
|
public void testHasWaiters() throws InterruptedException { |
569 |
|
final ReentrantLock lock = new ReentrantLock(); |
570 |
|
final Condition c = lock.newCondition(); |
571 |
< |
Thread t = new Thread(new CheckedRunnable() { |
571 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
572 |
|
public void realRun() throws InterruptedException { |
573 |
|
lock.lock(); |
574 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
575 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
574 |
> |
assertFalse(lock.hasWaiters(c)); |
575 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
576 |
|
c.await(); |
577 |
|
lock.unlock(); |
578 |
|
}}); |
579 |
|
|
580 |
< |
t.start(); |
589 |
< |
Thread.sleep(SHORT_DELAY_MS); |
580 |
> |
delay(SHORT_DELAY_MS); |
581 |
|
lock.lock(); |
582 |
|
assertTrue(lock.hasWaiters(c)); |
583 |
|
assertEquals(1, lock.getWaitQueueLength(c)); |
584 |
|
c.signal(); |
585 |
|
lock.unlock(); |
586 |
< |
Thread.sleep(SHORT_DELAY_MS); |
586 |
> |
delay(SHORT_DELAY_MS); |
587 |
|
lock.lock(); |
588 |
|
assertFalse(lock.hasWaiters(c)); |
589 |
|
assertEquals(0, lock.getWaitQueueLength(c)); |
590 |
|
lock.unlock(); |
591 |
< |
t.join(SHORT_DELAY_MS); |
601 |
< |
assertFalse(t.isAlive()); |
591 |
> |
awaitTermination(t); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
597 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
598 |
|
final ReentrantLock lock = new ReentrantLock(); |
599 |
|
final Condition c = lock.newCondition(); |
600 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
600 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
601 |
|
public void realRun() throws InterruptedException { |
602 |
|
lock.lock(); |
603 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
604 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
603 |
> |
assertFalse(lock.hasWaiters(c)); |
604 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
605 |
|
c.await(); |
606 |
|
lock.unlock(); |
607 |
|
}}); |
608 |
|
|
609 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
609 |
> |
delay(SHORT_DELAY_MS); |
610 |
> |
|
611 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
612 |
|
public void realRun() throws InterruptedException { |
613 |
|
lock.lock(); |
614 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
615 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
614 |
> |
assertTrue(lock.hasWaiters(c)); |
615 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
616 |
|
c.await(); |
617 |
|
lock.unlock(); |
618 |
|
}}); |
619 |
|
|
620 |
< |
t1.start(); |
629 |
< |
Thread.sleep(SHORT_DELAY_MS); |
630 |
< |
t2.start(); |
631 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
> |
delay(SHORT_DELAY_MS); |
621 |
|
lock.lock(); |
622 |
|
assertTrue(lock.hasWaiters(c)); |
623 |
|
assertEquals(2, lock.getWaitQueueLength(c)); |
624 |
|
c.signalAll(); |
625 |
|
lock.unlock(); |
626 |
< |
Thread.sleep(SHORT_DELAY_MS); |
626 |
> |
delay(SHORT_DELAY_MS); |
627 |
|
lock.lock(); |
628 |
|
assertFalse(lock.hasWaiters(c)); |
629 |
|
assertEquals(0, lock.getWaitQueueLength(c)); |
630 |
|
lock.unlock(); |
631 |
< |
t1.join(SHORT_DELAY_MS); |
632 |
< |
t2.join(SHORT_DELAY_MS); |
644 |
< |
assertFalse(t1.isAlive()); |
645 |
< |
assertFalse(t2.isAlive()); |
631 |
> |
awaitTermination(t1); |
632 |
> |
awaitTermination(t2); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
641 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
642 |
|
public void realRun() throws InterruptedException { |
643 |
|
lock.lock(); |
644 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
644 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
645 |
|
c.await(); |
646 |
|
lock.unlock(); |
647 |
|
}}); |
649 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
650 |
|
public void realRun() throws InterruptedException { |
651 |
|
lock.lock(); |
652 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
652 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
653 |
|
c.await(); |
654 |
|
lock.unlock(); |
655 |
|
}}); |
658 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
659 |
|
lock.unlock(); |
660 |
|
t1.start(); |
661 |
< |
Thread.sleep(SHORT_DELAY_MS); |
661 |
> |
delay(SHORT_DELAY_MS); |
662 |
|
t2.start(); |
663 |
< |
Thread.sleep(SHORT_DELAY_MS); |
663 |
> |
delay(SHORT_DELAY_MS); |
664 |
|
lock.lock(); |
665 |
|
assertTrue(lock.hasWaiters(c)); |
666 |
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
667 |
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
668 |
|
c.signalAll(); |
669 |
|
lock.unlock(); |
670 |
< |
Thread.sleep(SHORT_DELAY_MS); |
670 |
> |
delay(SHORT_DELAY_MS); |
671 |
|
lock.lock(); |
672 |
|
assertFalse(lock.hasWaiters(c)); |
673 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
674 |
|
lock.unlock(); |
675 |
< |
t1.join(SHORT_DELAY_MS); |
676 |
< |
t2.join(SHORT_DELAY_MS); |
690 |
< |
assertFalse(t1.isAlive()); |
691 |
< |
assertFalse(t2.isAlive()); |
675 |
> |
awaitTermination(t1); |
676 |
> |
awaitTermination(t2); |
677 |
|
} |
678 |
|
|
679 |
|
/** A helper class for uninterruptible wait tests */ |
714 |
|
thread.start(); |
715 |
|
|
716 |
|
while (!thread.lockStarted) { |
717 |
< |
Thread.sleep(100); |
717 |
> |
delay(100); |
718 |
|
} |
719 |
|
|
720 |
|
lock.lock(); |
737 |
|
public void testAwait_Interrupt() throws InterruptedException { |
738 |
|
final ReentrantLock lock = new ReentrantLock(); |
739 |
|
final Condition c = lock.newCondition(); |
740 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
740 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
741 |
|
public void realRun() throws InterruptedException { |
742 |
|
lock.lock(); |
743 |
|
c.await(); |
744 |
|
}}); |
745 |
|
|
746 |
< |
t.start(); |
762 |
< |
Thread.sleep(SHORT_DELAY_MS); |
746 |
> |
delay(SHORT_DELAY_MS); |
747 |
|
t.interrupt(); |
748 |
< |
t.join(SHORT_DELAY_MS); |
765 |
< |
assertFalse(t.isAlive()); |
748 |
> |
awaitTermination(t); |
749 |
|
} |
750 |
|
|
751 |
|
/** |
754 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
755 |
|
final ReentrantLock lock = new ReentrantLock(); |
756 |
|
final Condition c = lock.newCondition(); |
757 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
757 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
758 |
|
public void realRun() throws InterruptedException { |
759 |
|
lock.lock(); |
760 |
|
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
761 |
|
}}); |
762 |
|
|
763 |
< |
t.start(); |
781 |
< |
Thread.sleep(SHORT_DELAY_MS); |
763 |
> |
delay(SHORT_DELAY_MS); |
764 |
|
t.interrupt(); |
765 |
< |
t.join(SHORT_DELAY_MS); |
784 |
< |
assertFalse(t.isAlive()); |
765 |
> |
awaitTermination(t); |
766 |
|
} |
767 |
|
|
768 |
|
/** |
771 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
772 |
|
final ReentrantLock lock = new ReentrantLock(); |
773 |
|
final Condition c = lock.newCondition(); |
774 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
774 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
775 |
|
public void realRun() throws InterruptedException { |
776 |
|
lock.lock(); |
777 |
|
java.util.Date d = new java.util.Date(); |
778 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
779 |
|
}}); |
780 |
|
|
781 |
< |
t.start(); |
801 |
< |
Thread.sleep(SHORT_DELAY_MS); |
781 |
> |
delay(SHORT_DELAY_MS); |
782 |
|
t.interrupt(); |
783 |
< |
t.join(SHORT_DELAY_MS); |
804 |
< |
assertFalse(t.isAlive()); |
783 |
> |
awaitTermination(t); |
784 |
|
} |
785 |
|
|
786 |
|
/** |
789 |
|
public void testSignalAll() throws InterruptedException { |
790 |
|
final ReentrantLock lock = new ReentrantLock(); |
791 |
|
final Condition c = lock.newCondition(); |
792 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
792 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
793 |
|
public void realRun() throws InterruptedException { |
794 |
|
lock.lock(); |
795 |
|
c.await(); |
796 |
|
lock.unlock(); |
797 |
|
}}); |
798 |
|
|
799 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
799 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
800 |
|
public void realRun() throws InterruptedException { |
801 |
|
lock.lock(); |
802 |
|
c.await(); |
803 |
|
lock.unlock(); |
804 |
|
}}); |
805 |
|
|
806 |
< |
t1.start(); |
828 |
< |
t2.start(); |
829 |
< |
Thread.sleep(SHORT_DELAY_MS); |
806 |
> |
delay(SHORT_DELAY_MS); |
807 |
|
lock.lock(); |
808 |
|
c.signalAll(); |
809 |
|
lock.unlock(); |
810 |
< |
t1.join(SHORT_DELAY_MS); |
811 |
< |
t2.join(SHORT_DELAY_MS); |
835 |
< |
assertFalse(t1.isAlive()); |
836 |
< |
assertFalse(t2.isAlive()); |
810 |
> |
awaitTermination(t1); |
811 |
> |
awaitTermination(t2); |
812 |
|
} |
813 |
|
|
814 |
|
/** |
817 |
|
public void testAwaitLockCount() throws InterruptedException { |
818 |
|
final ReentrantLock lock = new ReentrantLock(); |
819 |
|
final Condition c = lock.newCondition(); |
820 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
820 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
821 |
|
public void realRun() throws InterruptedException { |
822 |
|
lock.lock(); |
823 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
823 |
> |
assertEquals(1, lock.getHoldCount()); |
824 |
|
c.await(); |
825 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
825 |
> |
assertEquals(1, lock.getHoldCount()); |
826 |
|
lock.unlock(); |
827 |
|
}}); |
828 |
|
|
829 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
829 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
830 |
|
public void realRun() throws InterruptedException { |
831 |
|
lock.lock(); |
832 |
|
lock.lock(); |
833 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
833 |
> |
assertEquals(2, lock.getHoldCount()); |
834 |
|
c.await(); |
835 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
835 |
> |
assertEquals(2, lock.getHoldCount()); |
836 |
|
lock.unlock(); |
837 |
|
lock.unlock(); |
838 |
|
}}); |
839 |
|
|
840 |
< |
t1.start(); |
866 |
< |
t2.start(); |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
840 |
> |
delay(SHORT_DELAY_MS); |
841 |
|
lock.lock(); |
842 |
|
c.signalAll(); |
843 |
|
lock.unlock(); |
844 |
< |
t1.join(SHORT_DELAY_MS); |
845 |
< |
t2.join(SHORT_DELAY_MS); |
873 |
< |
assertFalse(t1.isAlive()); |
874 |
< |
assertFalse(t2.isAlive()); |
844 |
> |
awaitTermination(t1); |
845 |
> |
awaitTermination(t2); |
846 |
|
} |
847 |
|
|
848 |
|
/** |
858 |
|
new ObjectOutputStream(new BufferedOutputStream(bout)); |
859 |
|
out.writeObject(l); |
860 |
|
out.close(); |
861 |
< |
|
861 |
> |
|
862 |
|
ByteArrayInputStream bin = |
863 |
|
new ByteArrayInputStream(bout.toByteArray()); |
864 |
|
ObjectInputStream in = |