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.6 by dl, Mon Nov 10 13:32:08 2003 UTC vs.
Revision 1.57 by dl, Sat May 7 11:15:04 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines