17 |
|
import java.util.concurrent.locks.Lock; |
18 |
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
19 |
|
|
20 |
– |
import junit.framework.AssertionFailedError; |
20 |
|
import junit.framework.Test; |
21 |
|
import junit.framework.TestSuite; |
22 |
|
|
23 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
24 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
26 |
|
main(suite(), args); |
87 |
|
long startTime = System.nanoTime(); |
88 |
|
while (!lock.hasQueuedThread(t)) { |
89 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
90 |
< |
throw new AssertionFailedError("timed out"); |
90 |
> |
throw new AssertionError("timed out"); |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
1009 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1010 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1011 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1012 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1013 |
< |
final Condition c = lock.writeLock().newCondition(); |
1012 |
> |
final Lock lock = new ReentrantReadWriteLock(fair).writeLock(); |
1013 |
> |
final Condition condition = lock.newCondition(); |
1014 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1015 |
|
|
1016 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1017 |
|
public void realRun() { |
1018 |
|
// Interrupt before awaitUninterruptibly |
1019 |
< |
lock.writeLock().lock(); |
1019 |
> |
lock.lock(); |
1020 |
|
pleaseInterrupt.countDown(); |
1021 |
|
Thread.currentThread().interrupt(); |
1022 |
< |
c.awaitUninterruptibly(); |
1022 |
> |
condition.awaitUninterruptibly(); |
1023 |
|
assertTrue(Thread.interrupted()); |
1024 |
< |
lock.writeLock().unlock(); |
1024 |
> |
lock.unlock(); |
1025 |
|
}}); |
1026 |
|
|
1027 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
1028 |
|
public void realRun() { |
1029 |
|
// Interrupt during awaitUninterruptibly |
1030 |
< |
lock.writeLock().lock(); |
1030 |
> |
lock.lock(); |
1031 |
|
pleaseInterrupt.countDown(); |
1032 |
< |
c.awaitUninterruptibly(); |
1032 |
> |
condition.awaitUninterruptibly(); |
1033 |
|
assertTrue(Thread.interrupted()); |
1034 |
< |
lock.writeLock().unlock(); |
1034 |
> |
lock.unlock(); |
1035 |
|
}}); |
1036 |
|
|
1037 |
|
await(pleaseInterrupt); |
1038 |
– |
lock.writeLock().lock(); |
1039 |
– |
lock.writeLock().unlock(); |
1038 |
|
t2.interrupt(); |
1039 |
< |
|
1040 |
< |
assertThreadStaysAlive(t1); |
1041 |
< |
assertTrue(t2.isAlive()); |
1042 |
< |
|
1043 |
< |
lock.writeLock().lock(); |
1044 |
< |
c.signalAll(); |
1045 |
< |
lock.writeLock().unlock(); |
1039 |
> |
lock.lock(); |
1040 |
> |
lock.unlock(); |
1041 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
1042 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
1043 |
> |
|
1044 |
> |
lock.lock(); |
1045 |
> |
condition.signalAll(); |
1046 |
> |
lock.unlock(); |
1047 |
|
|
1048 |
|
awaitTermination(t1); |
1049 |
|
awaitTermination(t2); |