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.27 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.40 by dl, Fri May 6 11:22:07 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 9 | Line 9
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 60 | Line 56 | public class ReentrantLockTest extends J
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());
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();
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);
85 >        ReentrantLock rl = new ReentrantLock(true);
86          rl.lock();
87          assertTrue(rl.isLocked());
88          rl.unlock();
# Line 98 | Line 92 | public class ReentrantLockTest extends J
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){}
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();
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();
143 <            t2.join();
144 <        } catch (Exception e){
145 <            unexpectedException();
146 <        }
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();
172 <            t2.join();
173 <        } catch (Exception e){
174 <            unexpectedException();
175 <        }
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();
201 <            t2.join();
202 <        } catch (Exception e){
203 <            unexpectedException();
204 <        }
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();
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();
247 <            t2.join();
248 <        } catch (Exception e){
249 <            unexpectedException();
250 <        }
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();
280 <            t2.join();
281 <        } catch (Exception e){
282 <            unexpectedException();
283 <        }
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 <            });
273 <        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 = new Thread(new CheckedInterruptedRunnable() {
266 >            public void realRun() throws InterruptedException {
267 >                lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS);
268 >            }});
269 >
270 >        t.start();
271 >        delay(SHORT_DELAY_MS);
272 >        t.interrupt();
273 >        t.join();
274      }
275  
276  
277      /**
278       * TryLock on a locked lock fails
279       */
280 <    public void testTryLockWhenLocked() {
281 <        final ReentrantLock lock = new ReentrantLock();
282 <        lock.lock();
283 <        Thread t = new Thread(new Runnable() {
284 <                public void run() {
285 <                    threadAssertFalse(lock.tryLock());
286 <                }
287 <            });
288 <        try {
289 <            t.start();
290 <            t.join();
324 <            lock.unlock();
325 <        } catch (Exception e){
326 <            unexpectedException();
327 <        }
280 >    public void testTryLockWhenLocked() throws InterruptedException {
281 >        final ReentrantLock lock = new ReentrantLock();
282 >        lock.lock();
283 >        Thread t = new Thread(new CheckedRunnable() {
284 >            public void realRun() {
285 >                assertFalse(lock.tryLock());
286 >            }});
287 >
288 >        t.start();
289 >        t.join();
290 >        lock.unlock();
291      }
292  
293      /**
294       * Timed tryLock on a locked lock times out
295       */
296 <    public void testTryLock_Timeout() {
297 <        final ReentrantLock lock = new ReentrantLock();
298 <        lock.lock();
299 <        Thread t = new Thread(new Runnable() {
300 <                public void run() {
301 <                    try {
302 <                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
303 <                    } catch (Exception ex) {
304 <                        threadUnexpectedException();
305 <                    }
306 <                }
344 <            });
345 <        try {
346 <            t.start();
347 <            t.join();
348 <            lock.unlock();
349 <        } catch (Exception e){
350 <            unexpectedException();
351 <        }
296 >    public void testTryLock_Timeout() throws InterruptedException {
297 >        final ReentrantLock lock = new ReentrantLock();
298 >        lock.lock();
299 >        Thread t = new Thread(new CheckedRunnable() {
300 >            public void realRun() throws InterruptedException {
301 >                assertFalse(lock.tryLock(1, MILLISECONDS));
302 >            }});
303 >
304 >        t.start();
305 >        t.join();
306 >        lock.unlock();
307      }
308  
309      /**
310       * getHoldCount returns number of recursive holds
311       */
312      public void testGetHoldCount() {
313 <        ReentrantLock lock = new ReentrantLock();
314 <        for (int i = 1; i <= SIZE; i++) {
315 <            lock.lock();
316 <            assertEquals(i,lock.getHoldCount());
317 <        }
318 <        for (int i = SIZE; i > 0; i--) {
319 <            lock.unlock();
320 <            assertEquals(i-1,lock.getHoldCount());
321 <        }
313 >        ReentrantLock lock = new ReentrantLock();
314 >        for (int i = 1; i <= SIZE; i++) {
315 >            lock.lock();
316 >            assertEquals(i, lock.getHoldCount());
317 >        }
318 >        for (int i = SIZE; i > 0; i--) {
319 >            lock.unlock();
320 >            assertEquals(i-1, lock.getHoldCount());
321 >        }
322      }
323  
324  
325      /**
326       * isLocked is true when locked and false when not
327       */
328 <    public void testIsLocked() {
329 <        final ReentrantLock lock = new ReentrantLock();
330 <        lock.lock();
331 <        assertTrue(lock.isLocked());
332 <        lock.unlock();
333 <        assertFalse(lock.isLocked());
334 <        Thread t = new Thread(new Runnable() {
335 <                public void run() {
336 <                    lock.lock();
337 <                    try {
338 <                        Thread.sleep(SMALL_DELAY_MS);
339 <                    }
340 <                    catch (Exception e) {
341 <                        threadUnexpectedException();
342 <                    }
343 <                    lock.unlock();
344 <                }
345 <            });
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 <        }
328 >    public void testIsLocked() throws InterruptedException {
329 >        final ReentrantLock lock = new ReentrantLock();
330 >        lock.lock();
331 >        assertTrue(lock.isLocked());
332 >        lock.unlock();
333 >        assertFalse(lock.isLocked());
334 >        Thread t = new Thread(new CheckedRunnable() {
335 >            public void realRun() throws InterruptedException {
336 >                lock.lock();
337 >                delay(SMALL_DELAY_MS);
338 >                lock.unlock();
339 >            }});
340 >
341 >        t.start();
342 >        delay(SHORT_DELAY_MS);
343 >        assertTrue(lock.isLocked());
344 >        t.join();
345 >        assertFalse(lock.isLocked());
346      }
347  
348  
349      /**
350       * lockInterruptibly is interruptible.
351       */
352 <    public void testLockInterruptibly1() {
353 <        final ReentrantLock lock = new ReentrantLock();
354 <        lock.lock();
355 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
356 <        try {
357 <            t.start();
358 <            Thread.sleep(SHORT_DELAY_MS);
359 <            t.interrupt();
360 <            Thread.sleep(SHORT_DELAY_MS);
361 <            lock.unlock();
416 <            t.join();
417 <        } catch (Exception e){
418 <            unexpectedException();
419 <        }
352 >    public void testLockInterruptibly1() throws InterruptedException {
353 >        final ReentrantLock lock = new ReentrantLock();
354 >        lock.lock();
355 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
356 >        t.start();
357 >        delay(SHORT_DELAY_MS);
358 >        t.interrupt();
359 >        delay(SHORT_DELAY_MS);
360 >        lock.unlock();
361 >        t.join();
362      }
363  
364      /**
365       * lockInterruptibly succeeds when unlocked, else is interruptible
366       */
367 <    public void testLockInterruptibly2() {
368 <        final ReentrantLock lock = new ReentrantLock();
369 <        try {
370 <            lock.lockInterruptibly();
371 <        } catch (Exception e) {
372 <            unexpectedException();
373 <        }
374 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
375 <        try {
376 <            t.start();
435 <            t.interrupt();
436 <            assertTrue(lock.isLocked());
437 <            assertTrue(lock.isHeldByCurrentThread());
438 <            t.join();
439 <        } catch (Exception e){
440 <            unexpectedException();
441 <        }
367 >    public void testLockInterruptibly2() throws InterruptedException {
368 >        final ReentrantLock lock = new ReentrantLock();
369 >        lock.lockInterruptibly();
370 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
371 >        t.start();
372 >        delay(SHORT_DELAY_MS);
373 >        t.interrupt();
374 >        assertTrue(lock.isLocked());
375 >        assertTrue(lock.isHeldByCurrentThread());
376 >        t.join();
377      }
378  
379      /**
380       * Calling await without holding lock throws IllegalMonitorStateException
381       */
382 <    public void testAwait_IllegalMonitor() {
383 <        final ReentrantLock lock = new ReentrantLock();
382 >    public void testAwait_IllegalMonitor() throws InterruptedException {
383 >        final ReentrantLock lock = new ReentrantLock();
384          final Condition c = lock.newCondition();
385          try {
386              c.await();
387              shouldThrow();
388 <        }
454 <        catch (IllegalMonitorStateException success) {
455 <        }
456 <        catch (Exception ex) {
457 <            unexpectedException();
458 <        }
388 >        } catch (IllegalMonitorStateException success) {}
389      }
390  
391      /**
392       * Calling signal without holding lock throws IllegalMonitorStateException
393       */
394      public void testSignal_IllegalMonitor() {
395 <        final ReentrantLock lock = new ReentrantLock();
395 >        final ReentrantLock lock = new ReentrantLock();
396          final Condition c = lock.newCondition();
397          try {
398              c.signal();
399              shouldThrow();
400 <        }
471 <        catch (IllegalMonitorStateException success) {
472 <        }
473 <        catch (Exception ex) {
474 <            unexpectedException();
475 <        }
400 >        } catch (IllegalMonitorStateException success) {}
401      }
402  
403      /**
404       * awaitNanos without a signal times out
405       */
406 <    public void testAwaitNanos_Timeout() {
407 <        final ReentrantLock lock = new ReentrantLock();
406 >    public void testAwaitNanos_Timeout() throws InterruptedException {
407 >        final ReentrantLock lock = new ReentrantLock();
408          final Condition c = lock.newCondition();
409 <        try {
410 <            lock.lock();
411 <            long t = c.awaitNanos(100);
412 <            assertTrue(t <= 0);
488 <            lock.unlock();
489 <        }
490 <        catch (Exception ex) {
491 <            unexpectedException();
492 <        }
409 >        lock.lock();
410 >        long t = c.awaitNanos(100);
411 >        assertTrue(t <= 0);
412 >        lock.unlock();
413      }
414  
415      /**
416 <     *  timed await without a signal times out
416 >     * timed await without a signal times out
417       */
418 <    public void testAwait_Timeout() {
419 <        final ReentrantLock lock = new ReentrantLock();
418 >    public void testAwait_Timeout() throws InterruptedException {
419 >        final ReentrantLock lock = new ReentrantLock();
420          final Condition c = lock.newCondition();
421 <        try {
422 <            lock.lock();
423 <            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
504 <            lock.unlock();
505 <        }
506 <        catch (Exception ex) {
507 <            unexpectedException();
508 <        }
421 >        lock.lock();
422 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
423 >        lock.unlock();
424      }
425  
426      /**
427       * awaitUntil without a signal times out
428       */
429 <    public void testAwaitUntil_Timeout() {
430 <        final ReentrantLock lock = new ReentrantLock();
429 >    public void testAwaitUntil_Timeout() throws InterruptedException {
430 >        final ReentrantLock lock = new ReentrantLock();
431          final Condition c = lock.newCondition();
432 <        try {
433 <            lock.lock();
434 <            java.util.Date d = new java.util.Date();
435 <            c.awaitUntil(new java.util.Date(d.getTime() + 10));
521 <            lock.unlock();
522 <        }
523 <        catch (Exception ex) {
524 <            unexpectedException();
525 <        }
432 >        lock.lock();
433 >        java.util.Date d = new java.util.Date();
434 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
435 >        lock.unlock();
436      }
437  
438      /**
439       * await returns when signalled
440       */
441 <    public void testAwait() {
442 <        final ReentrantLock lock = new ReentrantLock();
441 >    public void testAwait() throws InterruptedException {
442 >        final ReentrantLock lock = new ReentrantLock();
443          final Condition c = lock.newCondition();
444 <        Thread t = new Thread(new Runnable() {
445 <                public void run() {
446 <                    try {
447 <                        lock.lock();
448 <                        c.await();
449 <                        lock.unlock();
540 <                    }
541 <                    catch (InterruptedException e) {
542 <                        threadUnexpectedException();
543 <                    }
544 <                }
545 <            });
444 >        Thread t = new Thread(new CheckedRunnable() {
445 >            public void realRun() throws InterruptedException {
446 >                lock.lock();
447 >                c.await();
448 >                lock.unlock();
449 >            }});
450  
451 <        try {
452 <            t.start();
453 <            Thread.sleep(SHORT_DELAY_MS);
454 <            lock.lock();
455 <            c.signal();
456 <            lock.unlock();
457 <            t.join(SHORT_DELAY_MS);
554 <            assertFalse(t.isAlive());
555 <        }
556 <        catch (Exception ex) {
557 <            unexpectedException();
558 <        }
451 >        t.start();
452 >        delay(SHORT_DELAY_MS);
453 >        lock.lock();
454 >        c.signal();
455 >        lock.unlock();
456 >        t.join(SHORT_DELAY_MS);
457 >        assertFalse(t.isAlive());
458      }
459  
460      /**
461       * hasWaiters throws NPE if null
462       */
463      public void testHasWaitersNPE() {
464 <        final ReentrantLock lock = new ReentrantLock();
464 >        final ReentrantLock lock = new ReentrantLock();
465          try {
466              lock.hasWaiters(null);
467              shouldThrow();
468 <        } catch (NullPointerException success) {
570 <        } catch (Exception ex) {
571 <            unexpectedException();
572 <        }
468 >        } catch (NullPointerException success) {}
469      }
470  
471      /**
472       * getWaitQueueLength throws NPE if null
473       */
474      public void testGetWaitQueueLengthNPE() {
475 <        final ReentrantLock lock = new ReentrantLock();
475 >        final ReentrantLock lock = new ReentrantLock();
476          try {
477              lock.getWaitQueueLength(null);
478              shouldThrow();
479 <        } catch (NullPointerException success) {
584 <        } catch (Exception ex) {
585 <            unexpectedException();
586 <        }
479 >        } catch (NullPointerException success) {}
480      }
481  
482  
# Line 591 | Line 484 | public class ReentrantLockTest extends J
484       * getWaitingThreads throws NPE if null
485       */
486      public void testGetWaitingThreadsNPE() {
487 <        final PublicReentrantLock lock = new PublicReentrantLock();
487 >        final PublicReentrantLock lock = new PublicReentrantLock();
488          try {
489              lock.getWaitingThreads(null);
490              shouldThrow();
491 <        } catch (NullPointerException success) {
599 <        } catch (Exception ex) {
600 <            unexpectedException();
601 <        }
491 >        } catch (NullPointerException success) {}
492      }
493  
494  
# Line 606 | Line 496 | public class ReentrantLockTest extends J
496       * hasWaiters throws IAE if not owned
497       */
498      public void testHasWaitersIAE() {
499 <        final ReentrantLock lock = new ReentrantLock();
500 <        final Condition c = (lock.newCondition());
501 <        final ReentrantLock lock2 = new ReentrantLock();
499 >        final ReentrantLock lock = new ReentrantLock();
500 >        final Condition c = lock.newCondition();
501 >        final ReentrantLock lock2 = new ReentrantLock();
502          try {
503              lock2.hasWaiters(c);
504              shouldThrow();
505 <        } catch (IllegalArgumentException success) {
616 <        } catch (Exception ex) {
617 <            unexpectedException();
618 <        }
505 >        } catch (IllegalArgumentException success) {}
506      }
507  
508      /**
509       * hasWaiters throws IMSE if not locked
510       */
511      public void testHasWaitersIMSE() {
512 <        final ReentrantLock lock = new ReentrantLock();
513 <        final Condition c = (lock.newCondition());
512 >        final ReentrantLock lock = new ReentrantLock();
513 >        final Condition c = lock.newCondition();
514          try {
515              lock.hasWaiters(c);
516              shouldThrow();
517 <        } catch (IllegalMonitorStateException success) {
631 <        } catch (Exception ex) {
632 <            unexpectedException();
633 <        }
517 >        } catch (IllegalMonitorStateException success) {}
518      }
519  
520  
# Line 638 | Line 522 | public class ReentrantLockTest extends J
522       * getWaitQueueLength throws IAE if not owned
523       */
524      public void testGetWaitQueueLengthIAE() {
525 <        final ReentrantLock lock = new ReentrantLock();
526 <        final Condition c = (lock.newCondition());
527 <        final ReentrantLock lock2 = new ReentrantLock();
525 >        final ReentrantLock lock = new ReentrantLock();
526 >        final Condition c = lock.newCondition();
527 >        final ReentrantLock lock2 = new ReentrantLock();
528          try {
529              lock2.getWaitQueueLength(c);
530              shouldThrow();
531 <        } catch (IllegalArgumentException success) {
648 <        } catch (Exception ex) {
649 <            unexpectedException();
650 <        }
531 >        } catch (IllegalArgumentException success) {}
532      }
533  
534      /**
535       * getWaitQueueLength throws IMSE if not locked
536       */
537      public void testGetWaitQueueLengthIMSE() {
538 <        final ReentrantLock lock = new ReentrantLock();
539 <        final Condition c = (lock.newCondition());
538 >        final ReentrantLock lock = new ReentrantLock();
539 >        final Condition c = lock.newCondition();
540          try {
541              lock.getWaitQueueLength(c);
542              shouldThrow();
543 <        } catch (IllegalMonitorStateException success) {
663 <        } catch (Exception ex) {
664 <            unexpectedException();
665 <        }
543 >        } catch (IllegalMonitorStateException success) {}
544      }
545  
546  
# Line 670 | Line 548 | public class ReentrantLockTest extends J
548       * getWaitingThreads throws IAE if not owned
549       */
550      public void testGetWaitingThreadsIAE() {
551 <        final PublicReentrantLock lock = new PublicReentrantLock();
552 <        final Condition c = (lock.newCondition());
553 <        final PublicReentrantLock lock2 = new PublicReentrantLock();
551 >        final PublicReentrantLock lock = new PublicReentrantLock();
552 >        final Condition c = lock.newCondition();
553 >        final PublicReentrantLock lock2 = new PublicReentrantLock();
554          try {
555              lock2.getWaitingThreads(c);
556              shouldThrow();
557 <        } catch (IllegalArgumentException success) {
680 <        } catch (Exception ex) {
681 <            unexpectedException();
682 <        }
557 >        } catch (IllegalArgumentException success) {}
558      }
559  
560      /**
561       * getWaitingThreads throws IMSE if not locked
562       */
563      public void testGetWaitingThreadsIMSE() {
564 <        final PublicReentrantLock lock = new PublicReentrantLock();
565 <        final Condition c = (lock.newCondition());
564 >        final PublicReentrantLock lock = new PublicReentrantLock();
565 >        final Condition c = lock.newCondition();
566          try {
567              lock.getWaitingThreads(c);
568              shouldThrow();
569 <        } catch (IllegalMonitorStateException success) {
695 <        } catch (Exception ex) {
696 <            unexpectedException();
697 <        }
569 >        } catch (IllegalMonitorStateException success) {}
570      }
571  
572  
701
573      /**
574       * hasWaiters returns true when a thread is waiting, else false
575       */
576 <    public void testHasWaiters() {
577 <        final ReentrantLock lock = new ReentrantLock();
576 >    public void testHasWaiters() throws InterruptedException {
577 >        final ReentrantLock lock = new ReentrantLock();
578          final Condition c = lock.newCondition();
579 <        Thread t = new Thread(new Runnable() {
580 <                public void run() {
581 <                    try {
582 <                        lock.lock();
583 <                        threadAssertFalse(lock.hasWaiters(c));
584 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
585 <                        c.await();
586 <                        lock.unlock();
716 <                    }
717 <                    catch (InterruptedException e) {
718 <                        threadUnexpectedException();
719 <                    }
720 <                }
721 <            });
579 >        Thread t = new Thread(new CheckedRunnable() {
580 >            public void realRun() throws InterruptedException {
581 >                lock.lock();
582 >                assertFalse(lock.hasWaiters(c));
583 >                assertEquals(0, lock.getWaitQueueLength(c));
584 >                c.await();
585 >                lock.unlock();
586 >            }});
587  
588 <        try {
589 <            t.start();
590 <            Thread.sleep(SHORT_DELAY_MS);
591 <            lock.lock();
592 <            assertTrue(lock.hasWaiters(c));
593 <            assertEquals(1, lock.getWaitQueueLength(c));
594 <            c.signal();
595 <            lock.unlock();
596 <            Thread.sleep(SHORT_DELAY_MS);
597 <            lock.lock();
598 <            assertFalse(lock.hasWaiters(c));
599 <            assertEquals(0, lock.getWaitQueueLength(c));
600 <            lock.unlock();
601 <            t.join(SHORT_DELAY_MS);
737 <            assertFalse(t.isAlive());
738 <        }
739 <        catch (Exception ex) {
740 <            unexpectedException();
741 <        }
588 >        t.start();
589 >        delay(SHORT_DELAY_MS);
590 >        lock.lock();
591 >        assertTrue(lock.hasWaiters(c));
592 >        assertEquals(1, lock.getWaitQueueLength(c));
593 >        c.signal();
594 >        lock.unlock();
595 >        delay(SHORT_DELAY_MS);
596 >        lock.lock();
597 >        assertFalse(lock.hasWaiters(c));
598 >        assertEquals(0, lock.getWaitQueueLength(c));
599 >        lock.unlock();
600 >        t.join(SHORT_DELAY_MS);
601 >        assertFalse(t.isAlive());
602      }
603  
604      /**
605       * getWaitQueueLength returns number of waiting threads
606       */
607 <    public void testGetWaitQueueLength() {
608 <        final ReentrantLock lock = new ReentrantLock();
607 >    public void testGetWaitQueueLength() throws InterruptedException {
608 >        final ReentrantLock lock = new ReentrantLock();
609          final Condition c = lock.newCondition();
610 <        Thread t1 = new Thread(new Runnable() {
611 <                public void run() {
612 <                    try {
613 <                        lock.lock();
614 <                        threadAssertFalse(lock.hasWaiters(c));
615 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
616 <                        c.await();
617 <                        lock.unlock();
758 <                    }
759 <                    catch (InterruptedException e) {
760 <                        threadUnexpectedException();
761 <                    }
762 <                }
763 <            });
764 <
765 <        Thread t2 = new Thread(new Runnable() {
766 <                public void run() {
767 <                    try {
768 <                        lock.lock();
769 <                        threadAssertTrue(lock.hasWaiters(c));
770 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
771 <                        c.await();
772 <                        lock.unlock();
773 <                    }
774 <                    catch (InterruptedException e) {
775 <                        threadUnexpectedException();
776 <                    }
777 <                }
778 <            });
610 >        Thread t1 = new Thread(new CheckedRunnable() {
611 >            public void realRun() throws InterruptedException {
612 >                lock.lock();
613 >                assertFalse(lock.hasWaiters(c));
614 >                assertEquals(0, lock.getWaitQueueLength(c));
615 >                c.await();
616 >                lock.unlock();
617 >            }});
618  
619 <        try {
620 <            t1.start();
621 <            Thread.sleep(SHORT_DELAY_MS);
622 <            t2.start();
623 <            Thread.sleep(SHORT_DELAY_MS);
624 <            lock.lock();
625 <            assertTrue(lock.hasWaiters(c));
626 <            assertEquals(2, lock.getWaitQueueLength(c));
627 <            c.signalAll();
628 <            lock.unlock();
629 <            Thread.sleep(SHORT_DELAY_MS);
630 <            lock.lock();
631 <            assertFalse(lock.hasWaiters(c));
632 <            assertEquals(0, lock.getWaitQueueLength(c));
633 <            lock.unlock();
634 <            t1.join(SHORT_DELAY_MS);
635 <            t2.join(SHORT_DELAY_MS);
636 <            assertFalse(t1.isAlive());
637 <            assertFalse(t2.isAlive());
638 <        }
639 <        catch (Exception ex) {
640 <            unexpectedException();
641 <        }
619 >        Thread t2 = new Thread(new CheckedRunnable() {
620 >            public void realRun() throws InterruptedException {
621 >                lock.lock();
622 >                assertTrue(lock.hasWaiters(c));
623 >                assertEquals(1, lock.getWaitQueueLength(c));
624 >                c.await();
625 >                lock.unlock();
626 >            }});
627 >
628 >        t1.start();
629 >        delay(SHORT_DELAY_MS);
630 >        t2.start();
631 >        delay(SHORT_DELAY_MS);
632 >        lock.lock();
633 >        assertTrue(lock.hasWaiters(c));
634 >        assertEquals(2, lock.getWaitQueueLength(c));
635 >        c.signalAll();
636 >        lock.unlock();
637 >        delay(SHORT_DELAY_MS);
638 >        lock.lock();
639 >        assertFalse(lock.hasWaiters(c));
640 >        assertEquals(0, lock.getWaitQueueLength(c));
641 >        lock.unlock();
642 >        t1.join(SHORT_DELAY_MS);
643 >        t2.join(SHORT_DELAY_MS);
644 >        assertFalse(t1.isAlive());
645 >        assertFalse(t2.isAlive());
646      }
647  
648      /**
649       * getWaitingThreads returns only and all waiting threads
650       */
651 <    public void testGetWaitingThreads() {
652 <        final PublicReentrantLock lock = new PublicReentrantLock();
651 >    public void testGetWaitingThreads() throws InterruptedException {
652 >        final PublicReentrantLock lock = new PublicReentrantLock();
653          final Condition c = lock.newCondition();
654 <        Thread t1 = new Thread(new Runnable() {
655 <                public void run() {
656 <                    try {
657 <                        lock.lock();
658 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
659 <                        c.await();
660 <                        lock.unlock();
818 <                    }
819 <                    catch (InterruptedException e) {
820 <                        threadUnexpectedException();
821 <                    }
822 <                }
823 <            });
824 <
825 <        Thread t2 = new Thread(new Runnable() {
826 <                public void run() {
827 <                    try {
828 <                        lock.lock();
829 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
830 <                        c.await();
831 <                        lock.unlock();
832 <                    }
833 <                    catch (InterruptedException e) {
834 <                        threadUnexpectedException();
835 <                    }
836 <                }
837 <            });
654 >        Thread t1 = new Thread(new CheckedRunnable() {
655 >            public void realRun() throws InterruptedException {
656 >                lock.lock();
657 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
658 >                c.await();
659 >                lock.unlock();
660 >            }});
661  
662 <        try {
663 <            lock.lock();
664 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
665 <            lock.unlock();
666 <            t1.start();
667 <            Thread.sleep(SHORT_DELAY_MS);
668 <            t2.start();
669 <            Thread.sleep(SHORT_DELAY_MS);
670 <            lock.lock();
671 <            assertTrue(lock.hasWaiters(c));
672 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
673 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
674 <            c.signalAll();
675 <            lock.unlock();
676 <            Thread.sleep(SHORT_DELAY_MS);
677 <            lock.lock();
678 <            assertFalse(lock.hasWaiters(c));
679 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
680 <            lock.unlock();
681 <            t1.join(SHORT_DELAY_MS);
682 <            t2.join(SHORT_DELAY_MS);
683 <            assertFalse(t1.isAlive());
684 <            assertFalse(t2.isAlive());
685 <        }
686 <        catch (Exception ex) {
687 <            unexpectedException();
688 <        }
662 >        Thread t2 = new Thread(new CheckedRunnable() {
663 >            public void realRun() throws InterruptedException {
664 >                lock.lock();
665 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
666 >                c.await();
667 >                lock.unlock();
668 >            }});
669 >
670 >        lock.lock();
671 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
672 >        lock.unlock();
673 >        t1.start();
674 >        delay(SHORT_DELAY_MS);
675 >        t2.start();
676 >        delay(SHORT_DELAY_MS);
677 >        lock.lock();
678 >        assertTrue(lock.hasWaiters(c));
679 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
680 >        assertTrue(lock.getWaitingThreads(c).contains(t2));
681 >        c.signalAll();
682 >        lock.unlock();
683 >        delay(SHORT_DELAY_MS);
684 >        lock.lock();
685 >        assertFalse(lock.hasWaiters(c));
686 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
687 >        lock.unlock();
688 >        t1.join(SHORT_DELAY_MS);
689 >        t2.join(SHORT_DELAY_MS);
690 >        assertFalse(t1.isAlive());
691 >        assertFalse(t2.isAlive());
692      }
693  
694      /** A helper class for uninterruptible wait tests */
695 <    class UninterruptableThread extends Thread {
695 >    class UninterruptibleThread extends Thread {
696          private ReentrantLock lock;
697          private Condition c;
698  
# Line 874 | Line 700 | public class ReentrantLockTest extends J
700          public volatile boolean interrupted = false;
701          public volatile boolean lockStarted = false;
702  
703 <        public UninterruptableThread(ReentrantLock lock, Condition c) {
703 >        public UninterruptibleThread(ReentrantLock lock, Condition c) {
704              this.lock = lock;
705              this.c = c;
706          }
# Line 895 | Line 721 | public class ReentrantLockTest extends J
721      /**
722       * awaitUninterruptibly doesn't abort on interrupt
723       */
724 <    public void testAwaitUninterruptibly() {
724 >    public void testAwaitUninterruptibly() throws InterruptedException {
725          final ReentrantLock lock = new ReentrantLock();
726          final Condition c = lock.newCondition();
727 <        UninterruptableThread thread = new UninterruptableThread(lock, c);
902 <
903 <        try {
904 <            thread.start();
727 >        UninterruptibleThread thread = new UninterruptibleThread(lock, c);
728  
729 <            while (!thread.lockStarted) {
907 <                Thread.sleep(100);
908 <            }
729 >        thread.start();
730  
731 <            lock.lock();
732 <            try {
733 <                thread.interrupt();
913 <                thread.canAwake = true;
914 <                c.signal();
915 <            } finally {
916 <                lock.unlock();
917 <            }
731 >        while (!thread.lockStarted) {
732 >            delay(100);
733 >        }
734  
735 <            thread.join();
736 <            assertTrue(thread.interrupted);
737 <            assertFalse(thread.isAlive());
738 <        } catch (Exception ex) {
739 <            unexpectedException();
735 >        lock.lock();
736 >        try {
737 >            thread.interrupt();
738 >            thread.canAwake = true;
739 >            c.signal();
740 >        } finally {
741 >            lock.unlock();
742          }
743 +
744 +        thread.join();
745 +        assertTrue(thread.interrupted);
746 +        assertFalse(thread.isAlive());
747      }
748  
749      /**
750       * await is interruptible
751       */
752 <    public void testAwait_Interrupt() {
753 <        final ReentrantLock lock = new ReentrantLock();
752 >    public void testAwait_Interrupt() throws InterruptedException {
753 >        final ReentrantLock lock = new ReentrantLock();
754          final Condition c = lock.newCondition();
755 <        Thread t = new Thread(new Runnable() {
756 <                public void run() {
757 <                    try {
758 <                        lock.lock();
759 <                        c.await();
760 <                        lock.unlock();
761 <                        threadShouldThrow();
762 <                    }
763 <                    catch (InterruptedException success) {
764 <                    }
765 <                }
944 <            });
945 <
946 <        try {
947 <            t.start();
948 <            Thread.sleep(SHORT_DELAY_MS);
949 <            t.interrupt();
950 <            t.join(SHORT_DELAY_MS);
951 <            assertFalse(t.isAlive());
952 <        }
953 <        catch (Exception ex) {
954 <            unexpectedException();
955 <        }
755 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
756 >            public void realRun() throws InterruptedException {
757 >                lock.lock();
758 >                c.await();
759 >            }});
760 >
761 >        t.start();
762 >        delay(SHORT_DELAY_MS);
763 >        t.interrupt();
764 >        t.join(SHORT_DELAY_MS);
765 >        assertFalse(t.isAlive());
766      }
767  
768      /**
769       * awaitNanos is interruptible
770       */
771 <    public void testAwaitNanos_Interrupt() {
772 <        final ReentrantLock lock = new ReentrantLock();
771 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
772 >        final ReentrantLock lock = new ReentrantLock();
773          final Condition c = lock.newCondition();
774 <        Thread t = new Thread(new Runnable() {
775 <                public void run() {
776 <                    try {
777 <                        lock.lock();
778 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
779 <                        lock.unlock();
780 <                        threadShouldThrow();
781 <                    }
782 <                    catch (InterruptedException success) {
783 <                    }
784 <                }
975 <            });
976 <
977 <        try {
978 <            t.start();
979 <            Thread.sleep(SHORT_DELAY_MS);
980 <            t.interrupt();
981 <            t.join(SHORT_DELAY_MS);
982 <            assertFalse(t.isAlive());
983 <        }
984 <        catch (Exception ex) {
985 <            unexpectedException();
986 <        }
774 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
775 >            public void realRun() throws InterruptedException {
776 >                lock.lock();
777 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
778 >            }});
779 >
780 >        t.start();
781 >        delay(SHORT_DELAY_MS);
782 >        t.interrupt();
783 >        t.join(SHORT_DELAY_MS);
784 >        assertFalse(t.isAlive());
785      }
786  
787      /**
788       * awaitUntil is interruptible
789       */
790 <    public void testAwaitUntil_Interrupt() {
791 <        final ReentrantLock lock = new ReentrantLock();
790 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
791 >        final ReentrantLock lock = new ReentrantLock();
792          final Condition c = lock.newCondition();
793 <        Thread t = new Thread(new Runnable() {
794 <                public void run() {
795 <                    try {
796 <                        lock.lock();
797 <                        java.util.Date d = new java.util.Date();
798 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
799 <                        lock.unlock();
800 <                        threadShouldThrow();
801 <                    }
802 <                    catch (InterruptedException success) {
803 <                    }
804 <                }
1007 <            });
1008 <
1009 <        try {
1010 <            t.start();
1011 <            Thread.sleep(SHORT_DELAY_MS);
1012 <            t.interrupt();
1013 <            t.join(SHORT_DELAY_MS);
1014 <            assertFalse(t.isAlive());
1015 <        }
1016 <        catch (Exception ex) {
1017 <            unexpectedException();
1018 <        }
793 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
794 >            public void realRun() throws InterruptedException {
795 >                lock.lock();
796 >                java.util.Date d = new java.util.Date();
797 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
798 >            }});
799 >
800 >        t.start();
801 >        delay(SHORT_DELAY_MS);
802 >        t.interrupt();
803 >        t.join(SHORT_DELAY_MS);
804 >        assertFalse(t.isAlive());
805      }
806  
807      /**
808       * signalAll wakes up all threads
809       */
810 <    public void testSignalAll() {
811 <        final ReentrantLock lock = new ReentrantLock();
810 >    public void testSignalAll() throws InterruptedException {
811 >        final ReentrantLock lock = new ReentrantLock();
812          final Condition c = lock.newCondition();
813 <        Thread t1 = new Thread(new Runnable() {
814 <                public void run() {
815 <                    try {
816 <                        lock.lock();
817 <                        c.await();
818 <                        lock.unlock();
1033 <                    }
1034 <                    catch (InterruptedException e) {
1035 <                        threadUnexpectedException();
1036 <                    }
1037 <                }
1038 <            });
1039 <
1040 <        Thread t2 = new Thread(new Runnable() {
1041 <                public void run() {
1042 <                    try {
1043 <                        lock.lock();
1044 <                        c.await();
1045 <                        lock.unlock();
1046 <                    }
1047 <                    catch (InterruptedException e) {
1048 <                        threadUnexpectedException();
1049 <                    }
1050 <                }
1051 <            });
813 >        Thread t1 = new Thread(new CheckedRunnable() {
814 >            public void realRun() throws InterruptedException {
815 >                lock.lock();
816 >                c.await();
817 >                lock.unlock();
818 >            }});
819  
820 <        try {
821 <            t1.start();
822 <            t2.start();
823 <            Thread.sleep(SHORT_DELAY_MS);
824 <            lock.lock();
825 <            c.signalAll();
826 <            lock.unlock();
827 <            t1.join(SHORT_DELAY_MS);
828 <            t2.join(SHORT_DELAY_MS);
829 <            assertFalse(t1.isAlive());
830 <            assertFalse(t2.isAlive());
831 <        }
832 <        catch (Exception ex) {
833 <            unexpectedException();
834 <        }
820 >        Thread t2 = new Thread(new CheckedRunnable() {
821 >            public void realRun() throws InterruptedException {
822 >                lock.lock();
823 >                c.await();
824 >                lock.unlock();
825 >            }});
826 >
827 >        t1.start();
828 >        t2.start();
829 >        delay(SHORT_DELAY_MS);
830 >        lock.lock();
831 >        c.signalAll();
832 >        lock.unlock();
833 >        t1.join(SHORT_DELAY_MS);
834 >        t2.join(SHORT_DELAY_MS);
835 >        assertFalse(t1.isAlive());
836 >        assertFalse(t2.isAlive());
837      }
838  
839      /**
840       * await after multiple reentrant locking preserves lock count
841       */
842 <    public void testAwaitLockCount() {
843 <        final ReentrantLock lock = new ReentrantLock();
842 >    public void testAwaitLockCount() throws InterruptedException {
843 >        final ReentrantLock lock = new ReentrantLock();
844          final Condition c = lock.newCondition();
845 <        Thread t1 = new Thread(new Runnable() {
846 <                public void run() {
847 <                    try {
848 <                        lock.lock();
849 <                        threadAssertEquals(1, lock.getHoldCount());
850 <                        c.await();
851 <                        threadAssertEquals(1, lock.getHoldCount());
852 <                        lock.unlock();
1084 <                    }
1085 <                    catch (InterruptedException e) {
1086 <                        threadUnexpectedException();
1087 <                    }
1088 <                }
1089 <            });
1090 <
1091 <        Thread t2 = new Thread(new Runnable() {
1092 <                public void run() {
1093 <                    try {
1094 <                        lock.lock();
1095 <                        lock.lock();
1096 <                        threadAssertEquals(2, lock.getHoldCount());
1097 <                        c.await();
1098 <                        threadAssertEquals(2, lock.getHoldCount());
1099 <                        lock.unlock();
1100 <                        lock.unlock();
1101 <                    }
1102 <                    catch (InterruptedException e) {
1103 <                        threadUnexpectedException();
1104 <                    }
1105 <                }
1106 <            });
845 >        Thread t1 = new Thread(new CheckedRunnable() {
846 >            public void realRun() throws InterruptedException {
847 >                lock.lock();
848 >                assertEquals(1, lock.getHoldCount());
849 >                c.await();
850 >                assertEquals(1, lock.getHoldCount());
851 >                lock.unlock();
852 >            }});
853  
854 <        try {
855 <            t1.start();
856 <            t2.start();
857 <            Thread.sleep(SHORT_DELAY_MS);
858 <            lock.lock();
859 <            c.signalAll();
860 <            lock.unlock();
861 <            t1.join(SHORT_DELAY_MS);
862 <            t2.join(SHORT_DELAY_MS);
863 <            assertFalse(t1.isAlive());
864 <            assertFalse(t2.isAlive());
865 <        }
866 <        catch (Exception ex) {
867 <            unexpectedException();
868 <        }
854 >        Thread t2 = new Thread(new CheckedRunnable() {
855 >            public void realRun() throws InterruptedException {
856 >                lock.lock();
857 >                lock.lock();
858 >                assertEquals(2, lock.getHoldCount());
859 >                c.await();
860 >                assertEquals(2, lock.getHoldCount());
861 >                lock.unlock();
862 >                lock.unlock();
863 >            }});
864 >
865 >        t1.start();
866 >        t2.start();
867 >        delay(SHORT_DELAY_MS);
868 >        lock.lock();
869 >        c.signalAll();
870 >        lock.unlock();
871 >        t1.join(SHORT_DELAY_MS);
872 >        t2.join(SHORT_DELAY_MS);
873 >        assertFalse(t1.isAlive());
874 >        assertFalse(t2.isAlive());
875      }
876  
877      /**
878       * A serialized lock deserializes as unlocked
879       */
880 <    public void testSerialization() {
880 >    public void testSerialization() throws Exception {
881          ReentrantLock l = new ReentrantLock();
882          l.lock();
883          l.unlock();
884  
885 <        try {
886 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
887 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
888 <            out.writeObject(l);
889 <            out.close();
890 <
891 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
892 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
893 <            ReentrantLock r = (ReentrantLock) in.readObject();
894 <            r.lock();
895 <            r.unlock();
896 <        } catch (Exception e){
897 <            e.printStackTrace();
1146 <            unexpectedException();
1147 <        }
885 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
886 >        ObjectOutputStream out =
887 >            new ObjectOutputStream(new BufferedOutputStream(bout));
888 >        out.writeObject(l);
889 >        out.close();
890 >
891 >        ByteArrayInputStream bin =
892 >            new ByteArrayInputStream(bout.toByteArray());
893 >        ObjectInputStream in =
894 >            new ObjectInputStream(new BufferedInputStream(bin));
895 >        ReentrantLock r = (ReentrantLock) in.readObject();
896 >        r.lock();
897 >        r.unlock();
898      }
899  
900      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines