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.19 by dl, Sun Jan 25 13:25:28 2004 UTC vs.
Revision 1.33 by jsr166, Tue Dec 1 06:03:49 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines