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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.26 by dl, Fri Feb 24 00:03:16 2006 UTC vs.
Revision 1.54 by jsr166, Mon May 2 01:07:15 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines