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.26 by dl, Fri Feb 24 00:03:16 2006 UTC vs.
Revision 1.56 by jsr166, Tue May 3 06:08:49 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines