6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
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.atomic.AtomicBoolean; |
16 |
|
import java.util.concurrent.locks.Condition; |
17 |
|
import java.util.concurrent.locks.Lock; |
18 |
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
19 |
< |
import java.util.concurrent.CountDownLatch; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
< |
import java.util.*; |
19 |
> |
|
20 |
> |
import junit.framework.AssertionFailedError; |
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
94 |
< |
assertTrue(lock.getOwner() != t); |
94 |
> |
assertNotSame(t, lock.getOwner()); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
152 |
|
lock.writeLock().unlock(); |
153 |
|
} |
154 |
|
|
155 |
< |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
155 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
156 |
|
|
157 |
|
/** |
158 |
|
* Awaits condition using the specified AwaitMethod. |
1011 |
|
} |
1012 |
|
|
1013 |
|
/** |
1014 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1014 |
> |
* awaitUninterruptibly is uninterruptible |
1015 |
|
*/ |
1016 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1017 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1018 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1019 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1020 |
|
final Condition c = lock.writeLock().newCondition(); |
1021 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1022 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1021 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1022 |
> |
|
1023 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1024 |
|
public void realRun() { |
1025 |
+ |
// Interrupt before awaitUninterruptibly |
1026 |
|
lock.writeLock().lock(); |
1027 |
< |
locked.countDown(); |
1027 |
> |
pleaseInterrupt.countDown(); |
1028 |
> |
Thread.currentThread().interrupt(); |
1029 |
|
c.awaitUninterruptibly(); |
1030 |
|
assertTrue(Thread.interrupted()); |
1031 |
|
lock.writeLock().unlock(); |
1032 |
|
}}); |
1033 |
|
|
1034 |
< |
await(locked); |
1034 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1035 |
> |
public void realRun() { |
1036 |
> |
// Interrupt during awaitUninterruptibly |
1037 |
> |
lock.writeLock().lock(); |
1038 |
> |
pleaseInterrupt.countDown(); |
1039 |
> |
c.awaitUninterruptibly(); |
1040 |
> |
assertTrue(Thread.interrupted()); |
1041 |
> |
lock.writeLock().unlock(); |
1042 |
> |
}}); |
1043 |
> |
|
1044 |
> |
await(pleaseInterrupt); |
1045 |
|
lock.writeLock().lock(); |
1046 |
|
lock.writeLock().unlock(); |
1047 |
< |
t.interrupt(); |
1048 |
< |
long timeoutMillis = 10; |
1049 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1047 |
> |
t2.interrupt(); |
1048 |
> |
|
1049 |
> |
assertThreadStaysAlive(t1); |
1050 |
> |
assertTrue(t2.isAlive()); |
1051 |
> |
|
1052 |
|
lock.writeLock().lock(); |
1053 |
< |
c.signal(); |
1053 |
> |
c.signalAll(); |
1054 |
|
lock.writeLock().unlock(); |
1055 |
< |
awaitTermination(t); |
1055 |
> |
|
1056 |
> |
awaitTermination(t1); |
1057 |
> |
awaitTermination(t2); |
1058 |
|
} |
1059 |
|
|
1060 |
|
/** |