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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines