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 { |
146 |
|
lock.writeLock().unlock(); |
147 |
|
} |
148 |
|
|
149 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
149 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }; |
150 |
|
|
151 |
|
/** |
152 |
|
* Awaits condition using the specified AwaitMethod. |
157 |
|
case await: |
158 |
|
c.await(); |
159 |
|
break; |
160 |
+ |
case awaitTimed: |
161 |
+ |
assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
162 |
+ |
break; |
163 |
|
case awaitNanos: |
164 |
|
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
165 |
|
assertTrue(nanosRemaining > 0); |
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); } |
871 |
|
public void testAwait_IMSE(boolean fair) { |
872 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
873 |
|
final Condition c = lock.writeLock().newCondition(); |
874 |
< |
long startTime = System.nanoTime(); |
875 |
< |
try { |
871 |
< |
try { |
872 |
< |
c.await(); |
873 |
< |
shouldThrow(); |
874 |
< |
} catch (IllegalMonitorStateException success) {} |
875 |
< |
try { |
876 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
877 |
< |
shouldThrow(); |
878 |
< |
} catch (IllegalMonitorStateException success) {} |
879 |
< |
try { |
880 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
881 |
< |
shouldThrow(); |
882 |
< |
} catch (IllegalMonitorStateException success) {} |
874 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
875 |
> |
long startTime = System.nanoTime(); |
876 |
|
try { |
877 |
< |
c.awaitUninterruptibly(); |
877 |
> |
await(c, awaitMethod); |
878 |
|
shouldThrow(); |
879 |
< |
} catch (IllegalMonitorStateException success) {} |
880 |
< |
} catch (InterruptedException ie) { |
881 |
< |
threadUnexpectedException(ie); |
879 |
> |
} catch (IllegalMonitorStateException success) { |
880 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
881 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
882 |
|
} |
890 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
1039 |
|
*/ |
1040 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1041 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1042 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1043 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1044 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1045 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1046 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1078 |
|
*/ |
1079 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1080 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1081 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1082 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1083 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1084 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1085 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
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(); |
1627 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1628 |
< |
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); |
1634 |
< |
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(); |
1645 |
< |
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(); |
1649 |
< |
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(); |
1660 |
< |
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 |
|
} |