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.27 by dl, Thu May 18 10:29:23 2006 UTC vs.
Revision 1.33 by jsr166, Tue Nov 17 14:33:55 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines