ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.23 by dl, Mon Aug 1 19:53:01 2005 UTC vs.
Revision 1.64 by jsr166, Tue May 24 23:40:14 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
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.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.io.*;
15   import java.util.*;
16  
17   public class ReentrantReadWriteLockTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());  
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22 <        return new TestSuite(ReentrantReadWriteLockTest.class);
22 >        return new TestSuite(ReentrantReadWriteLockTest.class);
23      }
24  
25      /**
26       * A runnable calling lockInterruptibly
27       */
28 <    class InterruptibleLockRunnable implements Runnable {
28 >    class InterruptibleLockRunnable extends CheckedRunnable {
29          final ReentrantReadWriteLock lock;
30          InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
31 <        public void run() {
32 <            try {
31 <                lock.writeLock().lockInterruptibly();
32 <            } catch(InterruptedException success){}
31 >        public void realRun() throws InterruptedException {
32 >            lock.writeLock().lockInterruptibly();
33          }
34      }
35  
36
36      /**
37       * A runnable calling lockInterruptibly that expects to be
38       * interrupted
39       */
40 <    class InterruptedLockRunnable implements Runnable {
40 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
41          final ReentrantReadWriteLock lock;
42          InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
43 <        public void run() {
44 <            try {
46 <                lock.writeLock().lockInterruptibly();
47 <                threadShouldThrow();
48 <            } catch(InterruptedException success){}
43 >        public void realRun() throws InterruptedException {
44 >            lock.writeLock().lockInterruptibly();
45          }
46      }
47  
# Line 54 | Line 50 | public class ReentrantReadWriteLockTest
50       */
51      static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
52          PublicReentrantReadWriteLock() { super(); }
53 <        public Collection<Thread> getQueuedThreads() {
54 <            return super.getQueuedThreads();
53 >        PublicReentrantReadWriteLock(boolean fair) { super(fair); }
54 >        public Thread getOwner() {
55 >            return super.getOwner();
56 >        }
57 >        public Collection<Thread> getQueuedThreads() {
58 >            return super.getQueuedThreads();
59          }
60 <        public Collection<Thread> getWaitingThreads(Condition c) {
61 <            return super.getWaitingThreads(c);
60 >        public Collection<Thread> getWaitingThreads(Condition c) {
61 >            return super.getWaitingThreads(c);
62          }
63      }
64  
65      /**
66 <     * Constructor sets given fairness, and is in unlocked state
66 >     * Releases write lock, checking that it had a hold count of 1.
67       */
68 <    public void testConstructor() {
69 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
70 <        assertFalse(rl.isFair());
71 <        assertFalse(rl.isWriteLocked());
72 <        assertEquals(0, rl.getReadLockCount());
73 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
74 <        assertTrue(r2.isFair());
75 <        assertFalse(r2.isWriteLocked());
76 <        assertEquals(0, r2.getReadLockCount());
68 >    void releaseWriteLock(PublicReentrantReadWriteLock lock) {
69 >        ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
70 >        assertWriteLockedByMoi(lock);
71 >        assertEquals(1, lock.getWriteHoldCount());
72 >        writeLock.unlock();
73 >        assertNotWriteLocked(lock);
74      }
75  
76      /**
77 <     * write-locking and read-locking an unlocked lock succeed
77 >     * Spin-waits until lock.hasQueuedThread(t) becomes true.
78       */
79 <    public void testLock() {
80 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
81 <        rl.writeLock().lock();
82 <        assertTrue(rl.isWriteLocked());
83 <        assertTrue(rl.isWriteLockedByCurrentThread());
84 <        assertEquals(0, rl.getReadLockCount());
85 <        rl.writeLock().unlock();
86 <        assertFalse(rl.isWriteLocked());
87 <        assertFalse(rl.isWriteLockedByCurrentThread());
91 <        assertEquals(0, rl.getReadLockCount());
92 <        rl.readLock().lock();
93 <        assertFalse(rl.isWriteLocked());
94 <        assertFalse(rl.isWriteLockedByCurrentThread());
95 <        assertEquals(1, rl.getReadLockCount());
96 <        rl.readLock().unlock();
97 <        assertFalse(rl.isWriteLocked());
98 <        assertFalse(rl.isWriteLockedByCurrentThread());
99 <        assertEquals(0, rl.getReadLockCount());
79 >    void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
80 >        long startTime = System.nanoTime();
81 >        while (!lock.hasQueuedThread(t)) {
82 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
83 >                throw new AssertionFailedError("timed out");
84 >            Thread.yield();
85 >        }
86 >        assertTrue(t.isAlive());
87 >        assertTrue(lock.getOwner() != t);
88      }
89  
90 +    /**
91 +     * Checks that lock is not write-locked.
92 +     */
93 +    void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
94 +        assertFalse(lock.isWriteLocked());
95 +        assertFalse(lock.isWriteLockedByCurrentThread());
96 +        assertFalse(lock.writeLock().isHeldByCurrentThread());
97 +        assertEquals(0, lock.getWriteHoldCount());
98 +        assertEquals(0, lock.writeLock().getHoldCount());
99 +        assertNull(lock.getOwner());
100 +    }
101  
102      /**
103 <     * locking an unlocked fair lock succeeds
104 <     */
105 <    public void testFairLock() {
106 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
107 <        rl.writeLock().lock();
108 <        assertTrue(rl.isWriteLocked());
109 <        assertTrue(rl.isWriteLockedByCurrentThread());
110 <        assertEquals(0, rl.getReadLockCount());
111 <        rl.writeLock().unlock();
112 <        assertFalse(rl.isWriteLocked());
113 <        assertFalse(rl.isWriteLockedByCurrentThread());
114 <        assertEquals(0, rl.getReadLockCount());
115 <        rl.readLock().lock();
116 <        assertFalse(rl.isWriteLocked());
118 <        assertFalse(rl.isWriteLockedByCurrentThread());
119 <        assertEquals(1, rl.getReadLockCount());
120 <        rl.readLock().unlock();
121 <        assertFalse(rl.isWriteLocked());
122 <        assertFalse(rl.isWriteLockedByCurrentThread());
123 <        assertEquals(0, rl.getReadLockCount());
103 >     * Checks that lock is write-locked by the given thread.
104 >     */
105 >    void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
106 >        assertTrue(lock.isWriteLocked());
107 >        assertSame(t, lock.getOwner());
108 >        assertEquals(t == Thread.currentThread(),
109 >                     lock.isWriteLockedByCurrentThread());
110 >        assertEquals(t == Thread.currentThread(),
111 >                     lock.writeLock().isHeldByCurrentThread());
112 >        assertEquals(t == Thread.currentThread(),
113 >                     lock.getWriteHoldCount() > 0);
114 >        assertEquals(t == Thread.currentThread(),
115 >                     lock.writeLock().getHoldCount() > 0);
116 >        assertEquals(0, lock.getReadLockCount());
117      }
118  
119      /**
120 <     * getWriteHoldCount returns number of recursive holds
120 >     * Checks that lock is write-locked by the current thread.
121       */
122 <    public void testGetWriteHoldCount() {
123 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
131 <        for(int i = 1; i <= SIZE; i++) {
132 <            lock.writeLock().lock();
133 <            assertEquals(i,lock.getWriteHoldCount());
134 <        }
135 <        for(int i = SIZE; i > 0; i--) {
136 <            lock.writeLock().unlock();
137 <            assertEquals(i-1,lock.getWriteHoldCount());
138 <        }
122 >    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
123 >        assertWriteLockedBy(lock, Thread.currentThread());
124      }
125  
126      /**
127 <     * getReadHoldCount returns number of recursive holds
127 >     * Checks that condition c has no waiters.
128       */
129 <    public void testGetReadHoldCount() {
130 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
146 <        for(int i = 1; i <= SIZE; i++) {
147 <            lock.readLock().lock();
148 <            assertEquals(i,lock.getReadHoldCount());
149 <        }
150 <        for(int i = SIZE; i > 0; i--) {
151 <            lock.readLock().unlock();
152 <            assertEquals(i-1,lock.getReadHoldCount());
153 <        }
129 >    void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
130 >        assertHasWaiters(lock, c, new Thread[] {});
131      }
155    
132  
133      /**
134 <     * write-unlocking an unlocked lock throws IllegalMonitorStateException
134 >     * Checks that condition c has exactly the given waiter threads.
135       */
136 <    public void testUnlock_IllegalMonitorStateException() {
137 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
138 <        try {
139 <            rl.writeLock().unlock();
140 <            shouldThrow();
141 <        } catch(IllegalMonitorStateException success){}
136 >    void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
137 >                          Thread... threads) {
138 >        lock.writeLock().lock();
139 >        assertEquals(threads.length > 0, lock.hasWaiters(c));
140 >        assertEquals(threads.length, lock.getWaitQueueLength(c));
141 >        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
142 >        assertEquals(threads.length, lock.getWaitingThreads(c).size());
143 >        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
144 >                     new HashSet<Thread>(Arrays.asList(threads)));
145 >        lock.writeLock().unlock();
146      }
147  
148 +    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
149  
150      /**
151 <     * write-lockInterruptibly is interruptible
151 >     * Awaits condition using the specified AwaitMethod.
152       */
153 <    public void testWriteLockInterruptibly_Interrupted() {
154 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
155 <        Thread t = new Thread(new Runnable() {
156 <                public void run() {
157 <                    try {
158 <                        lock.writeLock().lockInterruptibly();
159 <                        lock.writeLock().unlock();
160 <                        lock.writeLock().lockInterruptibly();
161 <                        lock.writeLock().unlock();
162 <                    } catch(InterruptedException success){}
163 <                }
164 <            });
165 <        try {
166 <            lock.writeLock().lock();
167 <            t.start();
168 <            t.interrupt();
169 <            lock.writeLock().unlock();
189 <            t.join();
190 <        } catch(Exception e){
191 <            unexpectedException();
153 >    void await(Condition c, AwaitMethod awaitMethod)
154 >            throws InterruptedException {
155 >        switch (awaitMethod) {
156 >        case await:
157 >            c.await();
158 >            break;
159 >        case awaitTimed:
160 >            assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
161 >            break;
162 >        case awaitNanos:
163 >            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
164 >            assertTrue(nanosRemaining > 0);
165 >            break;
166 >        case awaitUntil:
167 >            java.util.Date d = new java.util.Date();
168 >            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
169 >            break;
170          }
171 <    }
171 >    }
172  
173      /**
174 <     * timed write-tryLock is interruptible
174 >     * Constructor sets given fairness, and is in unlocked state
175       */
176 <    public void testWriteTryLock_Interrupted() {
177 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
178 <        lock.writeLock().lock();
179 <        Thread t = new Thread(new Runnable() {
180 <                public void run() {
181 <                    try {
182 <                        lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
183 <                    } catch(InterruptedException success){}
184 <                }
185 <            });
186 <        try {
187 <            t.start();
188 <            t.interrupt();
189 <            lock.writeLock().unlock();
190 <            t.join();
191 <        } catch(Exception e){
192 <            unexpectedException();
215 <        }
176 >    public void testConstructor() {
177 >        PublicReentrantReadWriteLock lock;
178 >
179 >        lock = new PublicReentrantReadWriteLock();
180 >        assertFalse(lock.isFair());
181 >        assertNotWriteLocked(lock);
182 >        assertEquals(0, lock.getReadLockCount());
183 >
184 >        lock = new PublicReentrantReadWriteLock(true);
185 >        assertTrue(lock.isFair());
186 >        assertNotWriteLocked(lock);
187 >        assertEquals(0, lock.getReadLockCount());
188 >
189 >        lock = new PublicReentrantReadWriteLock(false);
190 >        assertFalse(lock.isFair());
191 >        assertNotWriteLocked(lock);
192 >        assertEquals(0, lock.getReadLockCount());
193      }
194  
195      /**
196 <     * read-lockInterruptibly is interruptible
196 >     * write-locking and read-locking an unlocked lock succeed
197       */
198 <    public void testReadLockInterruptibly_Interrupted() {
199 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
200 <        lock.writeLock().lock();
201 <        Thread t = new Thread(new Runnable() {
202 <                public void run() {
203 <                    try {
204 <                        lock.readLock().lockInterruptibly();
205 <                    } catch(InterruptedException success){}
206 <                }
207 <            });
208 <        try {
209 <            t.start();
210 <            t.interrupt();
198 >    public void testLock()      { testLock(false); }
199 >    public void testLock_fair() { testLock(true); }
200 >    public void testLock(boolean fair) {
201 >        PublicReentrantReadWriteLock lock =
202 >            new PublicReentrantReadWriteLock(fair);
203 >        assertNotWriteLocked(lock);
204 >        lock.writeLock().lock();
205 >        assertWriteLockedByMoi(lock);
206 >        lock.writeLock().unlock();
207 >        assertNotWriteLocked(lock);
208 >        assertEquals(0, lock.getReadLockCount());
209 >        lock.readLock().lock();
210 >        assertNotWriteLocked(lock);
211 >        assertEquals(1, lock.getReadLockCount());
212 >        lock.readLock().unlock();
213 >        assertNotWriteLocked(lock);
214 >        assertEquals(0, lock.getReadLockCount());
215 >    }
216 >
217 >    /**
218 >     * getWriteHoldCount returns number of recursive holds
219 >     */
220 >    public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
221 >    public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
222 >    public void testGetWriteHoldCount(boolean fair) {
223 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
224 >        for (int i = 1; i <= SIZE; i++) {
225 >            lock.writeLock().lock();
226 >            assertEquals(i,lock.getWriteHoldCount());
227 >        }
228 >        for (int i = SIZE; i > 0; i--) {
229              lock.writeLock().unlock();
230 <            t.join();
236 <        } catch(Exception e){
237 <            unexpectedException();
230 >            assertEquals(i-1,lock.getWriteHoldCount());
231          }
232 <    }
232 >    }
233  
234      /**
235 <     * timed read-tryLock is interruptible
235 >     * writelock.getHoldCount returns number of recursive holds
236       */
237 <    public void testReadTryLock_Interrupted() {
238 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
239 <        lock.writeLock().lock();
240 <        Thread t = new Thread(new Runnable() {
241 <                public void run() {
242 <                    try {
243 <                        lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
244 <                        threadShouldThrow();
245 <                    } catch(InterruptedException success){}
246 <                }
247 <            });
255 <        try {
256 <            t.start();
257 <            t.interrupt();
258 <            t.join();
259 <        } catch(Exception e){
260 <            unexpectedException();
237 >    public void testGetHoldCount()      { testGetHoldCount(false); }
238 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
239 >    public void testGetHoldCount(boolean fair) {
240 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
241 >        for (int i = 1; i <= SIZE; i++) {
242 >            lock.writeLock().lock();
243 >            assertEquals(i,lock.writeLock().getHoldCount());
244 >        }
245 >        for (int i = SIZE; i > 0; i--) {
246 >            lock.writeLock().unlock();
247 >            assertEquals(i-1,lock.writeLock().getHoldCount());
248          }
249      }
250  
264    
251      /**
252 <     * write-tryLock fails if locked
252 >     * getReadHoldCount returns number of recursive holds
253       */
254 <    public void testWriteTryLockWhenLocked() {
255 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
256 <        lock.writeLock().lock();
257 <        Thread t = new Thread(new Runnable() {
258 <                public void run() {
259 <                    threadAssertFalse(lock.writeLock().tryLock());
260 <                }
261 <            });
262 <        try {
263 <            t.start();
264 <            t.join();
279 <            lock.writeLock().unlock();
280 <        } catch(Exception e){
281 <            unexpectedException();
254 >    public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
255 >    public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
256 >    public void testGetReadHoldCount(boolean fair) {
257 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
258 >        for (int i = 1; i <= SIZE; i++) {
259 >            lock.readLock().lock();
260 >            assertEquals(i,lock.getReadHoldCount());
261 >        }
262 >        for (int i = SIZE; i > 0; i--) {
263 >            lock.readLock().unlock();
264 >            assertEquals(i-1,lock.getReadHoldCount());
265          }
266 <    }
266 >    }
267  
268      /**
269 <     * read-tryLock fails if locked
269 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
270       */
271 <    public void testReadTryLockWhenLocked() {
272 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
273 <        lock.writeLock().lock();
274 <        Thread t = new Thread(new Runnable() {
292 <                public void run() {
293 <                    threadAssertFalse(lock.readLock().tryLock());
294 <                }
295 <            });
271 >    public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
272 >    public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
273 >    public void testWriteUnlock_IMSE(boolean fair) {
274 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
275          try {
297            t.start();
298            t.join();
276              lock.writeLock().unlock();
277 <        } catch(Exception e){
278 <            unexpectedException();
279 <        }
303 <    }
277 >            shouldThrow();
278 >        } catch (IllegalMonitorStateException success) {}
279 >    }
280  
281      /**
282 <     * Multiple threads can hold a read lock when not write-locked
282 >     * read-unlocking an unlocked lock throws IllegalMonitorStateException
283       */
284 <    public void testMultipleReadLocks() {
285 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 <        lock.readLock().lock();
287 <        Thread t = new Thread(new Runnable() {
312 <                public void run() {
313 <                    threadAssertTrue(lock.readLock().tryLock());
314 <                    lock.readLock().unlock();
315 <                }
316 <            });
284 >    public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
285 >    public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
286 >    public void testReadUnlock_IMSE(boolean fair) {
287 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
288          try {
318            t.start();
319            t.join();
289              lock.readLock().unlock();
290 <        } catch(Exception e){
291 <            unexpectedException();
292 <        }
324 <    }
290 >            shouldThrow();
291 >        } catch (IllegalMonitorStateException success) {}
292 >    }
293  
294      /**
295 <     * A writelock succeeds after reading threads unlock
295 >     * write-lockInterruptibly is interruptible
296       */
297 <    public void testWriteAfterMultipleReadLocks() {
298 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
299 <        lock.readLock().lock();
300 <        Thread t1 = new Thread(new Runnable() {
301 <                public void run() {
302 <                    lock.readLock().lock();
303 <                    lock.readLock().unlock();
304 <                }
305 <            });
306 <        Thread t2 = new Thread(new Runnable() {
339 <                public void run() {
340 <                    lock.writeLock().lock();
341 <                    lock.writeLock().unlock();
342 <                }
343 <            });
297 >    public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
298 >    public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
299 >    public void testWriteLockInterruptibly_Interruptible(boolean fair) {
300 >        final PublicReentrantReadWriteLock lock =
301 >            new PublicReentrantReadWriteLock(fair);
302 >        lock.writeLock().lock();
303 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
304 >            public void realRun() throws InterruptedException {
305 >                lock.writeLock().lockInterruptibly();
306 >            }});
307  
308 <        try {
309 <            t1.start();
310 <            t2.start();
311 <            Thread.sleep(SHORT_DELAY_MS);
312 <            lock.readLock().unlock();
350 <            t1.join(MEDIUM_DELAY_MS);
351 <            t2.join(MEDIUM_DELAY_MS);
352 <            assertTrue(!t1.isAlive());
353 <            assertTrue(!t2.isAlive());
354 <          
355 <        } catch(Exception e){
356 <            unexpectedException();
357 <        }
358 <    }
308 >        waitForQueuedThread(lock, t);
309 >        t.interrupt();
310 >        awaitTermination(t);
311 >        releaseWriteLock(lock);
312 >    }
313  
314      /**
315 <     * Readlocks succeed after a writing thread unlocks
315 >     * timed write-tryLock is interruptible
316       */
317 <    public void testReadAfterWriteLock() {
318 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319 <        lock.writeLock().lock();
320 <        Thread t1 = new Thread(new Runnable() {
321 <                public void run() {
322 <                    lock.readLock().lock();
323 <                    lock.readLock().unlock();
324 <                }
325 <            });
326 <        Thread t2 = new Thread(new Runnable() {
327 <                public void run() {
328 <                    lock.readLock().lock();
329 <                    lock.readLock().unlock();
330 <                }
331 <            });
317 >    public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
318 >    public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
319 >    public void testWriteTryLock_Interruptible(boolean fair) {
320 >        final PublicReentrantReadWriteLock lock =
321 >            new PublicReentrantReadWriteLock(fair);
322 >        lock.writeLock().lock();
323 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
324 >            public void realRun() throws InterruptedException {
325 >                lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
326 >            }});
327 >
328 >        waitForQueuedThread(lock, t);
329 >        t.interrupt();
330 >        awaitTermination(t);
331 >        releaseWriteLock(lock);
332 >    }
333  
334 <        try {
335 <            t1.start();
336 <            t2.start();
337 <            Thread.sleep(SHORT_DELAY_MS);
338 <            lock.writeLock().unlock();
339 <            t1.join(MEDIUM_DELAY_MS);
340 <            t2.join(MEDIUM_DELAY_MS);
341 <            assertTrue(!t1.isAlive());
342 <            assertTrue(!t2.isAlive());
343 <          
344 <        } catch(Exception e){
345 <            unexpectedException();
346 <        }
347 <    }
334 >    /**
335 >     * read-lockInterruptibly is interruptible
336 >     */
337 >    public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
338 >    public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
339 >    public void testReadLockInterruptibly_Interruptible(boolean fair) {
340 >        final PublicReentrantReadWriteLock lock =
341 >            new PublicReentrantReadWriteLock(fair);
342 >        lock.writeLock().lock();
343 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
344 >            public void realRun() throws InterruptedException {
345 >                lock.readLock().lockInterruptibly();
346 >            }});
347 >
348 >        waitForQueuedThread(lock, t);
349 >        t.interrupt();
350 >        awaitTermination(t);
351 >        releaseWriteLock(lock);
352 >    }
353  
354      /**
355 <     * Read trylock succeeds if write locked by current thread
355 >     * timed read-tryLock is interruptible
356       */
357 <    public void testReadHoldingWriteLock() {
358 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
359 <        lock.writeLock().lock();
360 <        assertTrue(lock.readLock().tryLock());
361 <        lock.readLock().unlock();
357 >    public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
358 >    public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
359 >    public void testReadTryLock_Interruptible(boolean fair) {
360 >        final PublicReentrantReadWriteLock lock =
361 >            new PublicReentrantReadWriteLock(fair);
362 >        lock.writeLock().lock();
363 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
364 >            public void realRun() throws InterruptedException {
365 >                lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
366 >            }});
367 >
368 >        waitForQueuedThread(lock, t);
369 >        t.interrupt();
370 >        awaitTermination(t);
371 >        releaseWriteLock(lock);
372 >    }
373 >
374 >    /**
375 >     * write-tryLock on an unlocked lock succeeds
376 >     */
377 >    public void testWriteTryLock()      { testWriteTryLock(false); }
378 >    public void testWriteTryLock_fair() { testWriteTryLock(true); }
379 >    public void testWriteTryLock(boolean fair) {
380 >        final PublicReentrantReadWriteLock lock =
381 >            new PublicReentrantReadWriteLock(fair);
382 >        assertTrue(lock.writeLock().tryLock());
383 >        assertWriteLockedByMoi(lock);
384 >        assertTrue(lock.writeLock().tryLock());
385 >        assertWriteLockedByMoi(lock);
386          lock.writeLock().unlock();
387 <    }
387 >        releaseWriteLock(lock);
388 >    }
389  
390      /**
391 <     * Read lock succeeds if write locked by current thread even if
407 <     * other threads are waiting for readlock
391 >     * write-tryLock fails if locked
392       */
393 <    public void testReadHoldingWriteLock2() {
394 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
395 <        lock.writeLock().lock();
396 <        Thread t1 = new Thread(new Runnable() {
397 <                public void run() {
398 <                    lock.readLock().lock();
399 <                    lock.readLock().unlock();
400 <                }
401 <            });
402 <        Thread t2 = new Thread(new Runnable() {
419 <                public void run() {
420 <                    lock.readLock().lock();
421 <                    lock.readLock().unlock();
422 <                }
423 <            });
393 >    public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
394 >    public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
395 >    public void testWriteTryLockWhenLocked(boolean fair) {
396 >        final PublicReentrantReadWriteLock lock =
397 >            new PublicReentrantReadWriteLock(fair);
398 >        lock.writeLock().lock();
399 >        Thread t = newStartedThread(new CheckedRunnable() {
400 >            public void realRun() {
401 >                assertFalse(lock.writeLock().tryLock());
402 >            }});
403  
404 <        try {
405 <            t1.start();
406 <            t2.start();
428 <            lock.readLock().lock();
429 <            lock.readLock().unlock();
430 <            Thread.sleep(SHORT_DELAY_MS);
431 <            lock.readLock().lock();
432 <            lock.readLock().unlock();
433 <            lock.writeLock().unlock();
434 <            t1.join(MEDIUM_DELAY_MS);
435 <            t2.join(MEDIUM_DELAY_MS);
436 <            assertTrue(!t1.isAlive());
437 <            assertTrue(!t2.isAlive());
438 <          
439 <        } catch(Exception e){
440 <            unexpectedException();
441 <        }
442 <    }
404 >        awaitTermination(t);
405 >        releaseWriteLock(lock);
406 >    }
407  
408      /**
409 <     *  Read lock succeeds if write locked by current thread even if
446 <     * other threads are waiting for writelock
409 >     * read-tryLock fails if locked
410       */
411 <    public void testReadHoldingWriteLock3() {
412 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
413 <        lock.writeLock().lock();
414 <        Thread t1 = new Thread(new Runnable() {
415 <                public void run() {
416 <                    lock.writeLock().lock();
417 <                    lock.writeLock().unlock();
418 <                }
419 <            });
420 <        Thread t2 = new Thread(new Runnable() {
458 <                public void run() {
459 <                    lock.writeLock().lock();
460 <                    lock.writeLock().unlock();
461 <                }
462 <            });
411 >    public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
412 >    public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
413 >    public void testReadTryLockWhenLocked(boolean fair) {
414 >        final PublicReentrantReadWriteLock lock =
415 >            new PublicReentrantReadWriteLock(fair);
416 >        lock.writeLock().lock();
417 >        Thread t = newStartedThread(new CheckedRunnable() {
418 >            public void realRun() {
419 >                assertFalse(lock.readLock().tryLock());
420 >            }});
421  
422 <        try {
423 <            t1.start();
424 <            t2.start();
467 <            lock.readLock().lock();
468 <            lock.readLock().unlock();
469 <            Thread.sleep(SHORT_DELAY_MS);
470 <            lock.readLock().lock();
471 <            lock.readLock().unlock();
472 <            lock.writeLock().unlock();
473 <            t1.join(MEDIUM_DELAY_MS);
474 <            t2.join(MEDIUM_DELAY_MS);
475 <            assertTrue(!t1.isAlive());
476 <            assertTrue(!t2.isAlive());
477 <          
478 <        } catch(Exception e){
479 <            unexpectedException();
480 <        }
481 <    }
422 >        awaitTermination(t);
423 >        releaseWriteLock(lock);
424 >    }
425  
426 +    /**
427 +     * Multiple threads can hold a read lock when not write-locked
428 +     */
429 +    public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
430 +    public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
431 +    public void testMultipleReadLocks(boolean fair) {
432 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
433 +        lock.readLock().lock();
434 +        Thread t = newStartedThread(new CheckedRunnable() {
435 +            public void realRun() throws InterruptedException {
436 +                assertTrue(lock.readLock().tryLock());
437 +                lock.readLock().unlock();
438 +                assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
439 +                lock.readLock().unlock();
440 +                lock.readLock().lock();
441 +                lock.readLock().unlock();
442 +            }});
443 +
444 +        awaitTermination(t);
445 +        lock.readLock().unlock();
446 +    }
447  
448      /**
449 <     *  Write lock succeeds if write locked by current thread even if
486 <     * other threads are waiting for writelock
449 >     * A writelock succeeds only after a reading thread unlocks
450       */
451 <    public void testWriteHoldingWriteLock4() {
452 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
453 <        lock.writeLock().lock();
454 <        Thread t1 = new Thread(new Runnable() {
455 <                public void run() {
456 <                    lock.writeLock().lock();
457 <                    lock.writeLock().unlock();
458 <                }
459 <            });
460 <        Thread t2 = new Thread(new Runnable() {
461 <                public void run() {
462 <                    lock.writeLock().lock();
463 <                    lock.writeLock().unlock();
464 <                }
465 <            });
451 >    public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
452 >    public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
453 >    public void testWriteAfterReadLock(boolean fair) {
454 >        final PublicReentrantReadWriteLock lock =
455 >            new PublicReentrantReadWriteLock(fair);
456 >        lock.readLock().lock();
457 >        Thread t = newStartedThread(new CheckedRunnable() {
458 >            public void realRun() {
459 >                assertEquals(1, lock.getReadLockCount());
460 >                lock.writeLock().lock();
461 >                assertEquals(0, lock.getReadLockCount());
462 >                lock.writeLock().unlock();
463 >            }});
464 >        waitForQueuedThread(lock, t);
465 >        assertNotWriteLocked(lock);
466 >        assertEquals(1, lock.getReadLockCount());
467 >        lock.readLock().unlock();
468 >        assertEquals(0, lock.getReadLockCount());
469 >        awaitTermination(t);
470 >        assertNotWriteLocked(lock);
471 >    }
472  
473 <        try {
474 <            t1.start();
475 <            t2.start();
476 <            lock.writeLock().lock();
477 <            lock.writeLock().unlock();
478 <            Thread.sleep(SHORT_DELAY_MS);
479 <            lock.writeLock().lock();
480 <            lock.writeLock().unlock();
481 <            lock.writeLock().unlock();
482 <            t1.join(MEDIUM_DELAY_MS);
483 <            t2.join(MEDIUM_DELAY_MS);
484 <            assertTrue(!t1.isAlive());
485 <            assertTrue(!t2.isAlive());
486 <          
487 <        } catch(Exception e){
488 <            unexpectedException();
489 <        }
490 <    }
473 >    /**
474 >     * A writelock succeeds only after reading threads unlock
475 >     */
476 >    public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
477 >    public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
478 >    public void testWriteAfterMultipleReadLocks(boolean fair) {
479 >        final PublicReentrantReadWriteLock lock =
480 >            new PublicReentrantReadWriteLock(fair);
481 >        lock.readLock().lock();
482 >        lock.readLock().lock();
483 >        Thread t1 = newStartedThread(new CheckedRunnable() {
484 >            public void realRun() {
485 >                lock.readLock().lock();
486 >                assertEquals(3, lock.getReadLockCount());
487 >                lock.readLock().unlock();
488 >            }});
489 >        awaitTermination(t1);
490 >
491 >        Thread t2 = newStartedThread(new CheckedRunnable() {
492 >            public void realRun() {
493 >                assertEquals(2, lock.getReadLockCount());
494 >                lock.writeLock().lock();
495 >                assertEquals(0, lock.getReadLockCount());
496 >                lock.writeLock().unlock();
497 >            }});
498 >        waitForQueuedThread(lock, t2);
499 >        assertNotWriteLocked(lock);
500 >        assertEquals(2, lock.getReadLockCount());
501 >        lock.readLock().unlock();
502 >        lock.readLock().unlock();
503 >        assertEquals(0, lock.getReadLockCount());
504 >        awaitTermination(t2);
505 >        assertNotWriteLocked(lock);
506 >    }
507 >
508 >    /**
509 >     * A thread that tries to acquire a fair read lock (non-reentrantly)
510 >     * will block if there is a waiting writer thread
511 >     */
512 >    public void testReaderWriterReaderFairFifo() {
513 >        final PublicReentrantReadWriteLock lock =
514 >            new PublicReentrantReadWriteLock(true);
515 >        final AtomicBoolean t1GotLock = new AtomicBoolean(false);
516 >
517 >        lock.readLock().lock();
518 >        Thread t1 = newStartedThread(new CheckedRunnable() {
519 >            public void realRun() {
520 >                assertEquals(1, lock.getReadLockCount());
521 >                lock.writeLock().lock();
522 >                assertEquals(0, lock.getReadLockCount());
523 >                t1GotLock.set(true);
524 >                lock.writeLock().unlock();
525 >            }});
526 >        waitForQueuedThread(lock, t1);
527  
528 +        Thread t2 = newStartedThread(new CheckedRunnable() {
529 +            public void realRun() {
530 +                assertEquals(1, lock.getReadLockCount());
531 +                lock.readLock().lock();
532 +                assertEquals(1, lock.getReadLockCount());
533 +                assertTrue(t1GotLock.get());
534 +                lock.readLock().unlock();
535 +            }});
536 +        waitForQueuedThread(lock, t2);
537 +        assertTrue(t1.isAlive());
538 +        assertNotWriteLocked(lock);
539 +        assertEquals(1, lock.getReadLockCount());
540 +        lock.readLock().unlock();
541 +        awaitTermination(t1);
542 +        awaitTermination(t2);
543 +        assertNotWriteLocked(lock);
544 +    }
545 +
546 +    /**
547 +     * Readlocks succeed only after a writing thread unlocks
548 +     */
549 +    public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
550 +    public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
551 +    public void testReadAfterWriteLock(boolean fair) {
552 +        final PublicReentrantReadWriteLock lock =
553 +            new PublicReentrantReadWriteLock(fair);
554 +        lock.writeLock().lock();
555 +        Thread t1 = newStartedThread(new CheckedRunnable() {
556 +            public void realRun() {
557 +                lock.readLock().lock();
558 +                lock.readLock().unlock();
559 +            }});
560 +        Thread t2 = newStartedThread(new CheckedRunnable() {
561 +            public void realRun() {
562 +                lock.readLock().lock();
563 +                lock.readLock().unlock();
564 +            }});
565 +
566 +        waitForQueuedThread(lock, t1);
567 +        waitForQueuedThread(lock, t2);
568 +        releaseWriteLock(lock);
569 +        awaitTermination(t1);
570 +        awaitTermination(t2);
571 +    }
572  
573      /**
574 <     * Fair Read trylock succeeds if write locked by current thread
574 >     * Read trylock succeeds if write locked by current thread
575       */
576 <    public void testReadHoldingWriteLockFair() {
577 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
578 <        lock.writeLock().lock();
576 >    public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
577 >    public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
578 >    public void testReadHoldingWriteLock(boolean fair) {
579 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
580 >        lock.writeLock().lock();
581          assertTrue(lock.readLock().tryLock());
582          lock.readLock().unlock();
583          lock.writeLock().unlock();
584 <    }
584 >    }
585  
586      /**
587 <     * Fair Read lock succeeds if write locked by current thread even if
588 <     * other threads are waiting for readlock
587 >     * Read trylock succeeds (barging) even in the presence of waiting
588 >     * readers and/or writers
589       */
590 <    public void testReadHoldingWriteLockFair2() {
591 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
592 <        lock.writeLock().lock();
593 <        Thread t1 = new Thread(new Runnable() {
594 <                public void run() {
595 <                    lock.readLock().lock();
545 <                    lock.readLock().unlock();
546 <                }
547 <            });
548 <        Thread t2 = new Thread(new Runnable() {
549 <                public void run() {
550 <                    lock.readLock().lock();
551 <                    lock.readLock().unlock();
552 <                }
553 <            });
590 >    public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
591 >    public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
592 >    public void testReadTryLockBarging(boolean fair) {
593 >        final PublicReentrantReadWriteLock lock =
594 >            new PublicReentrantReadWriteLock(fair);
595 >        lock.readLock().lock();
596  
597 <        try {
598 <            t1.start();
599 <            t2.start();
600 <            lock.readLock().lock();
601 <            lock.readLock().unlock();
602 <            Thread.sleep(SHORT_DELAY_MS);
603 <            lock.readLock().lock();
604 <            lock.readLock().unlock();
605 <            lock.writeLock().unlock();
606 <            t1.join(MEDIUM_DELAY_MS);
607 <            t2.join(MEDIUM_DELAY_MS);
608 <            assertTrue(!t1.isAlive());
609 <            assertTrue(!t2.isAlive());
610 <          
611 <        } catch(Exception e){
612 <            unexpectedException();
613 <        }
614 <    }
597 >        Thread t1 = newStartedThread(new CheckedRunnable() {
598 >            public void realRun() {
599 >                lock.writeLock().lock();
600 >                lock.writeLock().unlock();
601 >            }});
602 >
603 >        waitForQueuedThread(lock, t1);
604 >
605 >        Thread t2 = newStartedThread(new CheckedRunnable() {
606 >            public void realRun() {
607 >                lock.readLock().lock();
608 >                lock.readLock().unlock();
609 >            }});
610 >
611 >        if (fair)
612 >            waitForQueuedThread(lock, t2);
613 >
614 >        Thread t3 = newStartedThread(new CheckedRunnable() {
615 >            public void realRun() {
616 >                lock.readLock().tryLock();
617 >                lock.readLock().unlock();
618 >            }});
619 >
620 >        assertTrue(lock.getReadLockCount() > 0);
621 >        awaitTermination(t3);
622 >        assertTrue(t1.isAlive());
623 >        if (fair) assertTrue(t2.isAlive());
624 >        lock.readLock().unlock();
625 >        awaitTermination(t1);
626 >        awaitTermination(t2);
627 >    }
628 >
629 >    /**
630 >     * Read lock succeeds if write locked by current thread even if
631 >     * other threads are waiting for readlock
632 >     */
633 >    public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
634 >    public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
635 >    public void testReadHoldingWriteLock2(boolean fair) {
636 >        final PublicReentrantReadWriteLock lock =
637 >            new PublicReentrantReadWriteLock(fair);
638 >        lock.writeLock().lock();
639 >        lock.readLock().lock();
640 >        lock.readLock().unlock();
641  
642 +        Thread t1 = newStartedThread(new CheckedRunnable() {
643 +            public void realRun() {
644 +                lock.readLock().lock();
645 +                lock.readLock().unlock();
646 +            }});
647 +        Thread t2 = newStartedThread(new CheckedRunnable() {
648 +            public void realRun() {
649 +                lock.readLock().lock();
650 +                lock.readLock().unlock();
651 +            }});
652 +
653 +        waitForQueuedThread(lock, t1);
654 +        waitForQueuedThread(lock, t2);
655 +        assertWriteLockedByMoi(lock);
656 +        lock.readLock().lock();
657 +        lock.readLock().unlock();
658 +        releaseWriteLock(lock);
659 +        awaitTermination(t1);
660 +        awaitTermination(t2);
661 +    }
662  
663      /**
664 <     * Fair Read lock succeeds if write locked by current thread even if
664 >     * Read lock succeeds if write locked by current thread even if
665       * other threads are waiting for writelock
666       */
667 <    public void testReadHoldingWriteLockFair3() {
668 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
669 <        lock.writeLock().lock();
670 <        Thread t1 = new Thread(new Runnable() {
671 <                public void run() {
672 <                    lock.writeLock().lock();
673 <                    lock.writeLock().unlock();
674 <                }
587 <            });
588 <        Thread t2 = new Thread(new Runnable() {
589 <                public void run() {
590 <                    lock.writeLock().lock();
591 <                    lock.writeLock().unlock();
592 <                }
593 <            });
667 >    public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
668 >    public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
669 >    public void testReadHoldingWriteLock3(boolean fair) {
670 >        final PublicReentrantReadWriteLock lock =
671 >            new PublicReentrantReadWriteLock(fair);
672 >        lock.writeLock().lock();
673 >        lock.readLock().lock();
674 >        lock.readLock().unlock();
675  
676 <        try {
677 <            t1.start();
678 <            t2.start();
679 <            lock.readLock().lock();
680 <            lock.readLock().unlock();
681 <            Thread.sleep(SHORT_DELAY_MS);
682 <            lock.readLock().lock();
683 <            lock.readLock().unlock();
684 <            lock.writeLock().unlock();
685 <            t1.join(MEDIUM_DELAY_MS);
605 <            t2.join(MEDIUM_DELAY_MS);
606 <            assertTrue(!t1.isAlive());
607 <            assertTrue(!t2.isAlive());
608 <          
609 <        } catch(Exception e){
610 <            unexpectedException();
611 <        }
612 <    }
676 >        Thread t1 = newStartedThread(new CheckedRunnable() {
677 >            public void realRun() {
678 >                lock.writeLock().lock();
679 >                lock.writeLock().unlock();
680 >            }});
681 >        Thread t2 = newStartedThread(new CheckedRunnable() {
682 >            public void realRun() {
683 >                lock.writeLock().lock();
684 >                lock.writeLock().unlock();
685 >            }});
686  
687 +        waitForQueuedThread(lock, t1);
688 +        waitForQueuedThread(lock, t2);
689 +        assertWriteLockedByMoi(lock);
690 +        lock.readLock().lock();
691 +        lock.readLock().unlock();
692 +        assertWriteLockedByMoi(lock);
693 +        lock.writeLock().unlock();
694 +        awaitTermination(t1);
695 +        awaitTermination(t2);
696 +    }
697  
698      /**
699 <     * Fair Write lock succeeds if write locked by current thread even if
699 >     * Write lock succeeds if write locked by current thread even if
700       * other threads are waiting for writelock
701       */
702 <    public void testWriteHoldingWriteLockFair4() {
703 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
704 <        lock.writeLock().lock();
705 <        Thread t1 = new Thread(new Runnable() {
706 <                public void run() {
707 <                    lock.writeLock().lock();
708 <                    lock.writeLock().unlock();
709 <                }
627 <            });
628 <        Thread t2 = new Thread(new Runnable() {
629 <                public void run() {
630 <                    lock.writeLock().lock();
631 <                    lock.writeLock().unlock();
632 <                }
633 <            });
702 >    public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
703 >    public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
704 >    public void testWriteHoldingWriteLock4(boolean fair) {
705 >        final PublicReentrantReadWriteLock lock =
706 >            new PublicReentrantReadWriteLock(fair);
707 >        lock.writeLock().lock();
708 >        lock.writeLock().lock();
709 >        lock.writeLock().unlock();
710  
711 <        try {
712 <            t1.start();
713 <            t2.start();
714 <            Thread.sleep(SHORT_DELAY_MS);
715 <            assertTrue(lock.isWriteLockedByCurrentThread());
716 <            assertTrue(lock.getWriteHoldCount() == 1);
717 <            lock.writeLock().lock();
718 <            assertTrue(lock.getWriteHoldCount() == 2);
719 <            lock.writeLock().unlock();
720 <            lock.writeLock().lock();
645 <            lock.writeLock().unlock();
646 <            lock.writeLock().unlock();
647 <            t1.join(MEDIUM_DELAY_MS);
648 <            t2.join(MEDIUM_DELAY_MS);
649 <            assertTrue(!t1.isAlive());
650 <            assertTrue(!t2.isAlive());
651 <          
652 <        } catch(Exception e){
653 <            unexpectedException();
654 <        }
655 <    }
711 >        Thread t1 = newStartedThread(new CheckedRunnable() {
712 >            public void realRun() {
713 >                lock.writeLock().lock();
714 >                lock.writeLock().unlock();
715 >            }});
716 >        Thread t2 = newStartedThread(new CheckedRunnable() {
717 >            public void realRun() {
718 >                lock.writeLock().lock();
719 >                lock.writeLock().unlock();
720 >            }});
721  
722 +        waitForQueuedThread(lock, t1);
723 +        waitForQueuedThread(lock, t2);
724 +        assertWriteLockedByMoi(lock);
725 +        assertEquals(1, lock.getWriteHoldCount());
726 +        lock.writeLock().lock();
727 +        assertWriteLockedByMoi(lock);
728 +        assertEquals(2, lock.getWriteHoldCount());
729 +        lock.writeLock().unlock();
730 +        assertWriteLockedByMoi(lock);
731 +        assertEquals(1, lock.getWriteHoldCount());
732 +        lock.writeLock().unlock();
733 +        awaitTermination(t1);
734 +        awaitTermination(t2);
735 +    }
736  
737      /**
738       * Read tryLock succeeds if readlocked but not writelocked
739       */
740 <    public void testTryLockWhenReadLocked() {
741 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
742 <        lock.readLock().lock();
743 <        Thread t = new Thread(new Runnable() {
744 <                public void run() {
745 <                    threadAssertTrue(lock.readLock().tryLock());
746 <                    lock.readLock().unlock();
747 <                }
748 <            });
749 <        try {
671 <            t.start();
672 <            t.join();
673 <            lock.readLock().unlock();
674 <        } catch(Exception e){
675 <            unexpectedException();
676 <        }
677 <    }
740 >    public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
741 >    public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
742 >    public void testTryLockWhenReadLocked(boolean fair) {
743 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
744 >        lock.readLock().lock();
745 >        Thread t = newStartedThread(new CheckedRunnable() {
746 >            public void realRun() {
747 >                assertTrue(lock.readLock().tryLock());
748 >                lock.readLock().unlock();
749 >            }});
750  
751 <    
751 >        awaitTermination(t);
752 >        lock.readLock().unlock();
753 >    }
754  
755      /**
756       * write tryLock fails when readlocked
757       */
758 <    public void testWriteTryLockWhenReadLocked() {
759 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
760 <        lock.readLock().lock();
761 <        Thread t = new Thread(new Runnable() {
762 <                public void run() {
763 <                    threadAssertFalse(lock.writeLock().tryLock());
764 <                }
765 <            });
766 <        try {
693 <            t.start();
694 <            t.join();
695 <            lock.readLock().unlock();
696 <        } catch(Exception e){
697 <            unexpectedException();
698 <        }
699 <    }
758 >    public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
759 >    public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
760 >    public void testWriteTryLockWhenReadLocked(boolean fair) {
761 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
762 >        lock.readLock().lock();
763 >        Thread t = newStartedThread(new CheckedRunnable() {
764 >            public void realRun() {
765 >                assertFalse(lock.writeLock().tryLock());
766 >            }});
767  
768 <    
768 >        awaitTermination(t);
769 >        lock.readLock().unlock();
770 >    }
771  
772      /**
773       * write timed tryLock times out if locked
774       */
775 <    public void testWriteTryLock_Timeout() {
776 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
777 <        lock.writeLock().lock();
778 <        Thread t = new Thread(new Runnable() {
779 <                public void run() {
780 <                    try {
781 <                        threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
782 <                    } catch (Exception ex) {
783 <                        threadUnexpectedException();
784 <                    }
785 <                }
786 <            });
787 <        try {
788 <            t.start();
789 <            t.join();
790 <            lock.writeLock().unlock();
791 <        } catch(Exception e){
723 <            unexpectedException();
724 <        }
725 <    }
775 >    public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
776 >    public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
777 >    public void testWriteTryLock_Timeout(boolean fair) {
778 >        final PublicReentrantReadWriteLock lock =
779 >            new PublicReentrantReadWriteLock(fair);
780 >        lock.writeLock().lock();
781 >        Thread t = newStartedThread(new CheckedRunnable() {
782 >            public void realRun() throws InterruptedException {
783 >                long startTime = System.nanoTime();
784 >                long timeoutMillis = 10;
785 >                assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
786 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
787 >            }});
788 >
789 >        awaitTermination(t);
790 >        releaseWriteLock(lock);
791 >    }
792  
793      /**
794       * read timed tryLock times out if write-locked
795       */
796 <    public void testReadTryLock_Timeout() {
797 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
798 <        lock.writeLock().lock();
799 <        Thread t = new Thread(new Runnable() {
800 <                public void run() {
801 <                    try {
802 <                        threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
803 <                    } catch (Exception ex) {
804 <                        threadUnexpectedException();
805 <                    }
806 <                }
807 <            });
742 <        try {
743 <            t.start();
744 <            t.join();
745 <            lock.writeLock().unlock();
746 <        } catch(Exception e){
747 <            unexpectedException();
748 <        }
749 <    }
796 >    public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
797 >    public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
798 >    public void testReadTryLock_Timeout(boolean fair) {
799 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
800 >        lock.writeLock().lock();
801 >        Thread t = newStartedThread(new CheckedRunnable() {
802 >            public void realRun() throws InterruptedException {
803 >                long startTime = System.nanoTime();
804 >                long timeoutMillis = 10;
805 >                assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
806 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
807 >            }});
808  
809 +        awaitTermination(t);
810 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
811 +        lock.writeLock().unlock();
812 +    }
813  
814      /**
815 <     * write lockInterruptibly succeeds if lock free else is interruptible
815 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
816       */
817 <    public void testWriteLockInterruptibly() {
818 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
819 <        try {
820 <            lock.writeLock().lockInterruptibly();
821 <        } catch(Exception e) {
760 <            unexpectedException();
761 <        }
762 <        Thread t = new Thread(new Runnable() {
763 <                public void run() {
764 <                    try {
765 <                        lock.writeLock().lockInterruptibly();
766 <                        threadShouldThrow();
767 <                    }
768 <                    catch(InterruptedException success) {
769 <                    }
770 <                }
771 <            });
817 >    public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
818 >    public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
819 >    public void testWriteLockInterruptibly(boolean fair) {
820 >        final PublicReentrantReadWriteLock lock =
821 >            new PublicReentrantReadWriteLock(fair);
822          try {
823 <            t.start();
824 <            t.interrupt();
825 <            t.join();
776 <            lock.writeLock().unlock();
777 <        } catch(Exception e){
778 <            unexpectedException();
823 >            lock.writeLock().lockInterruptibly();
824 >        } catch (InterruptedException ie) {
825 >            threadUnexpectedException(ie);
826          }
827 +        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
828 +            public void realRun() throws InterruptedException {
829 +                lock.writeLock().lockInterruptibly();
830 +            }});
831 +
832 +        waitForQueuedThread(lock, t);
833 +        t.interrupt();
834 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
835 +        awaitTermination(t);
836 +        releaseWriteLock(lock);
837      }
838  
839      /**
840 <     *  read lockInterruptibly succeeds if lock free else is interruptible
840 >     * read lockInterruptibly succeeds if lock free else is interruptible
841       */
842 <    public void testReadLockInterruptibly() {
843 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
844 <        try {
845 <            lock.writeLock().lockInterruptibly();
846 <        } catch(Exception e) {
790 <            unexpectedException();
791 <        }
792 <        Thread t = new Thread(new Runnable() {
793 <                public void run() {
794 <                    try {
795 <                        lock.readLock().lockInterruptibly();
796 <                        threadShouldThrow();
797 <                    }
798 <                    catch(InterruptedException success) {
799 <                    }
800 <                }
801 <            });
842 >    public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
843 >    public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
844 >    public void testReadLockInterruptibly(boolean fair) {
845 >        final PublicReentrantReadWriteLock lock =
846 >            new PublicReentrantReadWriteLock(fair);
847          try {
848 <            t.start();
849 <            t.interrupt();
850 <            t.join();
851 <            lock.writeLock().unlock();
852 <        } catch(Exception e){
808 <            unexpectedException();
848 >            lock.readLock().lockInterruptibly();
849 >            lock.readLock().unlock();
850 >            lock.writeLock().lockInterruptibly();
851 >        } catch (InterruptedException ie) {
852 >            threadUnexpectedException(ie);
853          }
854 +        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
855 +            public void realRun() throws InterruptedException {
856 +                lock.readLock().lockInterruptibly();
857 +            }});
858 +
859 +        waitForQueuedThread(lock, t);
860 +        t.interrupt();
861 +        awaitTermination(t);
862 +        releaseWriteLock(lock);
863      }
864  
865      /**
866       * Calling await without holding lock throws IllegalMonitorStateException
867       */
868 <    public void testAwait_IllegalMonitor() {
869 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
868 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
869 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
870 >    public void testAwait_IMSE(boolean fair) {
871 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
872          final Condition c = lock.writeLock().newCondition();
873 <        try {
874 <            c.await();
875 <            shouldThrow();
876 <        }
877 <        catch (IllegalMonitorStateException success) {
878 <        }
879 <        catch (Exception ex) {
880 <            shouldThrow();
873 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
874 >            long startTime = System.nanoTime();
875 >            try {
876 >                await(c, awaitMethod);
877 >                shouldThrow();
878 >            } catch (IllegalMonitorStateException success) {
879 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
880 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
881          }
882      }
883  
884      /**
885       * Calling signal without holding lock throws IllegalMonitorStateException
886       */
887 <    public void testSignal_IllegalMonitor() {
888 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
887 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
888 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
889 >    public void testSignal_IMSE(boolean fair) {
890 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
891          final Condition c = lock.writeLock().newCondition();
892          try {
893              c.signal();
894              shouldThrow();
895 <        }
839 <        catch (IllegalMonitorStateException success) {
840 <        }
841 <        catch (Exception ex) {
842 <            unexpectedException();
843 <        }
895 >        } catch (IllegalMonitorStateException success) {}
896      }
897  
898      /**
899 <     * awaitNanos without a signal times out
899 >     * Calling signalAll without holding lock throws IllegalMonitorStateException
900       */
901 <    public void testAwaitNanos_Timeout() {
902 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
901 >    public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
902 >    public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
903 >    public void testSignalAll_IMSE(boolean fair) {
904 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
905          final Condition c = lock.writeLock().newCondition();
906          try {
907 +            c.signalAll();
908 +            shouldThrow();
909 +        } catch (IllegalMonitorStateException success) {}
910 +    }
911 +
912 +    /**
913 +     * awaitNanos without a signal times out
914 +     */
915 +    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
916 +    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
917 +    public void testAwaitNanos_Timeout(boolean fair) {
918 +        try {
919 +            final ReentrantReadWriteLock lock =
920 +                new ReentrantReadWriteLock(fair);
921 +            final Condition c = lock.writeLock().newCondition();
922              lock.writeLock().lock();
923 <            long t = c.awaitNanos(100);
924 <            assertTrue(t <= 0);
923 >            long startTime = System.nanoTime();
924 >            long timeoutMillis = 10;
925 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
926 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
927 >            assertTrue(nanosRemaining <= 0);
928 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
929              lock.writeLock().unlock();
930 <        }
931 <        catch (Exception ex) {
859 <            unexpectedException();
930 >        } catch (InterruptedException e) {
931 >            threadUnexpectedException(e);
932          }
933      }
934  
863
935      /**
936 <     *  timed await without a signal times out
936 >     * timed await without a signal times out
937       */
938 <    public void testAwait_Timeout() {
939 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
940 <        final Condition c = lock.writeLock().newCondition();
938 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
939 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
940 >    public void testAwait_Timeout(boolean fair) {
941          try {
942 +            final ReentrantReadWriteLock lock =
943 +                new ReentrantReadWriteLock(fair);
944 +            final Condition c = lock.writeLock().newCondition();
945              lock.writeLock().lock();
946 +            long startTime = System.nanoTime();
947 +            long timeoutMillis = 10;
948 +            assertFalse(c.await(timeoutMillis, MILLISECONDS));
949 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
950              lock.writeLock().unlock();
951 <        }
952 <        catch (Exception ex) {
875 <            unexpectedException();
951 >        } catch (InterruptedException e) {
952 >            threadUnexpectedException(e);
953          }
954      }
955  
956      /**
957       * awaitUntil without a signal times out
958       */
959 <    public void testAwaitUntil_Timeout() {
960 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
961 <        final Condition c = lock.writeLock().newCondition();
962 <        try {
959 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
960 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
961 >    public void testAwaitUntil_Timeout(boolean fair) {
962 >        try {
963 >            final ReentrantReadWriteLock lock =
964 >                new ReentrantReadWriteLock(fair);
965 >            final Condition c = lock.writeLock().newCondition();
966              lock.writeLock().lock();
967 +            long startTime = System.nanoTime();
968 +            long timeoutMillis = 10;
969              java.util.Date d = new java.util.Date();
970 +            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
971 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
972              lock.writeLock().unlock();
973 <        }
974 <        catch (Exception ex) {
891 <            unexpectedException();
973 >        } catch (InterruptedException e) {
974 >            threadUnexpectedException(e);
975          }
976      }
977  
978      /**
979       * await returns when signalled
980       */
981 <    public void testAwait() {
982 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
981 >    public void testAwait()      { testAwait(false); }
982 >    public void testAwait_fair() { testAwait(true); }
983 >    public void testAwait(boolean fair) {
984 >        final PublicReentrantReadWriteLock lock =
985 >            new PublicReentrantReadWriteLock(fair);
986          final Condition c = lock.writeLock().newCondition();
987 <        Thread t = new Thread(new Runnable() {
988 <                public void run() {
989 <                    try {
990 <                        lock.writeLock().lock();
991 <                        c.await();
992 <                        lock.writeLock().unlock();
993 <                    }
994 <                    catch(InterruptedException e) {
909 <                        threadUnexpectedException();
910 <                    }
911 <                }
912 <            });
913 <
914 <        try {
915 <            t.start();
916 <            Thread.sleep(SHORT_DELAY_MS);
917 <            lock.writeLock().lock();
918 <            c.signal();
919 <            lock.writeLock().unlock();
920 <            t.join(SHORT_DELAY_MS);
921 <            assertFalse(t.isAlive());
922 <        }
923 <        catch (Exception ex) {
924 <            unexpectedException();
925 <        }
926 <    }
987 >        final CountDownLatch locked = new CountDownLatch(1);
988 >        Thread t = newStartedThread(new CheckedRunnable() {
989 >            public void realRun() throws InterruptedException {
990 >                lock.writeLock().lock();
991 >                locked.countDown();
992 >                c.await();
993 >                lock.writeLock().unlock();
994 >            }});
995  
996 <    /** A helper class for uninterruptible wait tests */
997 <    class UninterruptableThread extends Thread {
998 <        private Lock lock;
999 <        private Condition c;
1000 <        
1001 <        public volatile boolean canAwake = false;
1002 <        public volatile boolean interrupted = false;
1003 <        public volatile boolean lockStarted = false;
936 <        
937 <        public UninterruptableThread(Lock lock, Condition c) {
938 <            this.lock = lock;
939 <            this.c = c;
940 <        }
941 <        
942 <        public synchronized void run() {
943 <            lock.lock();
944 <            lockStarted = true;
945 <            
946 <            while (!canAwake) {
947 <                c.awaitUninterruptibly();
948 <            }
949 <            
950 <            interrupted = isInterrupted();
951 <            lock.unlock();
952 <        }
996 >        await(locked);
997 >        lock.writeLock().lock();
998 >        assertHasWaiters(lock, c, t);
999 >        c.signal();
1000 >        assertHasNoWaiters(lock, c);
1001 >        assertTrue(t.isAlive());
1002 >        lock.writeLock().unlock();
1003 >        awaitTermination(t);
1004      }
1005  
1006      /**
1007       * awaitUninterruptibly doesn't abort on interrupt
1008       */
1009 <    public void testAwaitUninterruptibly() {
1010 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
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();
1014 <        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1015 <
1016 <        try {
1017 <            thread.start();
1018 <
1019 <            while (!thread.lockStarted) {
1020 <                Thread.sleep(100);
968 <            }
969 <
970 <            lock.writeLock().lock();
971 <            try {
972 <                thread.interrupt();
973 <                thread.canAwake = true;
974 <                c.signal();
975 <            } finally {
1014 >        final CountDownLatch locked = new CountDownLatch(1);
1015 >        Thread t = newStartedThread(new CheckedRunnable() {
1016 >            public void realRun() {
1017 >                lock.writeLock().lock();
1018 >                locked.countDown();
1019 >                c.awaitUninterruptibly();
1020 >                assertTrue(Thread.interrupted());
1021                  lock.writeLock().unlock();
1022 <            }
1022 >            }});
1023  
1024 <            thread.join();
1025 <            assertTrue(thread.interrupted);
1026 <            assertFalse(thread.isAlive());
1027 <        } catch (Exception ex) {
1028 <            unexpectedException();
1029 <        }
1024 >        await(locked);
1025 >        lock.writeLock().lock();
1026 >        lock.writeLock().unlock();
1027 >        t.interrupt();
1028 >        long timeoutMillis = 10;
1029 >        assertThreadStaysAlive(t, timeoutMillis);
1030 >        lock.writeLock().lock();
1031 >        c.signal();
1032 >        lock.writeLock().unlock();
1033 >        awaitTermination(t);
1034      }
1035  
1036      /**
1037 <     * await is interruptible
1037 >     * await/awaitNanos/awaitUntil is interruptible
1038       */
1039 <    public void testAwait_Interrupt() {
1040 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1039 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1040 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1041 >    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1042 >    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1043 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1044 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1045 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
1046 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
1047 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1048 >        final PublicReentrantReadWriteLock lock =
1049 >            new PublicReentrantReadWriteLock(fair);
1050          final Condition c = lock.writeLock().newCondition();
1051 <        Thread t = new Thread(new Runnable() {
1052 <                public void run() {
1053 <                    try {
1054 <                        lock.writeLock().lock();
1055 <                        c.await();
1056 <                        lock.writeLock().unlock();
1057 <                        threadShouldThrow();
1058 <                    }
1059 <                    catch(InterruptedException success) {
1060 <                    }
1061 <                }
1062 <            });
1063 <
1064 <        try {
1065 <            t.start();
1066 <            Thread.sleep(SHORT_DELAY_MS);
1067 <            t.interrupt();
1068 <            t.join(SHORT_DELAY_MS);
1069 <            assertFalse(t.isAlive());
1070 <        }
1071 <        catch (Exception ex) {
1072 <            unexpectedException();
1015 <        }
1051 >        final CountDownLatch locked = new CountDownLatch(1);
1052 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1053 >            public void realRun() throws InterruptedException {
1054 >                lock.writeLock().lock();
1055 >                assertWriteLockedByMoi(lock);
1056 >                assertHasNoWaiters(lock, c);
1057 >                locked.countDown();
1058 >                try {
1059 >                    await(c, awaitMethod);
1060 >                } finally {
1061 >                    assertWriteLockedByMoi(lock);
1062 >                    assertHasNoWaiters(lock, c);
1063 >                    lock.writeLock().unlock();
1064 >                    assertFalse(Thread.interrupted());
1065 >                }
1066 >            }});
1067 >
1068 >        await(locked);
1069 >        assertHasWaiters(lock, c, t);
1070 >        t.interrupt();
1071 >        awaitTermination(t);
1072 >        assertNotWriteLocked(lock);
1073      }
1074  
1075      /**
1076 <     * awaitNanos is interruptible
1076 >     * signalAll wakes up all threads
1077       */
1078 <    public void testAwaitNanos_Interrupt() {
1079 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1078 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1079 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1080 >    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1081 >    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1082 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1083 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1084 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
1085 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
1086 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1087 >        final PublicReentrantReadWriteLock lock =
1088 >            new PublicReentrantReadWriteLock(fair);
1089          final Condition c = lock.writeLock().newCondition();
1090 <        Thread t = new Thread(new Runnable() {
1091 <                public void run() {
1092 <                    try {
1093 <                        lock.writeLock().lock();
1094 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
1095 <                        lock.writeLock().unlock();
1096 <                        threadShouldThrow();
1097 <                    }
1098 <                    catch(InterruptedException success) {
1033 <                    }
1034 <                }
1035 <            });
1036 <
1037 <        try {
1038 <            t.start();
1039 <            Thread.sleep(SHORT_DELAY_MS);
1040 <            t.interrupt();
1041 <            t.join(SHORT_DELAY_MS);
1042 <            assertFalse(t.isAlive());
1043 <        }
1044 <        catch (Exception ex) {
1045 <            unexpectedException();
1090 >        final CountDownLatch locked = new CountDownLatch(2);
1091 >        final Lock writeLock = lock.writeLock();
1092 >        class Awaiter extends CheckedRunnable {
1093 >            public void realRun() throws InterruptedException {
1094 >                writeLock.lock();
1095 >                locked.countDown();
1096 >                await(c, awaitMethod);
1097 >                writeLock.unlock();
1098 >            }
1099          }
1100 +
1101 +        Thread t1 = newStartedThread(new Awaiter());
1102 +        Thread t2 = newStartedThread(new Awaiter());
1103 +
1104 +        await(locked);
1105 +        writeLock.lock();
1106 +        assertHasWaiters(lock, c, t1, t2);
1107 +        c.signalAll();
1108 +        assertHasNoWaiters(lock, c);
1109 +        writeLock.unlock();
1110 +        awaitTermination(t1);
1111 +        awaitTermination(t2);
1112      }
1113  
1114      /**
1115 <     * awaitUntil is interruptible
1116 <     */
1117 <    public void testAwaitUntil_Interrupt() {
1118 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1115 >     * signal wakes up waiting threads in FIFO order
1116 >     */
1117 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1118 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1119 >    public void testSignalWakesFifo(boolean fair) {
1120 >        final PublicReentrantReadWriteLock lock =
1121 >            new PublicReentrantReadWriteLock(fair);
1122          final Condition c = lock.writeLock().newCondition();
1123 <        Thread t = new Thread(new Runnable() {
1124 <                public void run() {
1125 <                    try {
1126 <                        lock.writeLock().lock();
1127 <                        java.util.Date d = new java.util.Date();
1128 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1129 <                        lock.writeLock().unlock();
1130 <                        threadShouldThrow();
1131 <                    }
1132 <                    catch(InterruptedException success) {
1133 <                    }
1134 <                }
1135 <            });
1136 <
1137 <        try {
1138 <            t.start();
1139 <            Thread.sleep(SHORT_DELAY_MS);
1140 <            t.interrupt();
1141 <            t.join(SHORT_DELAY_MS);
1142 <            assertFalse(t.isAlive());
1143 <        }
1144 <        catch (Exception ex) {
1145 <            unexpectedException();
1146 <        }
1123 >        final CountDownLatch locked1 = new CountDownLatch(1);
1124 >        final CountDownLatch locked2 = new CountDownLatch(1);
1125 >        final Lock writeLock = lock.writeLock();
1126 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1127 >            public void realRun() throws InterruptedException {
1128 >                writeLock.lock();
1129 >                locked1.countDown();
1130 >                c.await();
1131 >                writeLock.unlock();
1132 >            }});
1133 >
1134 >        await(locked1);
1135 >
1136 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1137 >            public void realRun() throws InterruptedException {
1138 >                writeLock.lock();
1139 >                locked2.countDown();
1140 >                c.await();
1141 >                writeLock.unlock();
1142 >            }});
1143 >
1144 >        await(locked2);
1145 >
1146 >        writeLock.lock();
1147 >        assertHasWaiters(lock, c, t1, t2);
1148 >        assertFalse(lock.hasQueuedThreads());
1149 >        c.signal();
1150 >        assertHasWaiters(lock, c, t2);
1151 >        assertTrue(lock.hasQueuedThread(t1));
1152 >        assertFalse(lock.hasQueuedThread(t2));
1153 >        c.signal();
1154 >        assertHasNoWaiters(lock, c);
1155 >        assertTrue(lock.hasQueuedThread(t1));
1156 >        assertTrue(lock.hasQueuedThread(t2));
1157 >        writeLock.unlock();
1158 >        awaitTermination(t1);
1159 >        awaitTermination(t2);
1160      }
1161  
1162      /**
1163 <     * signalAll wakes up all threads
1164 <     */
1165 <    public void testSignalAll() {
1166 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1163 >     * await after multiple reentrant locking preserves lock count
1164 >     */
1165 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1166 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1167 >    public void testAwaitLockCount(boolean fair) {
1168 >        final PublicReentrantReadWriteLock lock =
1169 >            new PublicReentrantReadWriteLock(fair);
1170          final Condition c = lock.writeLock().newCondition();
1171 <        Thread t1 = new Thread(new Runnable() {
1172 <                public void run() {
1173 <                    try {
1174 <                        lock.writeLock().lock();
1175 <                        c.await();
1176 <                        lock.writeLock().unlock();
1177 <                    }
1178 <                    catch(InterruptedException e) {
1179 <                        threadUnexpectedException();
1180 <                    }
1181 <                }
1182 <            });
1099 <
1100 <        Thread t2 = new Thread(new Runnable() {
1101 <                public void run() {
1102 <                    try {
1103 <                        lock.writeLock().lock();
1104 <                        c.await();
1105 <                        lock.writeLock().unlock();
1106 <                    }
1107 <                    catch(InterruptedException e) {
1108 <                        threadUnexpectedException();
1109 <                    }
1110 <                }
1111 <            });
1171 >        final CountDownLatch locked = new CountDownLatch(2);
1172 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1173 >            public void realRun() throws InterruptedException {
1174 >                lock.writeLock().lock();
1175 >                assertWriteLockedByMoi(lock);
1176 >                assertEquals(1, lock.writeLock().getHoldCount());
1177 >                locked.countDown();
1178 >                c.await();
1179 >                assertWriteLockedByMoi(lock);
1180 >                assertEquals(1, lock.writeLock().getHoldCount());
1181 >                lock.writeLock().unlock();
1182 >            }});
1183  
1184 <        try {
1185 <            t1.start();
1186 <            t2.start();
1187 <            Thread.sleep(SHORT_DELAY_MS);
1188 <            lock.writeLock().lock();
1189 <            c.signalAll();
1190 <            lock.writeLock().unlock();
1191 <            t1.join(SHORT_DELAY_MS);
1192 <            t2.join(SHORT_DELAY_MS);
1193 <            assertFalse(t1.isAlive());
1194 <            assertFalse(t2.isAlive());
1195 <        }
1196 <        catch (Exception ex) {
1197 <            unexpectedException();
1198 <        }
1184 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1185 >            public void realRun() throws InterruptedException {
1186 >                lock.writeLock().lock();
1187 >                lock.writeLock().lock();
1188 >                assertWriteLockedByMoi(lock);
1189 >                assertEquals(2, lock.writeLock().getHoldCount());
1190 >                locked.countDown();
1191 >                c.await();
1192 >                assertWriteLockedByMoi(lock);
1193 >                assertEquals(2, lock.writeLock().getHoldCount());
1194 >                lock.writeLock().unlock();
1195 >                lock.writeLock().unlock();
1196 >            }});
1197 >
1198 >        await(locked);
1199 >        lock.writeLock().lock();
1200 >        assertHasWaiters(lock, c, t1, t2);
1201 >        c.signalAll();
1202 >        assertHasNoWaiters(lock, c);
1203 >        lock.writeLock().unlock();
1204 >        awaitTermination(t1);
1205 >        awaitTermination(t2);
1206      }
1207  
1208      /**
1209       * A serialized lock deserializes as unlocked
1210       */
1211 <    public void testSerialization() {
1212 <        ReentrantReadWriteLock l = new ReentrantReadWriteLock();
1213 <        l.readLock().lock();
1214 <        l.readLock().unlock();
1211 >    public void testSerialization()      { testSerialization(false); }
1212 >    public void testSerialization_fair() { testSerialization(true); }
1213 >    public void testSerialization(boolean fair) {
1214 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1215 >        lock.writeLock().lock();
1216 >        lock.readLock().lock();
1217  
1218 <        try {
1219 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1220 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1221 <            out.writeObject(l);
1222 <            out.close();
1223 <
1224 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1225 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1226 <            ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
1227 <            r.readLock().lock();
1228 <            r.readLock().unlock();
1229 <        } catch(Exception e){
1230 <            e.printStackTrace();
1231 <            unexpectedException();
1232 <        }
1218 >        ReentrantReadWriteLock clone = serialClone(lock);
1219 >        assertEquals(lock.isFair(), clone.isFair());
1220 >        assertTrue(lock.isWriteLocked());
1221 >        assertFalse(clone.isWriteLocked());
1222 >        assertEquals(1, lock.getReadLockCount());
1223 >        assertEquals(0, clone.getReadLockCount());
1224 >        clone.writeLock().lock();
1225 >        clone.readLock().lock();
1226 >        assertTrue(clone.isWriteLocked());
1227 >        assertEquals(1, clone.getReadLockCount());
1228 >        clone.readLock().unlock();
1229 >        clone.writeLock().unlock();
1230 >        assertFalse(clone.isWriteLocked());
1231 >        assertEquals(1, lock.getReadLockCount());
1232 >        assertEquals(0, clone.getReadLockCount());
1233      }
1234  
1235      /**
1236       * hasQueuedThreads reports whether there are waiting threads
1237       */
1238 <    public void testhasQueuedThreads() {
1239 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1238 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
1239 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
1240 >    public void testHasQueuedThreads(boolean fair) {
1241 >        final PublicReentrantReadWriteLock lock =
1242 >            new PublicReentrantReadWriteLock(fair);
1243          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1244          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1245 <        try {
1246 <            assertFalse(lock.hasQueuedThreads());
1247 <            lock.writeLock().lock();
1248 <            t1.start();
1249 <            Thread.sleep(SHORT_DELAY_MS);
1250 <            assertTrue(lock.hasQueuedThreads());
1251 <            t2.start();
1252 <            Thread.sleep(SHORT_DELAY_MS);
1253 <            assertTrue(lock.hasQueuedThreads());
1254 <            t1.interrupt();
1255 <            Thread.sleep(SHORT_DELAY_MS);
1256 <            assertTrue(lock.hasQueuedThreads());
1257 <            lock.writeLock().unlock();
1258 <            Thread.sleep(SHORT_DELAY_MS);
1259 <            assertFalse(lock.hasQueuedThreads());
1260 <            t1.join();
1178 <            t2.join();
1179 <        } catch(Exception e){
1180 <            unexpectedException();
1181 <        }
1182 <    }
1245 >        assertFalse(lock.hasQueuedThreads());
1246 >        lock.writeLock().lock();
1247 >        assertFalse(lock.hasQueuedThreads());
1248 >        t1.start();
1249 >        waitForQueuedThread(lock, t1);
1250 >        assertTrue(lock.hasQueuedThreads());
1251 >        t2.start();
1252 >        waitForQueuedThread(lock, t2);
1253 >        assertTrue(lock.hasQueuedThreads());
1254 >        t1.interrupt();
1255 >        awaitTermination(t1);
1256 >        assertTrue(lock.hasQueuedThreads());
1257 >        lock.writeLock().unlock();
1258 >        awaitTermination(t2);
1259 >        assertFalse(lock.hasQueuedThreads());
1260 >    }
1261  
1262      /**
1263       * hasQueuedThread(null) throws NPE
1264       */
1265 <    public void testHasQueuedThreadNPE() {
1266 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1265 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
1266 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
1267 >    public void testHasQueuedThreadNPE(boolean fair) {
1268 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1269          try {
1270 <            sync.hasQueuedThread(null);
1270 >            lock.hasQueuedThread(null);
1271              shouldThrow();
1272 <        } catch (NullPointerException success) {
1193 <        }
1272 >        } catch (NullPointerException success) {}
1273      }
1274  
1275      /**
1276 <     * hasQueuedThread reports whether a thread is queued.
1276 >     * hasQueuedThread reports whether a thread is queued
1277       */
1278 <    public void testHasQueuedThread() {
1279 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1280 <        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1281 <        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1282 <        try {
1283 <            assertFalse(sync.hasQueuedThread(t1));
1284 <            assertFalse(sync.hasQueuedThread(t2));
1285 <            sync.writeLock().lock();
1286 <            t1.start();
1287 <            Thread.sleep(SHORT_DELAY_MS);
1288 <            assertTrue(sync.hasQueuedThread(t1));
1289 <            t2.start();
1290 <            Thread.sleep(SHORT_DELAY_MS);
1291 <            assertTrue(sync.hasQueuedThread(t1));
1292 <            assertTrue(sync.hasQueuedThread(t2));
1293 <            t1.interrupt();
1294 <            Thread.sleep(SHORT_DELAY_MS);
1295 <            assertFalse(sync.hasQueuedThread(t1));
1296 <            assertTrue(sync.hasQueuedThread(t2));
1297 <            sync.writeLock().unlock();
1298 <            Thread.sleep(SHORT_DELAY_MS);
1299 <            assertFalse(sync.hasQueuedThread(t1));
1300 <            Thread.sleep(SHORT_DELAY_MS);
1301 <            assertFalse(sync.hasQueuedThread(t2));
1302 <            t1.join();
1303 <            t2.join();
1304 <        } catch(Exception e){
1226 <            unexpectedException();
1227 <        }
1228 <    }
1229 <
1278 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
1279 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
1280 >    public void testHasQueuedThread(boolean fair) {
1281 >        final PublicReentrantReadWriteLock lock =
1282 >            new PublicReentrantReadWriteLock(fair);
1283 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1284 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1285 >        assertFalse(lock.hasQueuedThread(t1));
1286 >        assertFalse(lock.hasQueuedThread(t2));
1287 >        lock.writeLock().lock();
1288 >        t1.start();
1289 >        waitForQueuedThread(lock, t1);
1290 >        assertTrue(lock.hasQueuedThread(t1));
1291 >        assertFalse(lock.hasQueuedThread(t2));
1292 >        t2.start();
1293 >        waitForQueuedThread(lock, t2);
1294 >        assertTrue(lock.hasQueuedThread(t1));
1295 >        assertTrue(lock.hasQueuedThread(t2));
1296 >        t1.interrupt();
1297 >        awaitTermination(t1);
1298 >        assertFalse(lock.hasQueuedThread(t1));
1299 >        assertTrue(lock.hasQueuedThread(t2));
1300 >        lock.writeLock().unlock();
1301 >        awaitTermination(t2);
1302 >        assertFalse(lock.hasQueuedThread(t1));
1303 >        assertFalse(lock.hasQueuedThread(t2));
1304 >    }
1305  
1306      /**
1307       * getQueueLength reports number of waiting threads
1308       */
1309 <    public void testGetQueueLength() {
1310 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1309 >    public void testGetQueueLength()      { testGetQueueLength(false); }
1310 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
1311 >    public void testGetQueueLength(boolean fair) {
1312 >        final PublicReentrantReadWriteLock lock =
1313 >            new PublicReentrantReadWriteLock(fair);
1314          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1315          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1316 <        try {
1317 <            assertEquals(0, lock.getQueueLength());
1318 <            lock.writeLock().lock();
1319 <            t1.start();
1320 <            Thread.sleep(SHORT_DELAY_MS);
1321 <            assertEquals(1, lock.getQueueLength());
1322 <            t2.start();
1323 <            Thread.sleep(SHORT_DELAY_MS);
1324 <            assertEquals(2, lock.getQueueLength());
1325 <            t1.interrupt();
1326 <            Thread.sleep(SHORT_DELAY_MS);
1327 <            assertEquals(1, lock.getQueueLength());
1328 <            lock.writeLock().unlock();
1329 <            Thread.sleep(SHORT_DELAY_MS);
1330 <            assertEquals(0, lock.getQueueLength());
1253 <            t1.join();
1254 <            t2.join();
1255 <        } catch(Exception e){
1256 <            unexpectedException();
1257 <        }
1258 <    }
1316 >        assertEquals(0, lock.getQueueLength());
1317 >        lock.writeLock().lock();
1318 >        t1.start();
1319 >        waitForQueuedThread(lock, t1);
1320 >        assertEquals(1, lock.getQueueLength());
1321 >        t2.start();
1322 >        waitForQueuedThread(lock, t2);
1323 >        assertEquals(2, lock.getQueueLength());
1324 >        t1.interrupt();
1325 >        awaitTermination(t1);
1326 >        assertEquals(1, lock.getQueueLength());
1327 >        lock.writeLock().unlock();
1328 >        awaitTermination(t2);
1329 >        assertEquals(0, lock.getQueueLength());
1330 >    }
1331  
1332      /**
1333       * getQueuedThreads includes waiting threads
1334       */
1335 <    public void testGetQueuedThreads() {
1336 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1335 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
1336 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
1337 >    public void testGetQueuedThreads(boolean fair) {
1338 >        final PublicReentrantReadWriteLock lock =
1339 >            new PublicReentrantReadWriteLock(fair);
1340          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1341          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1342 <        try {
1343 <            assertTrue(lock.getQueuedThreads().isEmpty());
1344 <            lock.writeLock().lock();
1345 <            assertTrue(lock.getQueuedThreads().isEmpty());
1346 <            t1.start();
1347 <            Thread.sleep(SHORT_DELAY_MS);
1348 <            assertTrue(lock.getQueuedThreads().contains(t1));
1349 <            t2.start();
1350 <            Thread.sleep(SHORT_DELAY_MS);
1351 <            assertTrue(lock.getQueuedThreads().contains(t1));
1352 <            assertTrue(lock.getQueuedThreads().contains(t2));
1353 <            t1.interrupt();
1354 <            Thread.sleep(SHORT_DELAY_MS);
1355 <            assertFalse(lock.getQueuedThreads().contains(t1));
1356 <            assertTrue(lock.getQueuedThreads().contains(t2));
1357 <            lock.writeLock().unlock();
1358 <            Thread.sleep(SHORT_DELAY_MS);
1359 <            assertTrue(lock.getQueuedThreads().isEmpty());
1360 <            t1.join();
1361 <            t2.join();
1362 <        } catch(Exception e){
1288 <            unexpectedException();
1289 <        }
1290 <    }
1342 >        assertTrue(lock.getQueuedThreads().isEmpty());
1343 >        lock.writeLock().lock();
1344 >        assertTrue(lock.getQueuedThreads().isEmpty());
1345 >        t1.start();
1346 >        waitForQueuedThread(lock, t1);
1347 >        assertEquals(1, lock.getQueuedThreads().size());
1348 >        assertTrue(lock.getQueuedThreads().contains(t1));
1349 >        t2.start();
1350 >        waitForQueuedThread(lock, t2);
1351 >        assertEquals(2, lock.getQueuedThreads().size());
1352 >        assertTrue(lock.getQueuedThreads().contains(t1));
1353 >        assertTrue(lock.getQueuedThreads().contains(t2));
1354 >        t1.interrupt();
1355 >        awaitTermination(t1);
1356 >        assertFalse(lock.getQueuedThreads().contains(t1));
1357 >        assertTrue(lock.getQueuedThreads().contains(t2));
1358 >        assertEquals(1, lock.getQueuedThreads().size());
1359 >        lock.writeLock().unlock();
1360 >        awaitTermination(t2);
1361 >        assertTrue(lock.getQueuedThreads().isEmpty());
1362 >    }
1363  
1364      /**
1365       * hasWaiters throws NPE if null
1366       */
1367 <    public void testHasWaitersNPE() {
1368 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1367 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
1368 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
1369 >    public void testHasWaitersNPE(boolean fair) {
1370 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1371          try {
1372              lock.hasWaiters(null);
1373              shouldThrow();
1374 <        } catch (NullPointerException success) {
1301 <        } catch (Exception ex) {
1302 <            unexpectedException();
1303 <        }
1374 >        } catch (NullPointerException success) {}
1375      }
1376  
1377      /**
1378       * getWaitQueueLength throws NPE if null
1379       */
1380 <    public void testGetWaitQueueLengthNPE() {
1381 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1380 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
1381 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
1382 >    public void testGetWaitQueueLengthNPE(boolean fair) {
1383 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1384          try {
1385              lock.getWaitQueueLength(null);
1386              shouldThrow();
1387 <        } catch (NullPointerException success) {
1315 <        } catch (Exception ex) {
1316 <            unexpectedException();
1317 <        }
1387 >        } catch (NullPointerException success) {}
1388      }
1389  
1320
1390      /**
1391       * getWaitingThreads throws NPE if null
1392       */
1393 <    public void testGetWaitingThreadsNPE() {
1394 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1393 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
1394 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
1395 >    public void testGetWaitingThreadsNPE(boolean fair) {
1396 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1397          try {
1398              lock.getWaitingThreads(null);
1399              shouldThrow();
1400 <        } catch (NullPointerException success) {
1330 <        } catch (Exception ex) {
1331 <            unexpectedException();
1332 <        }
1400 >        } catch (NullPointerException success) {}
1401      }
1402  
1403      /**
1404 <     * hasWaiters throws IAE if not owned
1404 >     * hasWaiters throws IllegalArgumentException if not owned
1405       */
1406 <    public void testHasWaitersIAE() {
1407 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1408 <        final Condition c = (lock.writeLock().newCondition());
1409 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1406 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
1407 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
1408 >    public void testHasWaitersIAE(boolean fair) {
1409 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1410 >        final Condition c = lock.writeLock().newCondition();
1411 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1412          try {
1413              lock2.hasWaiters(c);
1414              shouldThrow();
1415 <        } catch (IllegalArgumentException success) {
1346 <        } catch (Exception ex) {
1347 <            unexpectedException();
1348 <        }
1415 >        } catch (IllegalArgumentException success) {}
1416      }
1417  
1418      /**
1419 <     * hasWaiters throws IMSE if not locked
1419 >     * hasWaiters throws IllegalMonitorStateException if not locked
1420       */
1421 <    public void testHasWaitersIMSE() {
1422 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1423 <        final Condition c = (lock.writeLock().newCondition());
1421 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
1422 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
1423 >    public void testHasWaitersIMSE(boolean fair) {
1424 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1425 >        final Condition c = lock.writeLock().newCondition();
1426          try {
1427              lock.hasWaiters(c);
1428              shouldThrow();
1429 <        } catch (IllegalMonitorStateException success) {
1361 <        } catch (Exception ex) {
1362 <            unexpectedException();
1363 <        }
1429 >        } catch (IllegalMonitorStateException success) {}
1430      }
1431  
1366
1432      /**
1433 <     * getWaitQueueLength throws IAE if not owned
1433 >     * getWaitQueueLength throws IllegalArgumentException if not owned
1434       */
1435 <    public void testGetWaitQueueLengthIAE() {
1436 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1437 <        final Condition c = (lock.writeLock().newCondition());
1438 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1435 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
1436 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
1437 >    public void testGetWaitQueueLengthIAE(boolean fair) {
1438 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1439 >        final Condition c = lock.writeLock().newCondition();
1440 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1441          try {
1442              lock2.getWaitQueueLength(c);
1443              shouldThrow();
1444 <        } catch (IllegalArgumentException success) {
1378 <        } catch (Exception ex) {
1379 <            unexpectedException();
1380 <        }
1444 >        } catch (IllegalArgumentException success) {}
1445      }
1446  
1447      /**
1448 <     * getWaitQueueLength throws IMSE if not locked
1448 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
1449       */
1450 <    public void testGetWaitQueueLengthIMSE() {
1451 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1452 <        final Condition c = (lock.writeLock().newCondition());
1450 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
1451 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
1452 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
1453 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1454 >        final Condition c = lock.writeLock().newCondition();
1455          try {
1456              lock.getWaitQueueLength(c);
1457              shouldThrow();
1458 <        } catch (IllegalMonitorStateException success) {
1393 <        } catch (Exception ex) {
1394 <            unexpectedException();
1395 <        }
1458 >        } catch (IllegalMonitorStateException success) {}
1459      }
1460  
1398
1461      /**
1462 <     * getWaitingThreads throws IAE if not owned
1462 >     * getWaitingThreads throws IllegalArgumentException if not owned
1463       */
1464 <    public void testGetWaitingThreadsIAE() {
1465 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1466 <        final Condition c = (lock.writeLock().newCondition());
1467 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();  
1464 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
1465 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
1466 >    public void testGetWaitingThreadsIAE(boolean fair) {
1467 >        final PublicReentrantReadWriteLock lock =
1468 >            new PublicReentrantReadWriteLock(fair);
1469 >        final Condition c = lock.writeLock().newCondition();
1470 >        final PublicReentrantReadWriteLock lock2 =
1471 >            new PublicReentrantReadWriteLock(fair);
1472          try {
1473              lock2.getWaitingThreads(c);
1474              shouldThrow();
1475 <        } catch (IllegalArgumentException success) {
1410 <        } catch (Exception ex) {
1411 <            unexpectedException();
1412 <        }
1475 >        } catch (IllegalArgumentException success) {}
1476      }
1477  
1478      /**
1479 <     * getWaitingThreads throws IMSE if not locked
1479 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
1480       */
1481 <    public void testGetWaitingThreadsIMSE() {
1482 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1483 <        final Condition c = (lock.writeLock().newCondition());
1481 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
1482 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
1483 >    public void testGetWaitingThreadsIMSE(boolean fair) {
1484 >        final PublicReentrantReadWriteLock lock =
1485 >            new PublicReentrantReadWriteLock(fair);
1486 >        final Condition c = lock.writeLock().newCondition();
1487          try {
1488              lock.getWaitingThreads(c);
1489              shouldThrow();
1490 <        } catch (IllegalMonitorStateException success) {
1425 <        } catch (Exception ex) {
1426 <            unexpectedException();
1427 <        }
1490 >        } catch (IllegalMonitorStateException success) {}
1491      }
1492  
1430
1493      /**
1494       * hasWaiters returns true when a thread is waiting, else false
1495       */
1496 <    public void testHasWaiters() {
1497 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1498 <        final Condition c = (lock.writeLock().newCondition());
1499 <        Thread t = new Thread(new Runnable() {
1500 <                public void run() {
1501 <                    try {
1502 <                        lock.writeLock().lock();
1503 <                        threadAssertFalse(lock.hasWaiters(c));
1504 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1505 <                        c.await();
1506 <                        lock.writeLock().unlock();
1507 <                    }
1508 <                    catch(InterruptedException e) {
1509 <                        threadUnexpectedException();
1510 <                    }
1511 <                }
1512 <            });
1496 >    public void testHasWaiters()      { testHasWaiters(false); }
1497 >    public void testHasWaiters_fair() { testHasWaiters(true); }
1498 >    public void testHasWaiters(boolean fair) {
1499 >        final PublicReentrantReadWriteLock lock =
1500 >            new PublicReentrantReadWriteLock(fair);
1501 >        final Condition c = lock.writeLock().newCondition();
1502 >        final CountDownLatch locked = new CountDownLatch(1);
1503 >        Thread t = newStartedThread(new CheckedRunnable() {
1504 >            public void realRun() throws InterruptedException {
1505 >                lock.writeLock().lock();
1506 >                assertHasNoWaiters(lock, c);
1507 >                assertFalse(lock.hasWaiters(c));
1508 >                locked.countDown();
1509 >                c.await();
1510 >                assertHasNoWaiters(lock, c);
1511 >                assertFalse(lock.hasWaiters(c));
1512 >                lock.writeLock().unlock();
1513 >            }});
1514  
1515 <        try {
1516 <            t.start();
1517 <            Thread.sleep(SHORT_DELAY_MS);
1518 <            lock.writeLock().lock();
1519 <            assertTrue(lock.hasWaiters(c));
1520 <            assertEquals(1, lock.getWaitQueueLength(c));
1521 <            c.signal();
1522 <            lock.writeLock().unlock();
1523 <            Thread.sleep(SHORT_DELAY_MS);
1524 <            lock.writeLock().lock();
1462 <            assertFalse(lock.hasWaiters(c));
1463 <            assertEquals(0, lock.getWaitQueueLength(c));
1464 <            lock.writeLock().unlock();
1465 <            t.join(SHORT_DELAY_MS);
1466 <            assertFalse(t.isAlive());
1467 <        }
1468 <        catch (Exception ex) {
1469 <            unexpectedException();
1470 <        }
1515 >        await(locked);
1516 >        lock.writeLock().lock();
1517 >        assertHasWaiters(lock, c, t);
1518 >        assertTrue(lock.hasWaiters(c));
1519 >        c.signal();
1520 >        assertHasNoWaiters(lock, c);
1521 >        assertFalse(lock.hasWaiters(c));
1522 >        lock.writeLock().unlock();
1523 >        awaitTermination(t);
1524 >        assertHasNoWaiters(lock, c);
1525      }
1526  
1527      /**
1528       * getWaitQueueLength returns number of waiting threads
1529       */
1530 <    public void testGetWaitQueueLength() {
1531 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1532 <        final Condition c = (lock.writeLock().newCondition());
1533 <        Thread t = new Thread(new Runnable() {
1534 <                public void run() {
1535 <                    try {
1536 <                        lock.writeLock().lock();
1537 <                        threadAssertFalse(lock.hasWaiters(c));
1538 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1539 <                        c.await();
1540 <                        lock.writeLock().unlock();
1541 <                    }
1542 <                    catch(InterruptedException e) {
1543 <                        threadUnexpectedException();
1544 <                    }
1491 <                }
1492 <            });
1530 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
1531 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
1532 >    public void testGetWaitQueueLength(boolean fair) {
1533 >        final PublicReentrantReadWriteLock lock =
1534 >            new PublicReentrantReadWriteLock(fair);
1535 >        final Condition c = lock.writeLock().newCondition();
1536 >        final CountDownLatch locked = new CountDownLatch(1);
1537 >        Thread t = newStartedThread(new CheckedRunnable() {
1538 >            public void realRun() throws InterruptedException {
1539 >                lock.writeLock().lock();
1540 >                assertEquals(0, lock.getWaitQueueLength(c));
1541 >                locked.countDown();
1542 >                c.await();
1543 >                lock.writeLock().unlock();
1544 >            }});
1545  
1546 <        try {
1547 <            t.start();
1548 <            Thread.sleep(SHORT_DELAY_MS);
1549 <            lock.writeLock().lock();
1550 <            assertTrue(lock.hasWaiters(c));
1551 <            assertEquals(1, lock.getWaitQueueLength(c));
1552 <            c.signal();
1553 <            lock.writeLock().unlock();
1554 <            Thread.sleep(SHORT_DELAY_MS);
1503 <            lock.writeLock().lock();
1504 <            assertFalse(lock.hasWaiters(c));
1505 <            assertEquals(0, lock.getWaitQueueLength(c));
1506 <            lock.writeLock().unlock();
1507 <            t.join(SHORT_DELAY_MS);
1508 <            assertFalse(t.isAlive());
1509 <        }
1510 <        catch (Exception ex) {
1511 <            unexpectedException();
1512 <        }
1546 >        await(locked);
1547 >        lock.writeLock().lock();
1548 >        assertHasWaiters(lock, c, t);
1549 >        assertEquals(1, lock.getWaitQueueLength(c));
1550 >        c.signal();
1551 >        assertHasNoWaiters(lock, c);
1552 >        assertEquals(0, lock.getWaitQueueLength(c));
1553 >        lock.writeLock().unlock();
1554 >        awaitTermination(t);
1555      }
1556  
1515
1557      /**
1558       * getWaitingThreads returns only and all waiting threads
1559       */
1560 <    public void testGetWaitingThreads() {
1561 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1560 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
1561 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
1562 >    public void testGetWaitingThreads(boolean fair) {
1563 >        final PublicReentrantReadWriteLock lock =
1564 >            new PublicReentrantReadWriteLock(fair);
1565          final Condition c = lock.writeLock().newCondition();
1566 <        Thread t1 = new Thread(new Runnable() {
1567 <                public void run() {
1568 <                    try {
1569 <                        lock.writeLock().lock();
1570 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1571 <                        c.await();
1572 <                        lock.writeLock().unlock();
1573 <                    }
1574 <                    catch(InterruptedException e) {
1575 <                        threadUnexpectedException();
1532 <                    }
1533 <                }
1534 <            });
1535 <
1536 <        Thread t2 = new Thread(new Runnable() {
1537 <                public void run() {
1538 <                    try {
1539 <                        lock.writeLock().lock();
1540 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1541 <                        c.await();
1542 <                        lock.writeLock().unlock();
1543 <                    }
1544 <                    catch(InterruptedException e) {
1545 <                        threadUnexpectedException();
1546 <                    }
1547 <                }
1548 <            });
1566 >        final CountDownLatch locked1 = new CountDownLatch(1);
1567 >        final CountDownLatch locked2 = new CountDownLatch(1);
1568 >        Thread t1 = new Thread(new CheckedRunnable() {
1569 >            public void realRun() throws InterruptedException {
1570 >                lock.writeLock().lock();
1571 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1572 >                locked1.countDown();
1573 >                c.await();
1574 >                lock.writeLock().unlock();
1575 >            }});
1576  
1577 <        try {
1578 <            lock.writeLock().lock();
1579 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
1580 <            lock.writeLock().unlock();
1581 <            t1.start();
1582 <            Thread.sleep(SHORT_DELAY_MS);
1583 <            t2.start();
1584 <            Thread.sleep(SHORT_DELAY_MS);
1585 <            lock.writeLock().lock();
1586 <            assertTrue(lock.hasWaiters(c));
1587 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
1588 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
1589 <            c.signalAll();
1590 <            lock.writeLock().unlock();
1591 <            Thread.sleep(SHORT_DELAY_MS);
1592 <            lock.writeLock().lock();
1593 <            assertFalse(lock.hasWaiters(c));
1594 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
1595 <            lock.writeLock().unlock();
1596 <            t1.join(SHORT_DELAY_MS);
1597 <            t2.join(SHORT_DELAY_MS);
1598 <            assertFalse(t1.isAlive());
1599 <            assertFalse(t2.isAlive());
1600 <        }
1601 <        catch (Exception ex) {
1602 <            unexpectedException();
1603 <        }
1577 >        Thread t2 = new Thread(new CheckedRunnable() {
1578 >            public void realRun() throws InterruptedException {
1579 >                lock.writeLock().lock();
1580 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1581 >                locked2.countDown();
1582 >                c.await();
1583 >                lock.writeLock().unlock();
1584 >            }});
1585 >
1586 >        lock.writeLock().lock();
1587 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
1588 >        lock.writeLock().unlock();
1589 >
1590 >        t1.start();
1591 >        await(locked1);
1592 >        t2.start();
1593 >        await(locked2);
1594 >
1595 >        lock.writeLock().lock();
1596 >        assertTrue(lock.hasWaiters(c));
1597 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
1598 >        assertTrue(lock.getWaitingThreads(c).contains(t2));
1599 >        assertEquals(2, lock.getWaitingThreads(c).size());
1600 >        c.signalAll();
1601 >        assertHasNoWaiters(lock, c);
1602 >        lock.writeLock().unlock();
1603 >
1604 >        awaitTermination(t1);
1605 >        awaitTermination(t2);
1606 >
1607 >        assertHasNoWaiters(lock, c);
1608      }
1609  
1610      /**
1611       * toString indicates current lock state
1612       */
1613 <    public void testToString() {
1614 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1615 <        String us = lock.toString();
1616 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1617 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1618 <        lock.writeLock().lock();
1619 <        String ws = lock.toString();
1620 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1621 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1613 >    public void testToString()      { testToString(false); }
1614 >    public void testToString_fair() { testToString(true); }
1615 >    public void testToString(boolean fair) {
1616 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1617 >        assertTrue(lock.toString().contains("Write locks = 0"));
1618 >        assertTrue(lock.toString().contains("Read locks = 0"));
1619 >        lock.writeLock().lock();
1620 >        assertTrue(lock.toString().contains("Write locks = 1"));
1621 >        assertTrue(lock.toString().contains("Read locks = 0"));
1622          lock.writeLock().unlock();
1623          lock.readLock().lock();
1624          lock.readLock().lock();
1625 <        String rs = lock.toString();
1626 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1596 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1625 >        assertTrue(lock.toString().contains("Write locks = 0"));
1626 >        assertTrue(lock.toString().contains("Read locks = 2"));
1627      }
1628  
1629      /**
1630       * readLock.toString indicates current lock state
1631       */
1632 <    public void testReadLockToString() {
1633 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1634 <        String us = lock.readLock().toString();
1635 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1632 >    public void testReadLockToString()      { testReadLockToString(false); }
1633 >    public void testReadLockToString_fair() { testReadLockToString(true); }
1634 >    public void testReadLockToString(boolean fair) {
1635 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1636 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1637          lock.readLock().lock();
1638          lock.readLock().lock();
1639 <        String rs = lock.readLock().toString();
1609 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1639 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1640      }
1641  
1642      /**
1643       * writeLock.toString indicates current lock state
1644       */
1645 <    public void testWriteLockToString() {
1646 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1647 <        String us = lock.writeLock().toString();
1648 <        assertTrue(us.indexOf("Unlocked") >= 0);
1645 >    public void testWriteLockToString()      { testWriteLockToString(false); }
1646 >    public void testWriteLockToString_fair() { testWriteLockToString(true); }
1647 >    public void testWriteLockToString(boolean fair) {
1648 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1649 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1650          lock.writeLock().lock();
1651 <        String ls = lock.writeLock().toString();
1652 <        assertTrue(ls.indexOf("Locked") >= 0);
1651 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1652 >        lock.writeLock().unlock();
1653 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1654      }
1655  
1656   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines