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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.27 by dl, Thu May 18 10:29:23 2006 UTC vs.
Revision 1.37 by jsr166, Sat Nov 21 10:25:05 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines