6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.atomic.AtomicBoolean; |
11 |
– |
import java.util.concurrent.locks.*; |
12 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.io.*; |
11 |
< |
import java.util.*; |
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 |
> |
|
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. |
174 |
|
java.util.Date d = new java.util.Date(); |
175 |
|
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
176 |
|
break; |
177 |
+ |
default: |
178 |
+ |
throw new AssertionError(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
957 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
958 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
959 |
|
lock.writeLock().unlock(); |
960 |
< |
} catch (InterruptedException e) { |
952 |
< |
threadUnexpectedException(e); |
953 |
< |
} |
960 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
961 |
|
} |
962 |
|
|
963 |
|
/** |
977 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
978 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
979 |
|
lock.writeLock().unlock(); |
980 |
< |
} catch (InterruptedException e) { |
974 |
< |
threadUnexpectedException(e); |
975 |
< |
} |
980 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
981 |
|
} |
982 |
|
|
983 |
|
/** |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1012 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1012 |
> |
* awaitUninterruptibly is uninterruptible |
1013 |
|
*/ |
1014 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1015 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1016 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1017 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1018 |
|
final Condition c = lock.writeLock().newCondition(); |
1019 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1020 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1019 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1020 |
> |
|
1021 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1022 |
|
public void realRun() { |
1023 |
+ |
// Interrupt before awaitUninterruptibly |
1024 |
|
lock.writeLock().lock(); |
1025 |
< |
locked.countDown(); |
1025 |
> |
pleaseInterrupt.countDown(); |
1026 |
> |
Thread.currentThread().interrupt(); |
1027 |
|
c.awaitUninterruptibly(); |
1028 |
|
assertTrue(Thread.interrupted()); |
1029 |
|
lock.writeLock().unlock(); |
1030 |
|
}}); |
1031 |
|
|
1032 |
< |
await(locked); |
1032 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1033 |
> |
public void realRun() { |
1034 |
> |
// Interrupt during awaitUninterruptibly |
1035 |
> |
lock.writeLock().lock(); |
1036 |
> |
pleaseInterrupt.countDown(); |
1037 |
> |
c.awaitUninterruptibly(); |
1038 |
> |
assertTrue(Thread.interrupted()); |
1039 |
> |
lock.writeLock().unlock(); |
1040 |
> |
}}); |
1041 |
> |
|
1042 |
> |
await(pleaseInterrupt); |
1043 |
|
lock.writeLock().lock(); |
1044 |
|
lock.writeLock().unlock(); |
1045 |
< |
t.interrupt(); |
1046 |
< |
long timeoutMillis = 10; |
1047 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1045 |
> |
t2.interrupt(); |
1046 |
> |
|
1047 |
> |
assertThreadStaysAlive(t1); |
1048 |
> |
assertTrue(t2.isAlive()); |
1049 |
> |
|
1050 |
|
lock.writeLock().lock(); |
1051 |
< |
c.signal(); |
1051 |
> |
c.signalAll(); |
1052 |
|
lock.writeLock().unlock(); |
1053 |
< |
awaitTermination(t); |
1053 |
> |
|
1054 |
> |
awaitTermination(t1); |
1055 |
> |
awaitTermination(t2); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |