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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines