6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.locks.Condition; |
11 |
< |
import java.util.concurrent.locks.ReentrantLock; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
< |
import java.util.*; |
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) { |
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, awaitTimed, awaitNanos, awaitUntil }; |
146 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
|
|
148 |
|
/** |
149 |
|
* Awaits condition using the specified AwaitMethod. |
166 |
|
case awaitUntil: |
167 |
|
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
168 |
|
break; |
169 |
+ |
default: |
170 |
+ |
throw new AssertionError(); |
171 |
|
} |
172 |
|
} |
173 |
|
|
744 |
|
public void testHasWaiters(boolean fair) { |
745 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
746 |
|
final Condition c = lock.newCondition(); |
747 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
747 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
748 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
749 |
|
public void realRun() throws InterruptedException { |
750 |
|
lock.lock(); |
751 |
|
assertHasNoWaiters(lock, c); |
752 |
|
assertFalse(lock.hasWaiters(c)); |
753 |
< |
locked.countDown(); |
753 |
> |
pleaseSignal.countDown(); |
754 |
|
c.await(); |
755 |
|
assertHasNoWaiters(lock, c); |
756 |
|
assertFalse(lock.hasWaiters(c)); |
757 |
|
lock.unlock(); |
758 |
|
}}); |
759 |
|
|
760 |
< |
await(locked); |
760 |
> |
await(pleaseSignal); |
761 |
|
lock.lock(); |
762 |
|
assertHasWaiters(lock, c, t); |
763 |
|
assertTrue(lock.hasWaiters(c)); |
888 |
|
} |
889 |
|
|
890 |
|
/** |
891 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
891 |
> |
* awaitUninterruptibly is uninterruptible |
892 |
|
*/ |
893 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
894 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
895 |
|
public void testAwaitUninterruptibly(boolean fair) { |
896 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
897 |
|
final Condition c = lock.newCondition(); |
898 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
899 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
898 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
899 |
> |
|
900 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
901 |
|
public void realRun() { |
902 |
+ |
// Interrupt before awaitUninterruptibly |
903 |
|
lock.lock(); |
904 |
< |
locked.countDown(); |
904 |
> |
pleaseInterrupt.countDown(); |
905 |
> |
Thread.currentThread().interrupt(); |
906 |
|
c.awaitUninterruptibly(); |
907 |
|
assertTrue(Thread.interrupted()); |
908 |
|
lock.unlock(); |
909 |
|
}}); |
910 |
|
|
911 |
< |
await(locked); |
911 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
912 |
> |
public void realRun() { |
913 |
> |
// Interrupt during awaitUninterruptibly |
914 |
> |
lock.lock(); |
915 |
> |
pleaseInterrupt.countDown(); |
916 |
> |
c.awaitUninterruptibly(); |
917 |
> |
assertTrue(Thread.interrupted()); |
918 |
> |
lock.unlock(); |
919 |
> |
}}); |
920 |
> |
|
921 |
> |
await(pleaseInterrupt); |
922 |
|
lock.lock(); |
923 |
|
lock.unlock(); |
924 |
< |
t.interrupt(); |
925 |
< |
long timeoutMillis = 10; |
926 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
924 |
> |
t2.interrupt(); |
925 |
> |
|
926 |
> |
assertThreadStaysAlive(t1); |
927 |
> |
assertTrue(t2.isAlive()); |
928 |
> |
|
929 |
|
lock.lock(); |
930 |
< |
c.signal(); |
930 |
> |
c.signalAll(); |
931 |
|
lock.unlock(); |
932 |
< |
awaitTermination(t); |
932 |
> |
|
933 |
> |
awaitTermination(t1); |
934 |
> |
awaitTermination(t2); |
935 |
|
} |
936 |
|
|
937 |
|
/** |
949 |
|
final PublicReentrantLock lock = |
950 |
|
new PublicReentrantLock(fair); |
951 |
|
final Condition c = lock.newCondition(); |
952 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
952 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
953 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
954 |
|
public void realRun() throws InterruptedException { |
955 |
|
lock.lock(); |
956 |
|
assertLockedByMoi(lock); |
957 |
|
assertHasNoWaiters(lock, c); |
958 |
< |
locked.countDown(); |
958 |
> |
pleaseInterrupt.countDown(); |
959 |
|
try { |
960 |
|
await(c, awaitMethod); |
961 |
|
} finally { |
966 |
|
} |
967 |
|
}}); |
968 |
|
|
969 |
< |
await(locked); |
969 |
> |
await(pleaseInterrupt); |
970 |
|
assertHasWaiters(lock, c, t); |
971 |
|
t.interrupt(); |
972 |
|
awaitTermination(t); |
987 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
988 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
989 |
|
final Condition c = lock.newCondition(); |
990 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
990 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
991 |
|
class Awaiter extends CheckedRunnable { |
992 |
|
public void realRun() throws InterruptedException { |
993 |
|
lock.lock(); |
994 |
< |
locked.countDown(); |
994 |
> |
pleaseSignal.countDown(); |
995 |
|
await(c, awaitMethod); |
996 |
|
lock.unlock(); |
997 |
|
} |
1000 |
|
Thread t1 = newStartedThread(new Awaiter()); |
1001 |
|
Thread t2 = newStartedThread(new Awaiter()); |
1002 |
|
|
1003 |
< |
await(locked); |
1003 |
> |
await(pleaseSignal); |
1004 |
|
lock.lock(); |
1005 |
|
assertHasWaiters(lock, c, t1, t2); |
1006 |
|
c.signalAll(); |
1065 |
|
public void testAwaitLockCount(boolean fair) { |
1066 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1067 |
|
final Condition c = lock.newCondition(); |
1068 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1068 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1069 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1070 |
|
public void realRun() throws InterruptedException { |
1071 |
|
lock.lock(); |
1072 |
|
assertLockedByMoi(lock); |
1073 |
|
assertEquals(1, lock.getHoldCount()); |
1074 |
< |
locked.countDown(); |
1074 |
> |
pleaseSignal.countDown(); |
1075 |
|
c.await(); |
1076 |
|
assertLockedByMoi(lock); |
1077 |
|
assertEquals(1, lock.getHoldCount()); |
1084 |
|
lock.lock(); |
1085 |
|
assertLockedByMoi(lock); |
1086 |
|
assertEquals(2, lock.getHoldCount()); |
1087 |
< |
locked.countDown(); |
1087 |
> |
pleaseSignal.countDown(); |
1088 |
|
c.await(); |
1089 |
|
assertLockedByMoi(lock); |
1090 |
|
assertEquals(2, lock.getHoldCount()); |
1092 |
|
lock.unlock(); |
1093 |
|
}}); |
1094 |
|
|
1095 |
< |
await(locked); |
1095 |
> |
await(pleaseSignal); |
1096 |
|
lock.lock(); |
1097 |
|
assertHasWaiters(lock, c, t1, t2); |
1098 |
|
assertEquals(1, lock.getHoldCount()); |