8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
11 |
< |
import java.util.concurrent.locks.*; |
12 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.concurrent.locks.Condition; |
12 |
> |
import java.util.concurrent.locks.Lock; |
13 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.io.*; |
16 |
|
import java.util.*; |
17 |
|
|
18 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
508 |
|
|
509 |
|
/** |
510 |
|
* A thread that tries to acquire a fair read lock (non-reentrantly) |
511 |
< |
* will block if there is a waiting writer thread. |
511 |
> |
* will block if there is a waiting writer thread |
512 |
|
*/ |
513 |
|
public void testReaderWriterReaderFairFifo() { |
514 |
|
final PublicReentrantReadWriteLock lock = |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* Read trylock succeeds (barging) even in the presence of waiting readers and/or writers. |
588 |
> |
* Read trylock succeeds (barging) even in the presence of waiting |
589 |
> |
* readers and/or writers |
590 |
|
*/ |
591 |
|
public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
592 |
|
public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1008 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1008 |
> |
* awaitUninterruptibly is uninterruptible |
1009 |
|
*/ |
1010 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1011 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1012 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1013 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1014 |
|
final Condition c = lock.writeLock().newCondition(); |
1015 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1016 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1015 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1016 |
> |
|
1017 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1018 |
|
public void realRun() { |
1019 |
+ |
// Interrupt before awaitUninterruptibly |
1020 |
|
lock.writeLock().lock(); |
1021 |
< |
locked.countDown(); |
1021 |
> |
pleaseInterrupt.countDown(); |
1022 |
> |
Thread.currentThread().interrupt(); |
1023 |
|
c.awaitUninterruptibly(); |
1024 |
|
assertTrue(Thread.interrupted()); |
1025 |
|
lock.writeLock().unlock(); |
1026 |
|
}}); |
1027 |
|
|
1028 |
< |
await(locked); |
1028 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1029 |
> |
public void realRun() { |
1030 |
> |
// Interrupt during awaitUninterruptibly |
1031 |
> |
lock.writeLock().lock(); |
1032 |
> |
pleaseInterrupt.countDown(); |
1033 |
> |
c.awaitUninterruptibly(); |
1034 |
> |
assertTrue(Thread.interrupted()); |
1035 |
> |
lock.writeLock().unlock(); |
1036 |
> |
}}); |
1037 |
> |
|
1038 |
> |
await(pleaseInterrupt); |
1039 |
|
lock.writeLock().lock(); |
1040 |
|
lock.writeLock().unlock(); |
1041 |
< |
t.interrupt(); |
1042 |
< |
long timeoutMillis = 10; |
1043 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1041 |
> |
t2.interrupt(); |
1042 |
> |
|
1043 |
> |
assertThreadStaysAlive(t1); |
1044 |
> |
assertTrue(t2.isAlive()); |
1045 |
> |
|
1046 |
|
lock.writeLock().lock(); |
1047 |
< |
c.signal(); |
1047 |
> |
c.signalAll(); |
1048 |
|
lock.writeLock().unlock(); |
1049 |
< |
awaitTermination(t); |
1049 |
> |
|
1050 |
> |
awaitTermination(t1); |
1051 |
> |
awaitTermination(t2); |
1052 |
|
} |
1053 |
|
|
1054 |
|
/** |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* signal wakes up waiting threads in FIFO order. |
1133 |
> |
* signal wakes up waiting threads in FIFO order |
1134 |
|
*/ |
1135 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1136 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1291 |
|
} |
1292 |
|
|
1293 |
|
/** |
1294 |
< |
* hasQueuedThread reports whether a thread is queued. |
1294 |
> |
* hasQueuedThread reports whether a thread is queued |
1295 |
|
*/ |
1296 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1297 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1632 |
|
public void testToString_fair() { testToString(true); } |
1633 |
|
public void testToString(boolean fair) { |
1634 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1635 |
< |
String us = lock.toString(); |
1636 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1637 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1638 |
< |
lock.writeLock().lock(); |
1639 |
< |
String ws = lock.toString(); |
1621 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1622 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1635 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1636 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1637 |
> |
lock.writeLock().lock(); |
1638 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1639 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1640 |
|
lock.writeLock().unlock(); |
1641 |
|
lock.readLock().lock(); |
1642 |
|
lock.readLock().lock(); |
1643 |
< |
String rs = lock.toString(); |
1644 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1628 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1643 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1644 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1645 |
|
} |
1646 |
|
|
1647 |
|
/** |
1651 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1652 |
|
public void testReadLockToString(boolean fair) { |
1653 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1654 |
< |
String us = lock.readLock().toString(); |
1639 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1654 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1655 |
|
lock.readLock().lock(); |
1656 |
|
lock.readLock().lock(); |
1657 |
< |
String rs = lock.readLock().toString(); |
1643 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1657 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1658 |
|
} |
1659 |
|
|
1660 |
|
/** |
1664 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1665 |
|
public void testWriteLockToString(boolean fair) { |
1666 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1667 |
< |
String us = lock.writeLock().toString(); |
1654 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1667 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1668 |
|
lock.writeLock().lock(); |
1669 |
< |
String ls = lock.writeLock().toString(); |
1670 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1669 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1670 |
> |
lock.writeLock().unlock(); |
1671 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1672 |
|
} |
1673 |
|
|
1674 |
|
} |