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.52 by jsr166, Mon May 2 00:34:12 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines