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

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.24 by dl, Fri Feb 24 00:03:16 2006 UTC vs.
Revision 1.41 by jsr166, Sat May 7 02:51:33 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.util.*;
14   import java.io.*;
15  
16   public class ReentrantLockTest 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(ReentrantLockTest.class);
21 >        return new TestSuite(ReentrantLockTest.class);
22      }
23  
24      /**
25       * A runnable calling lockInterruptibly
26       */
27 <    class InterruptibleLockRunnable implements Runnable {
27 >    class InterruptibleLockRunnable extends CheckedRunnable {
28          final ReentrantLock lock;
29          InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
30 <        public void run() {
31 <            try {
31 <                lock.lockInterruptibly();
32 <            } catch(InterruptedException success){}
30 >        public void realRun() throws InterruptedException {
31 >            lock.lockInterruptibly();
32          }
33      }
34  
# Line 38 | Line 37 | public class ReentrantLockTest extends J
37       * A runnable calling lockInterruptibly that expects to be
38       * interrupted
39       */
40 <    class InterruptedLockRunnable implements Runnable {
40 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
41          final ReentrantLock lock;
42          InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 <        public void run() {
44 <            try {
46 <                lock.lockInterruptibly();
47 <                threadShouldThrow();
48 <            } catch(InterruptedException success){}
43 >        public void realRun() throws InterruptedException {
44 >            lock.lockInterruptibly();
45          }
46      }
47  
# Line 54 | Line 50 | public class ReentrantLockTest extends J
50       */
51      static class PublicReentrantLock extends ReentrantLock {
52          PublicReentrantLock() { 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          }
63
64
59      }
60  
61      /**
62       * Constructor sets given fairness
63       */
64 <    public void testConstructor() {
65 <        ReentrantLock rl = new ReentrantLock();
66 <        assertFalse(rl.isFair());
67 <        ReentrantLock r2 = new ReentrantLock(true);
74 <        assertTrue(r2.isFair());
64 >    public void testConstructor() {
65 >        assertFalse(new ReentrantLock().isFair());
66 >        assertFalse(new ReentrantLock(false).isFair());
67 >        assertTrue(new ReentrantLock(true).isFair());
68      }
69  
70      /**
71       * locking an unlocked lock succeeds
72       */
73 <    public void testLock() {
74 <        ReentrantLock rl = new ReentrantLock();
73 >    public void testLock() {
74 >        ReentrantLock rl = new ReentrantLock();
75          rl.lock();
76          assertTrue(rl.isLocked());
77          rl.unlock();
78 +        assertFalse(rl.isLocked());
79      }
80  
81      /**
82       * locking an unlocked fair lock succeeds
83       */
84 <    public void testFairLock() {
85 <        ReentrantLock rl = new ReentrantLock(true);
84 >    public void testFairLock() {
85 >        ReentrantLock rl = new ReentrantLock(true);
86          rl.lock();
87          assertTrue(rl.isLocked());
88          rl.unlock();
# Line 97 | Line 91 | public class ReentrantLockTest extends J
91      /**
92       * Unlocking an unlocked lock throws IllegalMonitorStateException
93       */
94 <    public void testUnlock_IllegalMonitorStateException() {
95 <        ReentrantLock rl = new ReentrantLock();
96 <        try {
97 <            rl.unlock();
98 <            shouldThrow();
99 <
106 <        } catch(IllegalMonitorStateException success){}
94 >    public void testUnlock_IllegalMonitorStateException() {
95 >        ReentrantLock rl = new ReentrantLock();
96 >        try {
97 >            rl.unlock();
98 >            shouldThrow();
99 >        } catch (IllegalMonitorStateException success) {}
100      }
101  
102      /**
103       * tryLock on an unlocked lock succeeds
104       */
105 <    public void testTryLock() {
106 <        ReentrantLock rl = new ReentrantLock();
105 >    public void testTryLock() {
106 >        ReentrantLock rl = new ReentrantLock();
107          assertTrue(rl.tryLock());
108          assertTrue(rl.isLocked());
109          rl.unlock();
# Line 120 | Line 113 | public class ReentrantLockTest extends J
113      /**
114       * hasQueuedThreads reports whether there are waiting threads
115       */
116 <    public void testhasQueuedThreads() {
117 <        final ReentrantLock lock = new ReentrantLock();
116 >    public void testhasQueuedThreads() throws InterruptedException {
117 >        final ReentrantLock lock = new ReentrantLock();
118          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
119          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
120 <        try {
121 <            assertFalse(lock.hasQueuedThreads());
122 <            lock.lock();
123 <            t1.start();
124 <            Thread.sleep(SHORT_DELAY_MS);
125 <            assertTrue(lock.hasQueuedThreads());
126 <            t2.start();
127 <            Thread.sleep(SHORT_DELAY_MS);
128 <            assertTrue(lock.hasQueuedThreads());
129 <            t1.interrupt();
130 <            Thread.sleep(SHORT_DELAY_MS);
131 <            assertTrue(lock.hasQueuedThreads());
132 <            lock.unlock();
133 <            Thread.sleep(SHORT_DELAY_MS);
134 <            assertFalse(lock.hasQueuedThreads());
135 <            t1.join();
136 <            t2.join();
144 <        } catch(Exception e){
145 <            unexpectedException();
146 <        }
147 <    }
120 >        assertFalse(lock.hasQueuedThreads());
121 >        lock.lock();
122 >        t1.start();
123 >        delay(SHORT_DELAY_MS);
124 >        assertTrue(lock.hasQueuedThreads());
125 >        t2.start();
126 >        delay(SHORT_DELAY_MS);
127 >        assertTrue(lock.hasQueuedThreads());
128 >        t1.interrupt();
129 >        delay(SHORT_DELAY_MS);
130 >        assertTrue(lock.hasQueuedThreads());
131 >        lock.unlock();
132 >        delay(SHORT_DELAY_MS);
133 >        assertFalse(lock.hasQueuedThreads());
134 >        t1.join();
135 >        t2.join();
136 >    }
137  
138      /**
139       * getQueueLength reports number of waiting threads
140       */
141 <    public void testGetQueueLength() {
142 <        final ReentrantLock lock = new ReentrantLock();
141 >    public void testGetQueueLength() throws InterruptedException {
142 >        final ReentrantLock lock = new ReentrantLock();
143          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
144          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
145 <        try {
146 <            assertEquals(0, lock.getQueueLength());
147 <            lock.lock();
148 <            t1.start();
149 <            Thread.sleep(SHORT_DELAY_MS);
150 <            assertEquals(1, lock.getQueueLength());
151 <            t2.start();
152 <            Thread.sleep(SHORT_DELAY_MS);
153 <            assertEquals(2, lock.getQueueLength());
154 <            t1.interrupt();
155 <            Thread.sleep(SHORT_DELAY_MS);
156 <            assertEquals(1, lock.getQueueLength());
157 <            lock.unlock();
158 <            Thread.sleep(SHORT_DELAY_MS);
159 <            assertEquals(0, lock.getQueueLength());
160 <            t1.join();
161 <            t2.join();
173 <        } catch(Exception e){
174 <            unexpectedException();
175 <        }
176 <    }
145 >        assertEquals(0, lock.getQueueLength());
146 >        lock.lock();
147 >        t1.start();
148 >        delay(SHORT_DELAY_MS);
149 >        assertEquals(1, lock.getQueueLength());
150 >        t2.start();
151 >        delay(SHORT_DELAY_MS);
152 >        assertEquals(2, lock.getQueueLength());
153 >        t1.interrupt();
154 >        delay(SHORT_DELAY_MS);
155 >        assertEquals(1, lock.getQueueLength());
156 >        lock.unlock();
157 >        delay(SHORT_DELAY_MS);
158 >        assertEquals(0, lock.getQueueLength());
159 >        t1.join();
160 >        t2.join();
161 >    }
162  
163      /**
164       * getQueueLength reports number of waiting threads
165       */
166 <    public void testGetQueueLength_fair() {
167 <        final ReentrantLock lock = new ReentrantLock(true);
166 >    public void testGetQueueLength_fair() throws InterruptedException {
167 >        final ReentrantLock lock = new ReentrantLock(true);
168          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
169          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
170 <        try {
171 <            assertEquals(0, lock.getQueueLength());
172 <            lock.lock();
173 <            t1.start();
174 <            Thread.sleep(SHORT_DELAY_MS);
175 <            assertEquals(1, lock.getQueueLength());
176 <            t2.start();
177 <            Thread.sleep(SHORT_DELAY_MS);
178 <            assertEquals(2, lock.getQueueLength());
179 <            t1.interrupt();
180 <            Thread.sleep(SHORT_DELAY_MS);
181 <            assertEquals(1, lock.getQueueLength());
182 <            lock.unlock();
183 <            Thread.sleep(SHORT_DELAY_MS);
184 <            assertEquals(0, lock.getQueueLength());
185 <            t1.join();
186 <            t2.join();
202 <        } catch(Exception e){
203 <            unexpectedException();
204 <        }
205 <    }
170 >        assertEquals(0, lock.getQueueLength());
171 >        lock.lock();
172 >        t1.start();
173 >        delay(SHORT_DELAY_MS);
174 >        assertEquals(1, lock.getQueueLength());
175 >        t2.start();
176 >        delay(SHORT_DELAY_MS);
177 >        assertEquals(2, lock.getQueueLength());
178 >        t1.interrupt();
179 >        delay(SHORT_DELAY_MS);
180 >        assertEquals(1, lock.getQueueLength());
181 >        lock.unlock();
182 >        delay(SHORT_DELAY_MS);
183 >        assertEquals(0, lock.getQueueLength());
184 >        t1.join();
185 >        t2.join();
186 >    }
187  
188      /**
189       * hasQueuedThread(null) throws NPE
190       */
191 <    public void testHasQueuedThreadNPE() {
192 <        final ReentrantLock sync = new ReentrantLock();
191 >    public void testHasQueuedThreadNPE() {
192 >        final ReentrantLock sync = new ReentrantLock();
193          try {
194              sync.hasQueuedThread(null);
195              shouldThrow();
196 <        } catch (NullPointerException success) {
216 <        }
196 >        } catch (NullPointerException success) {}
197      }
198  
199      /**
200       * hasQueuedThread reports whether a thread is queued.
201       */
202 <    public void testHasQueuedThread() {
203 <        final ReentrantLock sync = new ReentrantLock();
202 >    public void testHasQueuedThread() throws InterruptedException {
203 >        final ReentrantLock sync = new ReentrantLock();
204          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
205          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
206 <        try {
207 <            assertFalse(sync.hasQueuedThread(t1));
208 <            assertFalse(sync.hasQueuedThread(t2));
209 <            sync.lock();
210 <            t1.start();
211 <            Thread.sleep(SHORT_DELAY_MS);
212 <            assertTrue(sync.hasQueuedThread(t1));
213 <            t2.start();
214 <            Thread.sleep(SHORT_DELAY_MS);
215 <            assertTrue(sync.hasQueuedThread(t1));
216 <            assertTrue(sync.hasQueuedThread(t2));
217 <            t1.interrupt();
218 <            Thread.sleep(SHORT_DELAY_MS);
219 <            assertFalse(sync.hasQueuedThread(t1));
220 <            assertTrue(sync.hasQueuedThread(t2));
221 <            sync.unlock();
222 <            Thread.sleep(SHORT_DELAY_MS);
223 <            assertFalse(sync.hasQueuedThread(t1));
224 <            Thread.sleep(SHORT_DELAY_MS);
225 <            assertFalse(sync.hasQueuedThread(t2));
226 <            t1.join();
227 <            t2.join();
248 <        } catch(Exception e){
249 <            unexpectedException();
250 <        }
251 <    }
206 >        assertFalse(sync.hasQueuedThread(t1));
207 >        assertFalse(sync.hasQueuedThread(t2));
208 >        sync.lock();
209 >        t1.start();
210 >        delay(SHORT_DELAY_MS);
211 >        assertTrue(sync.hasQueuedThread(t1));
212 >        t2.start();
213 >        delay(SHORT_DELAY_MS);
214 >        assertTrue(sync.hasQueuedThread(t1));
215 >        assertTrue(sync.hasQueuedThread(t2));
216 >        t1.interrupt();
217 >        delay(SHORT_DELAY_MS);
218 >        assertFalse(sync.hasQueuedThread(t1));
219 >        assertTrue(sync.hasQueuedThread(t2));
220 >        sync.unlock();
221 >        delay(SHORT_DELAY_MS);
222 >        assertFalse(sync.hasQueuedThread(t1));
223 >        delay(SHORT_DELAY_MS);
224 >        assertFalse(sync.hasQueuedThread(t2));
225 >        t1.join();
226 >        t2.join();
227 >    }
228  
229  
230      /**
231       * getQueuedThreads includes waiting threads
232       */
233 <    public void testGetQueuedThreads() {
234 <        final PublicReentrantLock lock = new PublicReentrantLock();
233 >    public void testGetQueuedThreads() throws InterruptedException {
234 >        final PublicReentrantLock lock = new PublicReentrantLock();
235          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
236          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
237 <        try {
238 <            assertTrue(lock.getQueuedThreads().isEmpty());
239 <            lock.lock();
240 <            assertTrue(lock.getQueuedThreads().isEmpty());
241 <            t1.start();
242 <            Thread.sleep(SHORT_DELAY_MS);
243 <            assertTrue(lock.getQueuedThreads().contains(t1));
244 <            t2.start();
245 <            Thread.sleep(SHORT_DELAY_MS);
246 <            assertTrue(lock.getQueuedThreads().contains(t1));
247 <            assertTrue(lock.getQueuedThreads().contains(t2));
248 <            t1.interrupt();
249 <            Thread.sleep(SHORT_DELAY_MS);
250 <            assertFalse(lock.getQueuedThreads().contains(t1));
251 <            assertTrue(lock.getQueuedThreads().contains(t2));
252 <            lock.unlock();
253 <            Thread.sleep(SHORT_DELAY_MS);
254 <            assertTrue(lock.getQueuedThreads().isEmpty());
255 <            t1.join();
256 <            t2.join();
281 <        } catch(Exception e){
282 <            unexpectedException();
283 <        }
284 <    }
237 >        assertTrue(lock.getQueuedThreads().isEmpty());
238 >        lock.lock();
239 >        assertTrue(lock.getQueuedThreads().isEmpty());
240 >        t1.start();
241 >        delay(SHORT_DELAY_MS);
242 >        assertTrue(lock.getQueuedThreads().contains(t1));
243 >        t2.start();
244 >        delay(SHORT_DELAY_MS);
245 >        assertTrue(lock.getQueuedThreads().contains(t1));
246 >        assertTrue(lock.getQueuedThreads().contains(t2));
247 >        t1.interrupt();
248 >        delay(SHORT_DELAY_MS);
249 >        assertFalse(lock.getQueuedThreads().contains(t1));
250 >        assertTrue(lock.getQueuedThreads().contains(t2));
251 >        lock.unlock();
252 >        delay(SHORT_DELAY_MS);
253 >        assertTrue(lock.getQueuedThreads().isEmpty());
254 >        t1.join();
255 >        t2.join();
256 >    }
257  
258  
259      /**
260       * timed tryLock is interruptible.
261       */
262 <    public void testInterruptedException2() {
263 <        final ReentrantLock lock = new ReentrantLock();
264 <        lock.lock();
265 <        Thread t = new Thread(new Runnable() {
266 <                public void run() {
267 <                    try {
268 <                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
269 <                        threadShouldThrow();
270 <                    } catch(InterruptedException success){}
271 <                }
272 <            });
301 <        try {
302 <            t.start();
303 <            t.interrupt();
304 <        } catch(Exception e){
305 <            unexpectedException();
306 <        }
262 >    public void testInterruptedException2() throws InterruptedException {
263 >        final ReentrantLock lock = new ReentrantLock();
264 >        lock.lock();
265 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
266 >            public void realRun() throws InterruptedException {
267 >                lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS);
268 >            }});
269 >
270 >        delay(SHORT_DELAY_MS);
271 >        t.interrupt();
272 >        t.join();
273      }
274  
275  
276      /**
277       * TryLock on a locked lock fails
278       */
279 <    public void testTryLockWhenLocked() {
280 <        final ReentrantLock lock = new ReentrantLock();
281 <        lock.lock();
282 <        Thread t = new Thread(new Runnable() {
283 <                public void run() {
284 <                    threadAssertFalse(lock.tryLock());
285 <                }
286 <            });
287 <        try {
288 <            t.start();
289 <            t.join();
324 <            lock.unlock();
325 <        } catch(Exception e){
326 <            unexpectedException();
327 <        }
328 <    }
279 >    public void testTryLockWhenLocked() throws InterruptedException {
280 >        final ReentrantLock lock = new ReentrantLock();
281 >        lock.lock();
282 >        Thread t = newStartedThread(new CheckedRunnable() {
283 >            public void realRun() {
284 >                assertFalse(lock.tryLock());
285 >            }});
286 >
287 >        t.join();
288 >        lock.unlock();
289 >    }
290  
291      /**
292       * Timed tryLock on a locked lock times out
293       */
294 <    public void testTryLock_Timeout() {
295 <        final ReentrantLock lock = new ReentrantLock();
296 <        lock.lock();
297 <        Thread t = new Thread(new Runnable() {
298 <                public void run() {
299 <                    try {
300 <                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
301 <                    } catch (Exception ex) {
302 <                        threadUnexpectedException();
303 <                    }
304 <                }
305 <            });
345 <        try {
346 <            t.start();
347 <            t.join();
348 <            lock.unlock();
349 <        } catch(Exception e){
350 <            unexpectedException();
351 <        }
352 <    }
353 <    
294 >    public void testTryLock_Timeout() throws InterruptedException {
295 >        final ReentrantLock lock = new ReentrantLock();
296 >        lock.lock();
297 >        Thread t = newStartedThread(new CheckedRunnable() {
298 >            public void realRun() throws InterruptedException {
299 >                assertFalse(lock.tryLock(1, MILLISECONDS));
300 >            }});
301 >
302 >        t.join();
303 >        lock.unlock();
304 >    }
305 >
306      /**
307       * getHoldCount returns number of recursive holds
308       */
309      public void testGetHoldCount() {
310 <        ReentrantLock lock = new ReentrantLock();
311 <        for(int i = 1; i <= SIZE; i++) {
312 <            lock.lock();
313 <            assertEquals(i,lock.getHoldCount());
314 <        }
315 <        for(int i = SIZE; i > 0; i--) {
316 <            lock.unlock();
317 <            assertEquals(i-1,lock.getHoldCount());
318 <        }
310 >        ReentrantLock lock = new ReentrantLock();
311 >        for (int i = 1; i <= SIZE; i++) {
312 >            lock.lock();
313 >            assertEquals(i, lock.getHoldCount());
314 >        }
315 >        for (int i = SIZE; i > 0; i--) {
316 >            lock.unlock();
317 >            assertEquals(i-1, lock.getHoldCount());
318 >        }
319      }
320 <    
321 <  
320 >
321 >
322      /**
323       * isLocked is true when locked and false when not
324       */
325 <    public void testIsLocked() {
326 <        final ReentrantLock lock = new ReentrantLock();
327 <        lock.lock();
328 <        assertTrue(lock.isLocked());
329 <        lock.unlock();
330 <        assertFalse(lock.isLocked());
331 <        Thread t = new Thread(new Runnable() {
332 <                public void run() {
333 <                    lock.lock();
334 <                    try {
335 <                        Thread.sleep(SMALL_DELAY_MS);
336 <                    }
337 <                    catch(Exception e) {
338 <                        threadUnexpectedException();
339 <                    }
340 <                    lock.unlock();
341 <                }
390 <            });
391 <        try {
392 <            t.start();
393 <            Thread.sleep(SHORT_DELAY_MS);
394 <            assertTrue(lock.isLocked());
395 <            t.join();
396 <            assertFalse(lock.isLocked());
397 <        } catch(Exception e){
398 <            unexpectedException();
399 <        }
325 >    public void testIsLocked() throws InterruptedException {
326 >        final ReentrantLock lock = new ReentrantLock();
327 >        lock.lock();
328 >        assertTrue(lock.isLocked());
329 >        lock.unlock();
330 >        assertFalse(lock.isLocked());
331 >        Thread t = newStartedThread(new CheckedRunnable() {
332 >            public void realRun() throws InterruptedException {
333 >                lock.lock();
334 >                delay(SMALL_DELAY_MS);
335 >                lock.unlock();
336 >            }});
337 >
338 >        delay(SHORT_DELAY_MS);
339 >        assertTrue(lock.isLocked());
340 >        t.join();
341 >        assertFalse(lock.isLocked());
342      }
343  
344  
345      /**
346       * lockInterruptibly is interruptible.
347       */
348 <    public void testLockInterruptibly1() {
349 <        final ReentrantLock lock = new ReentrantLock();
350 <        lock.lock();
351 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
352 <        try {
353 <            t.start();
354 <            Thread.sleep(SHORT_DELAY_MS);
355 <            t.interrupt();
356 <            lock.unlock();
357 <            t.join();
416 <        } catch(Exception e){
417 <            unexpectedException();
418 <        }
419 <    }
348 >    public void testLockInterruptibly1() throws InterruptedException {
349 >        final ReentrantLock lock = new ReentrantLock();
350 >        lock.lock();
351 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
352 >        delay(SHORT_DELAY_MS);
353 >        t.interrupt();
354 >        delay(SHORT_DELAY_MS);
355 >        lock.unlock();
356 >        t.join();
357 >    }
358  
359      /**
360       * lockInterruptibly succeeds when unlocked, else is interruptible
361       */
362 <    public void testLockInterruptibly2() {
363 <        final ReentrantLock lock = new ReentrantLock();
364 <        try {
365 <            lock.lockInterruptibly();
366 <        } catch(Exception e) {
367 <            unexpectedException();
368 <        }
369 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
370 <        try {
433 <            t.start();
434 <            t.interrupt();
435 <            assertTrue(lock.isLocked());
436 <            assertTrue(lock.isHeldByCurrentThread());
437 <            t.join();
438 <        } catch(Exception e){
439 <            unexpectedException();
440 <        }
362 >    public void testLockInterruptibly2() throws InterruptedException {
363 >        final ReentrantLock lock = new ReentrantLock();
364 >        lock.lockInterruptibly();
365 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
366 >        delay(SHORT_DELAY_MS);
367 >        t.interrupt();
368 >        assertTrue(lock.isLocked());
369 >        assertTrue(lock.isHeldByCurrentThread());
370 >        t.join();
371      }
372  
373      /**
374       * Calling await without holding lock throws IllegalMonitorStateException
375       */
376 <    public void testAwait_IllegalMonitor() {
377 <        final ReentrantLock lock = new ReentrantLock();
376 >    public void testAwait_IllegalMonitor() throws InterruptedException {
377 >        final ReentrantLock lock = new ReentrantLock();
378          final Condition c = lock.newCondition();
379          try {
380              c.await();
381              shouldThrow();
382 <        }
453 <        catch (IllegalMonitorStateException success) {
454 <        }
455 <        catch (Exception ex) {
456 <            unexpectedException();
457 <        }
382 >        } catch (IllegalMonitorStateException success) {}
383      }
384  
385      /**
386       * Calling signal without holding lock throws IllegalMonitorStateException
387       */
388      public void testSignal_IllegalMonitor() {
389 <        final ReentrantLock lock = new ReentrantLock();
389 >        final ReentrantLock lock = new ReentrantLock();
390          final Condition c = lock.newCondition();
391          try {
392              c.signal();
393              shouldThrow();
394 <        }
470 <        catch (IllegalMonitorStateException success) {
471 <        }
472 <        catch (Exception ex) {
473 <            unexpectedException();
474 <        }
394 >        } catch (IllegalMonitorStateException success) {}
395      }
396  
397      /**
398       * awaitNanos without a signal times out
399       */
400 <    public void testAwaitNanos_Timeout() {
401 <        final ReentrantLock lock = new ReentrantLock();
400 >    public void testAwaitNanos_Timeout() throws InterruptedException {
401 >        final ReentrantLock lock = new ReentrantLock();
402          final Condition c = lock.newCondition();
403 <        try {
404 <            lock.lock();
405 <            long t = c.awaitNanos(100);
406 <            assertTrue(t <= 0);
487 <            lock.unlock();
488 <        }
489 <        catch (Exception ex) {
490 <            unexpectedException();
491 <        }
403 >        lock.lock();
404 >        long t = c.awaitNanos(100);
405 >        assertTrue(t <= 0);
406 >        lock.unlock();
407      }
408  
409      /**
410 <     *  timed await without a signal times out
410 >     * timed await without a signal times out
411       */
412 <    public void testAwait_Timeout() {
413 <        final ReentrantLock lock = new ReentrantLock();
412 >    public void testAwait_Timeout() throws InterruptedException {
413 >        final ReentrantLock lock = new ReentrantLock();
414          final Condition c = lock.newCondition();
415 <        try {
416 <            lock.lock();
417 <            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
503 <            lock.unlock();
504 <        }
505 <        catch (Exception ex) {
506 <            unexpectedException();
507 <        }
415 >        lock.lock();
416 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
417 >        lock.unlock();
418      }
419  
420      /**
421       * awaitUntil without a signal times out
422       */
423 <    public void testAwaitUntil_Timeout() {
424 <        final ReentrantLock lock = new ReentrantLock();
423 >    public void testAwaitUntil_Timeout() throws InterruptedException {
424 >        final ReentrantLock lock = new ReentrantLock();
425          final Condition c = lock.newCondition();
426 <        try {
427 <            lock.lock();
428 <            java.util.Date d = new java.util.Date();
429 <            c.awaitUntil(new java.util.Date(d.getTime() + 10));
520 <            lock.unlock();
521 <        }
522 <        catch (Exception ex) {
523 <            unexpectedException();
524 <        }
426 >        lock.lock();
427 >        java.util.Date d = new java.util.Date();
428 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
429 >        lock.unlock();
430      }
431  
432      /**
433       * await returns when signalled
434       */
435 <    public void testAwait() {
436 <        final ReentrantLock lock = new ReentrantLock();
435 >    public void testAwait() throws InterruptedException {
436 >        final ReentrantLock lock = new ReentrantLock();
437          final Condition c = lock.newCondition();
438 <        Thread t = new Thread(new Runnable() {
439 <                public void run() {
440 <                    try {
441 <                        lock.lock();
442 <                        c.await();
443 <                        lock.unlock();
539 <                    }
540 <                    catch(InterruptedException e) {
541 <                        threadUnexpectedException();
542 <                    }
543 <                }
544 <            });
438 >        Thread t = newStartedThread(new CheckedRunnable() {
439 >            public void realRun() throws InterruptedException {
440 >                lock.lock();
441 >                c.await();
442 >                lock.unlock();
443 >            }});
444  
445 <        try {
446 <            t.start();
447 <            Thread.sleep(SHORT_DELAY_MS);
448 <            lock.lock();
449 <            c.signal();
450 <            lock.unlock();
552 <            t.join(SHORT_DELAY_MS);
553 <            assertFalse(t.isAlive());
554 <        }
555 <        catch (Exception ex) {
556 <            unexpectedException();
557 <        }
445 >        delay(SHORT_DELAY_MS);
446 >        lock.lock();
447 >        c.signal();
448 >        lock.unlock();
449 >        t.join(SHORT_DELAY_MS);
450 >        assertFalse(t.isAlive());
451      }
452  
453      /**
454       * hasWaiters throws NPE if null
455       */
456      public void testHasWaitersNPE() {
457 <        final ReentrantLock lock = new ReentrantLock();
457 >        final ReentrantLock lock = new ReentrantLock();
458          try {
459              lock.hasWaiters(null);
460              shouldThrow();
461 <        } catch (NullPointerException success) {
569 <        } catch (Exception ex) {
570 <            unexpectedException();
571 <        }
461 >        } catch (NullPointerException success) {}
462      }
463  
464      /**
465       * getWaitQueueLength throws NPE if null
466       */
467      public void testGetWaitQueueLengthNPE() {
468 <        final ReentrantLock lock = new ReentrantLock();
468 >        final ReentrantLock lock = new ReentrantLock();
469          try {
470              lock.getWaitQueueLength(null);
471              shouldThrow();
472 <        } catch (NullPointerException success) {
583 <        } catch (Exception ex) {
584 <            unexpectedException();
585 <        }
472 >        } catch (NullPointerException success) {}
473      }
474  
475  
# Line 590 | Line 477 | public class ReentrantLockTest extends J
477       * getWaitingThreads throws NPE if null
478       */
479      public void testGetWaitingThreadsNPE() {
480 <        final PublicReentrantLock lock = new PublicReentrantLock();
480 >        final PublicReentrantLock lock = new PublicReentrantLock();
481          try {
482              lock.getWaitingThreads(null);
483              shouldThrow();
484 <        } catch (NullPointerException success) {
598 <        } catch (Exception ex) {
599 <            unexpectedException();
600 <        }
484 >        } catch (NullPointerException success) {}
485      }
486  
487  
# Line 605 | Line 489 | public class ReentrantLockTest extends J
489       * hasWaiters throws IAE if not owned
490       */
491      public void testHasWaitersIAE() {
492 <        final ReentrantLock lock = new ReentrantLock();
493 <        final Condition c = (lock.newCondition());
494 <        final ReentrantLock lock2 = new ReentrantLock();
492 >        final ReentrantLock lock = new ReentrantLock();
493 >        final Condition c = lock.newCondition();
494 >        final ReentrantLock lock2 = new ReentrantLock();
495          try {
496              lock2.hasWaiters(c);
497              shouldThrow();
498 <        } catch (IllegalArgumentException success) {
615 <        } catch (Exception ex) {
616 <            unexpectedException();
617 <        }
498 >        } catch (IllegalArgumentException success) {}
499      }
500  
501      /**
502       * hasWaiters throws IMSE if not locked
503       */
504      public void testHasWaitersIMSE() {
505 <        final ReentrantLock lock = new ReentrantLock();
506 <        final Condition c = (lock.newCondition());
505 >        final ReentrantLock lock = new ReentrantLock();
506 >        final Condition c = lock.newCondition();
507          try {
508              lock.hasWaiters(c);
509              shouldThrow();
510 <        } catch (IllegalMonitorStateException success) {
630 <        } catch (Exception ex) {
631 <            unexpectedException();
632 <        }
510 >        } catch (IllegalMonitorStateException success) {}
511      }
512  
513  
# Line 637 | Line 515 | public class ReentrantLockTest extends J
515       * getWaitQueueLength throws IAE if not owned
516       */
517      public void testGetWaitQueueLengthIAE() {
518 <        final ReentrantLock lock = new ReentrantLock();
519 <        final Condition c = (lock.newCondition());
520 <        final ReentrantLock lock2 = new ReentrantLock();
518 >        final ReentrantLock lock = new ReentrantLock();
519 >        final Condition c = lock.newCondition();
520 >        final ReentrantLock lock2 = new ReentrantLock();
521          try {
522              lock2.getWaitQueueLength(c);
523              shouldThrow();
524 <        } catch (IllegalArgumentException success) {
647 <        } catch (Exception ex) {
648 <            unexpectedException();
649 <        }
524 >        } catch (IllegalArgumentException success) {}
525      }
526  
527      /**
528       * getWaitQueueLength throws IMSE if not locked
529       */
530      public void testGetWaitQueueLengthIMSE() {
531 <        final ReentrantLock lock = new ReentrantLock();
532 <        final Condition c = (lock.newCondition());
531 >        final ReentrantLock lock = new ReentrantLock();
532 >        final Condition c = lock.newCondition();
533          try {
534              lock.getWaitQueueLength(c);
535              shouldThrow();
536 <        } catch (IllegalMonitorStateException success) {
662 <        } catch (Exception ex) {
663 <            unexpectedException();
664 <        }
536 >        } catch (IllegalMonitorStateException success) {}
537      }
538  
539  
# Line 669 | Line 541 | public class ReentrantLockTest extends J
541       * getWaitingThreads throws IAE if not owned
542       */
543      public void testGetWaitingThreadsIAE() {
544 <        final PublicReentrantLock lock = new PublicReentrantLock();    
545 <        final Condition c = (lock.newCondition());
546 <        final PublicReentrantLock lock2 = new PublicReentrantLock();    
544 >        final PublicReentrantLock lock = new PublicReentrantLock();
545 >        final Condition c = lock.newCondition();
546 >        final PublicReentrantLock lock2 = new PublicReentrantLock();
547          try {
548              lock2.getWaitingThreads(c);
549              shouldThrow();
550 <        } catch (IllegalArgumentException success) {
679 <        } catch (Exception ex) {
680 <            unexpectedException();
681 <        }
550 >        } catch (IllegalArgumentException success) {}
551      }
552  
553      /**
554       * getWaitingThreads throws IMSE if not locked
555       */
556      public void testGetWaitingThreadsIMSE() {
557 <        final PublicReentrantLock lock = new PublicReentrantLock();    
558 <        final Condition c = (lock.newCondition());
557 >        final PublicReentrantLock lock = new PublicReentrantLock();
558 >        final Condition c = lock.newCondition();
559          try {
560              lock.getWaitingThreads(c);
561              shouldThrow();
562 <        } catch (IllegalMonitorStateException success) {
694 <        } catch (Exception ex) {
695 <            unexpectedException();
696 <        }
562 >        } catch (IllegalMonitorStateException success) {}
563      }
564  
565  
700
566      /**
567       * hasWaiters returns true when a thread is waiting, else false
568       */
569 <    public void testHasWaiters() {
570 <        final ReentrantLock lock = new ReentrantLock();
569 >    public void testHasWaiters() throws InterruptedException {
570 >        final ReentrantLock lock = new ReentrantLock();
571          final Condition c = lock.newCondition();
572 <        Thread t = new Thread(new Runnable() {
573 <                public void run() {
574 <                    try {
575 <                        lock.lock();
576 <                        threadAssertFalse(lock.hasWaiters(c));
577 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
578 <                        c.await();
579 <                        lock.unlock();
715 <                    }
716 <                    catch(InterruptedException e) {
717 <                        threadUnexpectedException();
718 <                    }
719 <                }
720 <            });
572 >        Thread t = newStartedThread(new CheckedRunnable() {
573 >            public void realRun() throws InterruptedException {
574 >                lock.lock();
575 >                assertFalse(lock.hasWaiters(c));
576 >                assertEquals(0, lock.getWaitQueueLength(c));
577 >                c.await();
578 >                lock.unlock();
579 >            }});
580  
581 <        try {
582 <            t.start();
583 <            Thread.sleep(SHORT_DELAY_MS);
584 <            lock.lock();
585 <            assertTrue(lock.hasWaiters(c));
586 <            assertEquals(1, lock.getWaitQueueLength(c));
587 <            c.signal();
588 <            lock.unlock();
589 <            Thread.sleep(SHORT_DELAY_MS);
590 <            lock.lock();
591 <            assertFalse(lock.hasWaiters(c));
592 <            assertEquals(0, lock.getWaitQueueLength(c));
593 <            lock.unlock();
735 <            t.join(SHORT_DELAY_MS);
736 <            assertFalse(t.isAlive());
737 <        }
738 <        catch (Exception ex) {
739 <            unexpectedException();
740 <        }
581 >        delay(SHORT_DELAY_MS);
582 >        lock.lock();
583 >        assertTrue(lock.hasWaiters(c));
584 >        assertEquals(1, lock.getWaitQueueLength(c));
585 >        c.signal();
586 >        lock.unlock();
587 >        delay(SHORT_DELAY_MS);
588 >        lock.lock();
589 >        assertFalse(lock.hasWaiters(c));
590 >        assertEquals(0, lock.getWaitQueueLength(c));
591 >        lock.unlock();
592 >        t.join(SHORT_DELAY_MS);
593 >        assertFalse(t.isAlive());
594      }
595  
596      /**
597       * getWaitQueueLength returns number of waiting threads
598       */
599 <    public void testGetWaitQueueLength() {
600 <        final ReentrantLock lock = new ReentrantLock();
599 >    public void testGetWaitQueueLength() throws InterruptedException {
600 >        final ReentrantLock lock = new ReentrantLock();
601          final Condition c = lock.newCondition();
602 <        Thread t1 = new Thread(new Runnable() {
603 <                public void run() {
604 <                    try {
605 <                        lock.lock();
606 <                        threadAssertFalse(lock.hasWaiters(c));
607 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
608 <                        c.await();
609 <                        lock.unlock();
757 <                    }
758 <                    catch(InterruptedException e) {
759 <                        threadUnexpectedException();
760 <                    }
761 <                }
762 <            });
763 <
764 <        Thread t2 = new Thread(new Runnable() {
765 <                public void run() {
766 <                    try {
767 <                        lock.lock();
768 <                        threadAssertTrue(lock.hasWaiters(c));
769 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
770 <                        c.await();
771 <                        lock.unlock();
772 <                    }
773 <                    catch(InterruptedException e) {
774 <                        threadUnexpectedException();
775 <                    }
776 <                }
777 <            });
602 >        Thread t1 = newStartedThread(new CheckedRunnable() {
603 >            public void realRun() throws InterruptedException {
604 >                lock.lock();
605 >                assertFalse(lock.hasWaiters(c));
606 >                assertEquals(0, lock.getWaitQueueLength(c));
607 >                c.await();
608 >                lock.unlock();
609 >            }});
610  
611 <        try {
612 <            t1.start();
613 <            Thread.sleep(SHORT_DELAY_MS);
614 <            t2.start();
615 <            Thread.sleep(SHORT_DELAY_MS);
616 <            lock.lock();
617 <            assertTrue(lock.hasWaiters(c));
618 <            assertEquals(2, lock.getWaitQueueLength(c));
619 <            c.signalAll();
620 <            lock.unlock();
621 <            Thread.sleep(SHORT_DELAY_MS);
622 <            lock.lock();
623 <            assertFalse(lock.hasWaiters(c));
624 <            assertEquals(0, lock.getWaitQueueLength(c));
625 <            lock.unlock();
626 <            t1.join(SHORT_DELAY_MS);
627 <            t2.join(SHORT_DELAY_MS);
628 <            assertFalse(t1.isAlive());
629 <            assertFalse(t2.isAlive());
630 <        }
631 <        catch (Exception ex) {
632 <            unexpectedException();
633 <        }
611 >        delay(SHORT_DELAY_MS);
612 >
613 >        Thread t2 = newStartedThread(new CheckedRunnable() {
614 >            public void realRun() throws InterruptedException {
615 >                lock.lock();
616 >                assertTrue(lock.hasWaiters(c));
617 >                assertEquals(1, lock.getWaitQueueLength(c));
618 >                c.await();
619 >                lock.unlock();
620 >            }});
621 >
622 >        delay(SHORT_DELAY_MS);
623 >        lock.lock();
624 >        assertTrue(lock.hasWaiters(c));
625 >        assertEquals(2, lock.getWaitQueueLength(c));
626 >        c.signalAll();
627 >        lock.unlock();
628 >        delay(SHORT_DELAY_MS);
629 >        lock.lock();
630 >        assertFalse(lock.hasWaiters(c));
631 >        assertEquals(0, lock.getWaitQueueLength(c));
632 >        lock.unlock();
633 >        t1.join(SHORT_DELAY_MS);
634 >        t2.join(SHORT_DELAY_MS);
635 >        assertFalse(t1.isAlive());
636 >        assertFalse(t2.isAlive());
637      }
638  
639      /**
640       * getWaitingThreads returns only and all waiting threads
641       */
642 <    public void testGetWaitingThreads() {
643 <        final PublicReentrantLock lock = new PublicReentrantLock();    
642 >    public void testGetWaitingThreads() throws InterruptedException {
643 >        final PublicReentrantLock lock = new PublicReentrantLock();
644          final Condition c = lock.newCondition();
645 <        Thread t1 = new Thread(new Runnable() {
646 <                public void run() {
647 <                    try {
648 <                        lock.lock();
649 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
650 <                        c.await();
651 <                        lock.unlock();
817 <                    }
818 <                    catch(InterruptedException e) {
819 <                        threadUnexpectedException();
820 <                    }
821 <                }
822 <            });
823 <
824 <        Thread t2 = new Thread(new Runnable() {
825 <                public void run() {
826 <                    try {
827 <                        lock.lock();
828 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
829 <                        c.await();
830 <                        lock.unlock();
831 <                    }
832 <                    catch(InterruptedException e) {
833 <                        threadUnexpectedException();
834 <                    }
835 <                }
836 <            });
645 >        Thread t1 = new Thread(new CheckedRunnable() {
646 >            public void realRun() throws InterruptedException {
647 >                lock.lock();
648 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
649 >                c.await();
650 >                lock.unlock();
651 >            }});
652  
653 <        try {
654 <            lock.lock();
655 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
656 <            lock.unlock();
657 <            t1.start();
658 <            Thread.sleep(SHORT_DELAY_MS);
659 <            t2.start();
660 <            Thread.sleep(SHORT_DELAY_MS);
661 <            lock.lock();
662 <            assertTrue(lock.hasWaiters(c));
663 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
664 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
665 <            c.signalAll();
666 <            lock.unlock();
667 <            Thread.sleep(SHORT_DELAY_MS);
668 <            lock.lock();
669 <            assertFalse(lock.hasWaiters(c));
670 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
671 <            lock.unlock();
672 <            t1.join(SHORT_DELAY_MS);
673 <            t2.join(SHORT_DELAY_MS);
674 <            assertFalse(t1.isAlive());
675 <            assertFalse(t2.isAlive());
676 <        }
677 <        catch (Exception ex) {
678 <            unexpectedException();
679 <        }
653 >        Thread t2 = new Thread(new CheckedRunnable() {
654 >            public void realRun() throws InterruptedException {
655 >                lock.lock();
656 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
657 >                c.await();
658 >                lock.unlock();
659 >            }});
660 >
661 >        lock.lock();
662 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
663 >        lock.unlock();
664 >        t1.start();
665 >        delay(SHORT_DELAY_MS);
666 >        t2.start();
667 >        delay(SHORT_DELAY_MS);
668 >        lock.lock();
669 >        assertTrue(lock.hasWaiters(c));
670 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
671 >        assertTrue(lock.getWaitingThreads(c).contains(t2));
672 >        c.signalAll();
673 >        lock.unlock();
674 >        delay(SHORT_DELAY_MS);
675 >        lock.lock();
676 >        assertFalse(lock.hasWaiters(c));
677 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
678 >        lock.unlock();
679 >        t1.join(SHORT_DELAY_MS);
680 >        t2.join(SHORT_DELAY_MS);
681 >        assertFalse(t1.isAlive());
682 >        assertFalse(t2.isAlive());
683      }
684  
685      /** A helper class for uninterruptible wait tests */
686 <    class UninterruptableThread extends Thread {
686 >    class UninterruptibleThread extends Thread {
687          private ReentrantLock lock;
688          private Condition c;
689 <        
689 >
690          public volatile boolean canAwake = false;
691          public volatile boolean interrupted = false;
692          public volatile boolean lockStarted = false;
693 <        
694 <        public UninterruptableThread(ReentrantLock lock, Condition c) {
693 >
694 >        public UninterruptibleThread(ReentrantLock lock, Condition c) {
695              this.lock = lock;
696              this.c = c;
697          }
698 <        
698 >
699          public synchronized void run() {
700              lock.lock();
701              lockStarted = true;
702 <            
702 >
703              while (!canAwake) {
704                  c.awaitUninterruptibly();
705              }
706 <            
706 >
707              interrupted = isInterrupted();
708              lock.unlock();
709          }
# Line 894 | Line 712 | public class ReentrantLockTest extends J
712      /**
713       * awaitUninterruptibly doesn't abort on interrupt
714       */
715 <    public void testAwaitUninterruptibly() {
715 >    public void testAwaitUninterruptibly() throws InterruptedException {
716          final ReentrantLock lock = new ReentrantLock();
717          final Condition c = lock.newCondition();
718 <        UninterruptableThread thread = new UninterruptableThread(lock, c);
718 >        UninterruptibleThread thread = new UninterruptibleThread(lock, c);
719  
720 <        try {
903 <            thread.start();
720 >        thread.start();
721  
722 <            while (!thread.lockStarted) {
723 <                Thread.sleep(100);
724 <            }
908 <
909 <            lock.lock();
910 <            try {
911 <                thread.interrupt();
912 <                thread.canAwake = true;
913 <                c.signal();
914 <            } finally {
915 <                lock.unlock();
916 <            }
722 >        while (!thread.lockStarted) {
723 >            delay(100);
724 >        }
725  
726 <            thread.join();
727 <            assertTrue(thread.interrupted);
728 <            assertFalse(thread.isAlive());
729 <        } catch (Exception ex) {
730 <            unexpectedException();
726 >        lock.lock();
727 >        try {
728 >            thread.interrupt();
729 >            thread.canAwake = true;
730 >            c.signal();
731 >        } finally {
732 >            lock.unlock();
733          }
734 +
735 +        thread.join();
736 +        assertTrue(thread.interrupted);
737 +        assertFalse(thread.isAlive());
738      }
739  
740      /**
741       * await is interruptible
742       */
743 <    public void testAwait_Interrupt() {
744 <        final ReentrantLock lock = new ReentrantLock();
743 >    public void testAwait_Interrupt() throws InterruptedException {
744 >        final ReentrantLock lock = new ReentrantLock();
745          final Condition c = lock.newCondition();
746 <        Thread t = new Thread(new Runnable() {
747 <                public void run() {
748 <                    try {
749 <                        lock.lock();
750 <                        c.await();
751 <                        lock.unlock();
752 <                        threadShouldThrow();
753 <                    }
754 <                    catch(InterruptedException success) {
755 <                    }
942 <                }
943 <            });
944 <
945 <        try {
946 <            t.start();
947 <            Thread.sleep(SHORT_DELAY_MS);
948 <            t.interrupt();
949 <            t.join(SHORT_DELAY_MS);
950 <            assertFalse(t.isAlive());
951 <        }
952 <        catch (Exception ex) {
953 <            unexpectedException();
954 <        }
746 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
747 >            public void realRun() throws InterruptedException {
748 >                lock.lock();
749 >                c.await();
750 >            }});
751 >
752 >        delay(SHORT_DELAY_MS);
753 >        t.interrupt();
754 >        t.join(SHORT_DELAY_MS);
755 >        assertFalse(t.isAlive());
756      }
757  
758      /**
759       * awaitNanos is interruptible
760       */
761 <    public void testAwaitNanos_Interrupt() {
762 <        final ReentrantLock lock = new ReentrantLock();
761 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
762 >        final ReentrantLock lock = new ReentrantLock();
763          final Condition c = lock.newCondition();
764 <        Thread t = new Thread(new Runnable() {
765 <                public void run() {
766 <                    try {
767 <                        lock.lock();
768 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
769 <                        lock.unlock();
770 <                        threadShouldThrow();
771 <                    }
772 <                    catch(InterruptedException success) {
773 <                    }
973 <                }
974 <            });
975 <
976 <        try {
977 <            t.start();
978 <            Thread.sleep(SHORT_DELAY_MS);
979 <            t.interrupt();
980 <            t.join(SHORT_DELAY_MS);
981 <            assertFalse(t.isAlive());
982 <        }
983 <        catch (Exception ex) {
984 <            unexpectedException();
985 <        }
764 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
765 >            public void realRun() throws InterruptedException {
766 >                lock.lock();
767 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
768 >            }});
769 >
770 >        delay(SHORT_DELAY_MS);
771 >        t.interrupt();
772 >        t.join(SHORT_DELAY_MS);
773 >        assertFalse(t.isAlive());
774      }
775  
776      /**
777       * awaitUntil is interruptible
778       */
779 <    public void testAwaitUntil_Interrupt() {
780 <        final ReentrantLock lock = new ReentrantLock();
779 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
780 >        final ReentrantLock lock = new ReentrantLock();
781          final Condition c = lock.newCondition();
782 <        Thread t = new Thread(new Runnable() {
783 <                public void run() {
784 <                    try {
785 <                        lock.lock();
786 <                        java.util.Date d = new java.util.Date();
787 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
788 <                        lock.unlock();
789 <                        threadShouldThrow();
790 <                    }
791 <                    catch(InterruptedException success) {
792 <                    }
1005 <                }
1006 <            });
1007 <
1008 <        try {
1009 <            t.start();
1010 <            Thread.sleep(SHORT_DELAY_MS);
1011 <            t.interrupt();
1012 <            t.join(SHORT_DELAY_MS);
1013 <            assertFalse(t.isAlive());
1014 <        }
1015 <        catch (Exception ex) {
1016 <            unexpectedException();
1017 <        }
782 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
783 >            public void realRun() throws InterruptedException {
784 >                lock.lock();
785 >                java.util.Date d = new java.util.Date();
786 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
787 >            }});
788 >
789 >        delay(SHORT_DELAY_MS);
790 >        t.interrupt();
791 >        t.join(SHORT_DELAY_MS);
792 >        assertFalse(t.isAlive());
793      }
794  
795      /**
796       * signalAll wakes up all threads
797       */
798 <    public void testSignalAll() {
799 <        final ReentrantLock lock = new ReentrantLock();
798 >    public void testSignalAll() throws InterruptedException {
799 >        final ReentrantLock lock = new ReentrantLock();
800          final Condition c = lock.newCondition();
801 <        Thread t1 = new Thread(new Runnable() {
802 <                public void run() {
803 <                    try {
804 <                        lock.lock();
805 <                        c.await();
806 <                        lock.unlock();
1032 <                    }
1033 <                    catch(InterruptedException e) {
1034 <                        threadUnexpectedException();
1035 <                    }
1036 <                }
1037 <            });
1038 <
1039 <        Thread t2 = new Thread(new Runnable() {
1040 <                public void run() {
1041 <                    try {
1042 <                        lock.lock();
1043 <                        c.await();
1044 <                        lock.unlock();
1045 <                    }
1046 <                    catch(InterruptedException e) {
1047 <                        threadUnexpectedException();
1048 <                    }
1049 <                }
1050 <            });
801 >        Thread t1 = newStartedThread(new CheckedRunnable() {
802 >            public void realRun() throws InterruptedException {
803 >                lock.lock();
804 >                c.await();
805 >                lock.unlock();
806 >            }});
807  
808 <        try {
809 <            t1.start();
810 <            t2.start();
811 <            Thread.sleep(SHORT_DELAY_MS);
812 <            lock.lock();
813 <            c.signalAll();
814 <            lock.unlock();
815 <            t1.join(SHORT_DELAY_MS);
816 <            t2.join(SHORT_DELAY_MS);
817 <            assertFalse(t1.isAlive());
818 <            assertFalse(t2.isAlive());
819 <        }
820 <        catch (Exception ex) {
821 <            unexpectedException();
822 <        }
808 >        Thread t2 = newStartedThread(new CheckedRunnable() {
809 >            public void realRun() throws InterruptedException {
810 >                lock.lock();
811 >                c.await();
812 >                lock.unlock();
813 >            }});
814 >
815 >        delay(SHORT_DELAY_MS);
816 >        lock.lock();
817 >        c.signalAll();
818 >        lock.unlock();
819 >        t1.join(SHORT_DELAY_MS);
820 >        t2.join(SHORT_DELAY_MS);
821 >        assertFalse(t1.isAlive());
822 >        assertFalse(t2.isAlive());
823      }
824  
825      /**
826       * await after multiple reentrant locking preserves lock count
827       */
828 <    public void testAwaitLockCount() {
829 <        final ReentrantLock lock = new ReentrantLock();
828 >    public void testAwaitLockCount() throws InterruptedException {
829 >        final ReentrantLock lock = new ReentrantLock();
830          final Condition c = lock.newCondition();
831 <        Thread t1 = new Thread(new Runnable() {
832 <                public void run() {
833 <                    try {
834 <                        lock.lock();
835 <                        threadAssertEquals(1, lock.getHoldCount());
836 <                        c.await();
837 <                        threadAssertEquals(1, lock.getHoldCount());
838 <                        lock.unlock();
1083 <                    }
1084 <                    catch(InterruptedException e) {
1085 <                        threadUnexpectedException();
1086 <                    }
1087 <                }
1088 <            });
1089 <
1090 <        Thread t2 = new Thread(new Runnable() {
1091 <                public void run() {
1092 <                    try {
1093 <                        lock.lock();
1094 <                        lock.lock();
1095 <                        threadAssertEquals(2, lock.getHoldCount());
1096 <                        c.await();
1097 <                        threadAssertEquals(2, lock.getHoldCount());
1098 <                        lock.unlock();
1099 <                        lock.unlock();
1100 <                    }
1101 <                    catch(InterruptedException e) {
1102 <                        threadUnexpectedException();
1103 <                    }
1104 <                }
1105 <            });
831 >        Thread t1 = newStartedThread(new CheckedRunnable() {
832 >            public void realRun() throws InterruptedException {
833 >                lock.lock();
834 >                assertEquals(1, lock.getHoldCount());
835 >                c.await();
836 >                assertEquals(1, lock.getHoldCount());
837 >                lock.unlock();
838 >            }});
839  
840 <        try {
841 <            t1.start();
842 <            t2.start();
843 <            Thread.sleep(SHORT_DELAY_MS);
844 <            lock.lock();
845 <            c.signalAll();
846 <            lock.unlock();
847 <            t1.join(SHORT_DELAY_MS);
848 <            t2.join(SHORT_DELAY_MS);
849 <            assertFalse(t1.isAlive());
850 <            assertFalse(t2.isAlive());
851 <        }
852 <        catch (Exception ex) {
853 <            unexpectedException();
854 <        }
840 >        Thread t2 = newStartedThread(new CheckedRunnable() {
841 >            public void realRun() throws InterruptedException {
842 >                lock.lock();
843 >                lock.lock();
844 >                assertEquals(2, lock.getHoldCount());
845 >                c.await();
846 >                assertEquals(2, lock.getHoldCount());
847 >                lock.unlock();
848 >                lock.unlock();
849 >            }});
850 >
851 >        delay(SHORT_DELAY_MS);
852 >        lock.lock();
853 >        c.signalAll();
854 >        lock.unlock();
855 >        t1.join(SHORT_DELAY_MS);
856 >        t2.join(SHORT_DELAY_MS);
857 >        assertFalse(t1.isAlive());
858 >        assertFalse(t2.isAlive());
859      }
860  
861      /**
862       * A serialized lock deserializes as unlocked
863       */
864 <    public void testSerialization() {
864 >    public void testSerialization() throws Exception {
865          ReentrantLock l = new ReentrantLock();
866          l.lock();
867          l.unlock();
868  
869 <        try {
870 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
871 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
872 <            out.writeObject(l);
873 <            out.close();
874 <
875 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
876 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
877 <            ReentrantLock r = (ReentrantLock) in.readObject();
878 <            r.lock();
879 <            r.unlock();
880 <        } catch(Exception e){
881 <            e.printStackTrace();
1145 <            unexpectedException();
1146 <        }
869 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
870 >        ObjectOutputStream out =
871 >            new ObjectOutputStream(new BufferedOutputStream(bout));
872 >        out.writeObject(l);
873 >        out.close();
874 >
875 >        ByteArrayInputStream bin =
876 >            new ByteArrayInputStream(bout.toByteArray());
877 >        ObjectInputStream in =
878 >            new ObjectInputStream(new BufferedInputStream(bin));
879 >        ReentrantLock r = (ReentrantLock) in.readObject();
880 >        r.lock();
881 >        r.unlock();
882      }
883  
884      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines