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); } |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1116 |
< |
* signal wakes up waiting threads in FIFO order. |
1116 |
> |
* signal wakes up waiting threads in FIFO order |
1117 |
|
*/ |
1118 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1119 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1274 |
|
} |
1275 |
|
|
1276 |
|
/** |
1277 |
< |
* hasQueuedThread reports whether a thread is queued. |
1277 |
> |
* hasQueuedThread reports whether a thread is queued |
1278 |
|
*/ |
1279 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1280 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1615 |
|
public void testToString_fair() { testToString(true); } |
1616 |
|
public void testToString(boolean fair) { |
1617 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1618 |
< |
String us = lock.toString(); |
1619 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1620 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1621 |
< |
lock.writeLock().lock(); |
1622 |
< |
String ws = lock.toString(); |
1621 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1622 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1618 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1619 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1620 |
> |
lock.writeLock().lock(); |
1621 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1622 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1623 |
|
lock.writeLock().unlock(); |
1624 |
|
lock.readLock().lock(); |
1625 |
|
lock.readLock().lock(); |
1626 |
< |
String rs = lock.toString(); |
1627 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1628 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1626 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1627 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1628 |
|
} |
1629 |
|
|
1630 |
|
/** |
1634 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1635 |
|
public void testReadLockToString(boolean fair) { |
1636 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1637 |
< |
String us = lock.readLock().toString(); |
1639 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1637 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1638 |
|
lock.readLock().lock(); |
1639 |
|
lock.readLock().lock(); |
1640 |
< |
String rs = lock.readLock().toString(); |
1643 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1640 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1641 |
|
} |
1642 |
|
|
1643 |
|
/** |
1647 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1648 |
|
public void testWriteLockToString(boolean fair) { |
1649 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1650 |
< |
String us = lock.writeLock().toString(); |
1654 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1650 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1651 |
|
lock.writeLock().lock(); |
1652 |
< |
String ls = lock.writeLock().toString(); |
1653 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1652 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1653 |
> |
lock.writeLock().unlock(); |
1654 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1655 |
|
} |
1656 |
|
|
1657 |
|
} |