6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.locks.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.*; |
11 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.CyclicBarrier; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.ReentrantLock; |
18 |
> |
|
19 |
> |
import junit.framework.AssertionFailedError; |
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ReentrantLockTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(ReentrantLockTest.class); |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* A runnable calling lockInterruptibly |
32 |
> |
* A checked runnable calling lockInterruptibly |
33 |
|
*/ |
34 |
|
class InterruptibleLockRunnable extends CheckedRunnable { |
35 |
|
final ReentrantLock lock; |
36 |
< |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
36 |
> |
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; } |
37 |
|
public void realRun() throws InterruptedException { |
38 |
|
lock.lockInterruptibly(); |
39 |
|
} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* A runnable calling lockInterruptibly that expects to be |
43 |
> |
* A checked runnable calling lockInterruptibly that expects to be |
44 |
|
* interrupted |
45 |
|
*/ |
46 |
|
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
47 |
|
final ReentrantLock lock; |
48 |
< |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
48 |
> |
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; } |
49 |
|
public void realRun() throws InterruptedException { |
50 |
|
lock.lockInterruptibly(); |
51 |
|
} |
85 |
|
long startTime = System.nanoTime(); |
86 |
|
while (!lock.hasQueuedThread(t)) { |
87 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
88 |
< |
throw new AssertionError("timed out"); |
88 |
> |
throw new AssertionFailedError("timed out"); |
89 |
|
Thread.yield(); |
90 |
|
} |
91 |
|
assertTrue(t.isAlive()); |
92 |
< |
assertTrue(lock.getOwner() != t); |
92 |
> |
assertNotSame(t, lock.getOwner()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
143 |
|
lock.unlock(); |
144 |
|
} |
145 |
|
|
146 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
146 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
|
|
148 |
|
/** |
149 |
< |
* Awaits condition using the specified AwaitMethod. |
149 |
> |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
150 |
|
*/ |
151 |
|
void await(Condition c, AwaitMethod awaitMethod) |
152 |
|
throws InterruptedException { |
153 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
154 |
|
switch (awaitMethod) { |
155 |
|
case await: |
156 |
|
c.await(); |
157 |
|
break; |
158 |
+ |
case awaitTimed: |
159 |
+ |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
160 |
+ |
break; |
161 |
|
case awaitNanos: |
162 |
< |
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
163 |
< |
assertTrue(nanosRemaining > 0); |
162 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
163 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
164 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
165 |
> |
assertTrue(nanosRemaining <= timeoutNanos); |
166 |
|
break; |
167 |
|
case awaitUntil: |
168 |
< |
java.util.Date d = new java.util.Date(); |
156 |
< |
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
168 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
169 |
|
break; |
170 |
+ |
default: |
171 |
+ |
throw new AssertionError(); |
172 |
|
} |
173 |
|
} |
174 |
|
|
209 |
|
public void testUnlock_IMSE() { testUnlock_IMSE(false); } |
210 |
|
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } |
211 |
|
public void testUnlock_IMSE(boolean fair) { |
212 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
212 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
213 |
|
try { |
214 |
|
lock.unlock(); |
215 |
|
shouldThrow(); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* hasQueuedThread reports whether a thread is queued. |
299 |
> |
* hasQueuedThread reports whether a thread is queued |
300 |
|
*/ |
301 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
302 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
< |
* timed tryLock is interruptible. |
360 |
> |
* timed tryLock is interruptible |
361 |
|
*/ |
362 |
|
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
363 |
|
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
399 |
|
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } |
400 |
|
public void testTryLock_Timeout(boolean fair) { |
401 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
402 |
+ |
final long timeoutMillis = timeoutMillis(); |
403 |
|
lock.lock(); |
404 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
405 |
|
public void realRun() throws InterruptedException { |
406 |
|
long startTime = System.nanoTime(); |
392 |
– |
long timeoutMillis = 10; |
407 |
|
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
408 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
409 |
|
}}); |
418 |
|
public void testGetHoldCount() { testGetHoldCount(false); } |
419 |
|
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
420 |
|
public void testGetHoldCount(boolean fair) { |
421 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
421 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
422 |
|
for (int i = 1; i <= SIZE; i++) { |
423 |
|
lock.lock(); |
424 |
|
assertEquals(i, lock.getHoldCount()); |
425 |
|
} |
426 |
|
for (int i = SIZE; i > 0; i--) { |
427 |
|
lock.unlock(); |
428 |
< |
assertEquals(i-1, lock.getHoldCount()); |
428 |
> |
assertEquals(i - 1, lock.getHoldCount()); |
429 |
|
} |
430 |
|
} |
431 |
|
|
435 |
|
public void testIsLocked() { testIsLocked(false); } |
436 |
|
public void testIsLocked_fair() { testIsLocked(true); } |
437 |
|
public void testIsLocked(boolean fair) { |
438 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
439 |
|
try { |
425 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
440 |
|
assertFalse(lock.isLocked()); |
441 |
|
lock.lock(); |
442 |
|
assertTrue(lock.isLocked()); |
461 |
|
barrier.await(); |
462 |
|
awaitTermination(t); |
463 |
|
assertFalse(lock.isLocked()); |
464 |
< |
} catch (Exception e) { |
451 |
< |
threadUnexpectedException(e); |
452 |
< |
} |
464 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
465 |
|
} |
466 |
|
|
467 |
|
/** |
473 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
474 |
|
try { |
475 |
|
lock.lockInterruptibly(); |
476 |
< |
} catch (InterruptedException ie) { |
465 |
< |
threadUnexpectedException(ie); |
466 |
< |
} |
476 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
477 |
|
assertLockedByMoi(lock); |
478 |
|
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
479 |
|
waitForQueuedThread(lock, t); |
492 |
|
public void testAwait_IMSE(boolean fair) { |
493 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
494 |
|
final Condition c = lock.newCondition(); |
495 |
< |
long startTime = System.nanoTime(); |
496 |
< |
try { |
487 |
< |
try { |
488 |
< |
c.await(); |
489 |
< |
shouldThrow(); |
490 |
< |
} catch (IllegalMonitorStateException success) {} |
491 |
< |
try { |
492 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
493 |
< |
shouldThrow(); |
494 |
< |
} catch (IllegalMonitorStateException success) {} |
495 |
< |
try { |
496 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
497 |
< |
shouldThrow(); |
498 |
< |
} catch (IllegalMonitorStateException success) {} |
495 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
496 |
> |
long startTime = System.nanoTime(); |
497 |
|
try { |
498 |
< |
c.awaitUninterruptibly(); |
498 |
> |
await(c, awaitMethod); |
499 |
|
shouldThrow(); |
500 |
< |
} catch (IllegalMonitorStateException success) {} |
501 |
< |
} catch (InterruptedException ie) { |
502 |
< |
threadUnexpectedException(ie); |
500 |
> |
} catch (IllegalMonitorStateException success) { |
501 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
502 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
503 |
|
} |
506 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
523 |
|
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
524 |
|
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
525 |
|
public void testAwaitNanos_Timeout(boolean fair) { |
526 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
527 |
+ |
final Condition c = lock.newCondition(); |
528 |
+ |
final long timeoutMillis = timeoutMillis(); |
529 |
+ |
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
530 |
+ |
lock.lock(); |
531 |
+ |
final long startTime = System.nanoTime(); |
532 |
|
try { |
530 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
531 |
– |
final Condition c = lock.newCondition(); |
532 |
– |
lock.lock(); |
533 |
– |
long startTime = System.nanoTime(); |
534 |
– |
long timeoutMillis = 10; |
535 |
– |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
533 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
534 |
|
assertTrue(nanosRemaining <= 0); |
535 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
536 |
< |
lock.unlock(); |
537 |
< |
} catch (InterruptedException e) { |
541 |
< |
threadUnexpectedException(e); |
542 |
< |
} |
535 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
536 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
537 |
> |
lock.unlock(); |
538 |
|
} |
539 |
|
|
540 |
|
/** |
543 |
|
public void testAwait_Timeout() { testAwait_Timeout(false); } |
544 |
|
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
545 |
|
public void testAwait_Timeout(boolean fair) { |
546 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
547 |
+ |
final Condition c = lock.newCondition(); |
548 |
+ |
final long timeoutMillis = timeoutMillis(); |
549 |
+ |
lock.lock(); |
550 |
+ |
final long startTime = System.nanoTime(); |
551 |
|
try { |
552 |
– |
final ReentrantLock lock = new ReentrantLock(fair); |
553 |
– |
final Condition c = lock.newCondition(); |
554 |
– |
lock.lock(); |
555 |
– |
long startTime = System.nanoTime(); |
556 |
– |
long timeoutMillis = 10; |
552 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
553 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
554 |
< |
lock.unlock(); |
555 |
< |
} catch (InterruptedException e) { |
561 |
< |
threadUnexpectedException(e); |
562 |
< |
} |
553 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
554 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
555 |
> |
lock.unlock(); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
561 |
|
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
562 |
|
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
563 |
|
public void testAwaitUntil_Timeout(boolean fair) { |
564 |
+ |
final ReentrantLock lock = new ReentrantLock(fair); |
565 |
+ |
final Condition c = lock.newCondition(); |
566 |
+ |
lock.lock(); |
567 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
568 |
+ |
// use the same time source, so don't use nanoTime here. |
569 |
+ |
final java.util.Date delayedDate = delayedDate(timeoutMillis()); |
570 |
|
try { |
571 |
< |
final ReentrantLock lock = new ReentrantLock(fair); |
572 |
< |
final Condition c = lock.newCondition(); |
573 |
< |
lock.lock(); |
574 |
< |
long startTime = System.nanoTime(); |
576 |
< |
long timeoutMillis = 10; |
577 |
< |
java.util.Date d = new java.util.Date(); |
578 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
579 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
580 |
< |
lock.unlock(); |
581 |
< |
} catch (InterruptedException e) { |
582 |
< |
threadUnexpectedException(e); |
583 |
< |
} |
571 |
> |
assertFalse(c.awaitUntil(delayedDate)); |
572 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
573 |
> |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
574 |
> |
lock.unlock(); |
575 |
|
} |
576 |
|
|
577 |
|
/** |
735 |
|
public void testHasWaiters(boolean fair) { |
736 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
737 |
|
final Condition c = lock.newCondition(); |
738 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
738 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
739 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
740 |
|
public void realRun() throws InterruptedException { |
741 |
|
lock.lock(); |
742 |
|
assertHasNoWaiters(lock, c); |
743 |
|
assertFalse(lock.hasWaiters(c)); |
744 |
< |
locked.countDown(); |
744 |
> |
pleaseSignal.countDown(); |
745 |
|
c.await(); |
746 |
|
assertHasNoWaiters(lock, c); |
747 |
|
assertFalse(lock.hasWaiters(c)); |
748 |
|
lock.unlock(); |
749 |
|
}}); |
750 |
|
|
751 |
< |
await(locked); |
751 |
> |
await(pleaseSignal); |
752 |
|
lock.lock(); |
753 |
|
assertHasWaiters(lock, c, t); |
754 |
|
assertTrue(lock.hasWaiters(c)); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
882 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
882 |
> |
* awaitUninterruptibly is uninterruptible |
883 |
|
*/ |
884 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
885 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
886 |
|
public void testAwaitUninterruptibly(boolean fair) { |
887 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
888 |
< |
final Condition c = lock.newCondition(); |
889 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
890 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
888 |
> |
final Condition condition = lock.newCondition(); |
889 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
890 |
> |
|
891 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
892 |
|
public void realRun() { |
893 |
+ |
// Interrupt before awaitUninterruptibly |
894 |
|
lock.lock(); |
895 |
< |
locked.countDown(); |
896 |
< |
c.awaitUninterruptibly(); |
895 |
> |
pleaseInterrupt.countDown(); |
896 |
> |
Thread.currentThread().interrupt(); |
897 |
> |
condition.awaitUninterruptibly(); |
898 |
|
assertTrue(Thread.interrupted()); |
899 |
|
lock.unlock(); |
900 |
|
}}); |
901 |
|
|
902 |
< |
await(locked); |
902 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
903 |
> |
public void realRun() { |
904 |
> |
// Interrupt during awaitUninterruptibly |
905 |
> |
lock.lock(); |
906 |
> |
pleaseInterrupt.countDown(); |
907 |
> |
condition.awaitUninterruptibly(); |
908 |
> |
assertTrue(Thread.interrupted()); |
909 |
> |
lock.unlock(); |
910 |
> |
}}); |
911 |
> |
|
912 |
> |
await(pleaseInterrupt); |
913 |
> |
t2.interrupt(); |
914 |
|
lock.lock(); |
915 |
|
lock.unlock(); |
916 |
< |
t.interrupt(); |
917 |
< |
long timeoutMillis = 10; |
918 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
916 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
917 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
918 |
> |
|
919 |
|
lock.lock(); |
920 |
< |
c.signal(); |
920 |
> |
condition.signalAll(); |
921 |
|
lock.unlock(); |
922 |
< |
awaitTermination(t); |
922 |
> |
|
923 |
> |
awaitTermination(t1); |
924 |
> |
awaitTermination(t2); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
929 |
|
*/ |
930 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
931 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
932 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
933 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
934 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
935 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
936 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
939 |
|
final PublicReentrantLock lock = |
940 |
|
new PublicReentrantLock(fair); |
941 |
|
final Condition c = lock.newCondition(); |
942 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
942 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
943 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
944 |
|
public void realRun() throws InterruptedException { |
945 |
|
lock.lock(); |
946 |
|
assertLockedByMoi(lock); |
947 |
|
assertHasNoWaiters(lock, c); |
948 |
< |
locked.countDown(); |
948 |
> |
pleaseInterrupt.countDown(); |
949 |
|
try { |
950 |
|
await(c, awaitMethod); |
951 |
|
} finally { |
956 |
|
} |
957 |
|
}}); |
958 |
|
|
959 |
< |
await(locked); |
959 |
> |
await(pleaseInterrupt); |
960 |
|
assertHasWaiters(lock, c, t); |
961 |
|
t.interrupt(); |
962 |
|
awaitTermination(t); |
968 |
|
*/ |
969 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
970 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
971 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
972 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
973 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
974 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
975 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
977 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
978 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
979 |
|
final Condition c = lock.newCondition(); |
980 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
980 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
981 |
|
class Awaiter extends CheckedRunnable { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
|
lock.lock(); |
984 |
< |
locked.countDown(); |
984 |
> |
pleaseSignal.countDown(); |
985 |
|
await(c, awaitMethod); |
986 |
|
lock.unlock(); |
987 |
|
} |
990 |
|
Thread t1 = newStartedThread(new Awaiter()); |
991 |
|
Thread t2 = newStartedThread(new Awaiter()); |
992 |
|
|
993 |
< |
await(locked); |
993 |
> |
await(pleaseSignal); |
994 |
|
lock.lock(); |
995 |
|
assertHasWaiters(lock, c, t1, t2); |
996 |
|
c.signalAll(); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* signal wakes up waiting threads in FIFO order. |
1004 |
> |
* signal wakes up waiting threads in FIFO order |
1005 |
|
*/ |
1006 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1007 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1055 |
|
public void testAwaitLockCount(boolean fair) { |
1056 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1057 |
|
final Condition c = lock.newCondition(); |
1058 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1058 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1059 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
|
lock.lock(); |
1062 |
|
assertLockedByMoi(lock); |
1063 |
|
assertEquals(1, lock.getHoldCount()); |
1064 |
< |
locked.countDown(); |
1064 |
> |
pleaseSignal.countDown(); |
1065 |
|
c.await(); |
1066 |
|
assertLockedByMoi(lock); |
1067 |
|
assertEquals(1, lock.getHoldCount()); |
1074 |
|
lock.lock(); |
1075 |
|
assertLockedByMoi(lock); |
1076 |
|
assertEquals(2, lock.getHoldCount()); |
1077 |
< |
locked.countDown(); |
1077 |
> |
pleaseSignal.countDown(); |
1078 |
|
c.await(); |
1079 |
|
assertLockedByMoi(lock); |
1080 |
|
assertEquals(2, lock.getHoldCount()); |
1082 |
|
lock.unlock(); |
1083 |
|
}}); |
1084 |
|
|
1085 |
< |
await(locked); |
1085 |
> |
await(pleaseSignal); |
1086 |
|
lock.lock(); |
1087 |
|
assertHasWaiters(lock, c, t1, t2); |
1088 |
|
assertEquals(1, lock.getHoldCount()); |
1099 |
|
public void testSerialization() { testSerialization(false); } |
1100 |
|
public void testSerialization_fair() { testSerialization(true); } |
1101 |
|
public void testSerialization(boolean fair) { |
1102 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1102 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1103 |
|
lock.lock(); |
1104 |
|
|
1105 |
|
ReentrantLock clone = serialClone(lock); |
1125 |
|
public void testToString() { testToString(false); } |
1126 |
|
public void testToString_fair() { testToString(true); } |
1127 |
|
public void testToString(boolean fair) { |
1128 |
< |
ReentrantLock lock = new ReentrantLock(fair); |
1129 |
< |
String us = lock.toString(); |
1119 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1128 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
1129 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1130 |
|
lock.lock(); |
1131 |
< |
String ls = lock.toString(); |
1132 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1131 |
> |
assertTrue(lock.toString().contains("Locked by")); |
1132 |
> |
lock.unlock(); |
1133 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1134 |
|
} |
1135 |
|
} |