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.25 by dl, Sun Jan 29 21:17:38 2006 UTC vs.
Revision 1.55 by jsr166, Mon May 2 01:15:26 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines