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.14 by dl, Sun Dec 28 22:44:59 2003 UTC vs.
Revision 1.31 by jsr166, Sat Nov 21 02:33:20 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
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 <     * getQueuedThreads includes waiting threads
166 >     * getQueueLength reports number of waiting threads
167       */
168 <    public void testGetQueuedThreads() {
169 <        final PublicReentrantLock lock = new PublicReentrantLock();
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 <            assertTrue(lock.getQueuedThreads().isEmpty());
174 <            lock.lock();
175 <            assertTrue(lock.getQueuedThreads().isEmpty());
176 <            t1.start();
177 <            Thread.sleep(SHORT_DELAY_MS);
178 <            assertTrue(lock.getQueuedThreads().contains(t1));
179 <            t2.start();
180 <            Thread.sleep(SHORT_DELAY_MS);
181 <            assertTrue(lock.getQueuedThreads().contains(t1));
182 <            assertTrue(lock.getQueuedThreads().contains(t2));
183 <            t1.interrupt();
184 <            Thread.sleep(SHORT_DELAY_MS);
185 <            assertFalse(lock.getQueuedThreads().contains(t1));
186 <            assertTrue(lock.getQueuedThreads().contains(t2));
187 <            lock.unlock();
188 <            Thread.sleep(SHORT_DELAY_MS);
202 <            assertTrue(lock.getQueuedThreads().isEmpty());
203 <            t1.join();
204 <            t2.join();
205 <        } catch(Exception e){
206 <            unexpectedException();
207 <        }
208 <    }
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();
195 +        try {
196 +            sync.hasQueuedThread(null);
197 +            shouldThrow();
198 +        } catch (NullPointerException success) {}
199 +    }
200  
201      /**
202 <     * timed trylock is interruptible.
203 <     */
204 <    public void testInterruptedException2() {
205 <        final ReentrantLock lock = new ReentrantLock();
206 <        lock.lock();
207 <        Thread t = new Thread(new Runnable() {
208 <                public void run() {
209 <                    try {
210 <                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
211 <                        threadShouldThrow();
212 <                    } catch(InterruptedException success){}
213 <                }
214 <            });
215 <        try {
216 <            t.start();
217 <            t.interrupt();
218 <        } catch(Exception e){
219 <            unexpectedException();
220 <        }
202 >     * hasQueuedThread reports whether a thread is queued.
203 >     */
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 >        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 <     * Trylock on a locked lock fails
233 >     * getQueuedThreads includes waiting threads
234       */
235 <    public void testTryLockWhenLocked() {
236 <        final ReentrantLock lock = new ReentrantLock();
237 <        lock.lock();
238 <        Thread t = new Thread(new Runnable() {
239 <                public void run() {
240 <                    threadAssertFalse(lock.tryLock());
241 <                }
242 <            });
243 <        try {
244 <            t.start();
245 <            t.join();
246 <            lock.unlock();
247 <        } catch(Exception e){
248 <            unexpectedException();
249 <        }
250 <    }
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 >        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() 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 >        t.interrupt();
274 >        t.join();
275 >    }
276 >
277 >
278 >    /**
279 >     * TryLock on a locked lock fails
280 >     */
281 >    public void testTryLockWhenLocked() throws InterruptedException {
282 >        final ReentrantLock lock = new ReentrantLock();
283 >        lock.lock();
284 >        Thread t = new Thread(new CheckedRunnable() {
285 >            public void realRun() {
286 >                threadAssertFalse(lock.tryLock());
287 >            }});
288 >
289 >        t.start();
290 >        t.join();
291 >        lock.unlock();
292 >    }
293 >
294 >    /**
295 >     * Timed tryLock on a locked lock times out
296 >     */
297 >    public void testTryLock_Timeout() throws InterruptedException {
298 >        final ReentrantLock lock = new ReentrantLock();
299 >        lock.lock();
300 >        Thread t = new Thread(new CheckedRunnable() {
301 >            public void realRun() throws InterruptedException {
302 >                threadAssertFalse(lock.tryLock(1, MILLISECONDS));
303 >            }});
304 >
305 >        t.start();
306 >        t.join();
307 >        lock.unlock();
308 >    }
309  
310      /**
255     * Timed trylock on a locked lock times out
256     */
257    public void testTryLock_Timeout() {
258        final ReentrantLock lock = new ReentrantLock();
259        lock.lock();
260        Thread t = new Thread(new Runnable() {
261                public void run() {
262                    try {
263                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
264                    } catch (Exception ex) {
265                        threadUnexpectedException();
266                    }
267                }
268            });
269        try {
270            t.start();
271            t.join();
272            lock.unlock();
273        } catch(Exception e){
274            unexpectedException();
275        }
276    }
277    
278    /**
311       * getHoldCount returns number of recursive holds
312       */
313      public void testGetHoldCount() {
314 <        ReentrantLock lock = new ReentrantLock();
315 <        for(int i = 1; i <= SIZE; i++) {
316 <            lock.lock();
317 <            assertEquals(i,lock.getHoldCount());
318 <        }
319 <        for(int i = SIZE; i > 0; i--) {
320 <            lock.unlock();
321 <            assertEquals(i-1,lock.getHoldCount());
322 <        }
314 >        ReentrantLock lock = new ReentrantLock();
315 >        for (int i = 1; i <= SIZE; i++) {
316 >            lock.lock();
317 >            assertEquals(i, lock.getHoldCount());
318 >        }
319 >        for (int i = SIZE; i > 0; i--) {
320 >            lock.unlock();
321 >            assertEquals(i-1, lock.getHoldCount());
322 >        }
323      }
324 <    
325 <  
324 >
325 >
326      /**
327       * isLocked is true when locked and false when not
328       */
329 <    public void testIsLocked() {
330 <        final ReentrantLock lock = new ReentrantLock();
331 <        lock.lock();
332 <        assertTrue(lock.isLocked());
333 <        lock.unlock();
334 <        assertFalse(lock.isLocked());
335 <        Thread t = new Thread(new Runnable() {
336 <                public void run() {
337 <                    lock.lock();
338 <                    try {
339 <                        Thread.sleep(SMALL_DELAY_MS);
340 <                    }
341 <                    catch(Exception e) {
342 <                        threadUnexpectedException();
343 <                    }
344 <                    lock.unlock();
345 <                }
346 <            });
315 <        try {
316 <            t.start();
317 <            Thread.sleep(SHORT_DELAY_MS);
318 <            assertTrue(lock.isLocked());
319 <            t.join();
320 <            assertFalse(lock.isLocked());
321 <        } catch(Exception e){
322 <            unexpectedException();
323 <        }
329 >    public void testIsLocked() throws InterruptedException {
330 >        final ReentrantLock lock = new ReentrantLock();
331 >        lock.lock();
332 >        assertTrue(lock.isLocked());
333 >        lock.unlock();
334 >        assertFalse(lock.isLocked());
335 >        Thread t = new Thread(new CheckedRunnable() {
336 >            public void realRun() throws InterruptedException {
337 >                lock.lock();
338 >                Thread.sleep(SMALL_DELAY_MS);
339 >                lock.unlock();
340 >            }});
341 >
342 >        t.start();
343 >        Thread.sleep(SHORT_DELAY_MS);
344 >        assertTrue(lock.isLocked());
345 >        t.join();
346 >        assertFalse(lock.isLocked());
347      }
348  
349  
350      /**
351       * lockInterruptibly is interruptible.
352       */
353 <    public void testLockInterruptibly1() {
354 <        final ReentrantLock lock = new ReentrantLock();
355 <        lock.lock();
356 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
357 <        try {
358 <            t.start();
359 <            t.interrupt();
360 <            lock.unlock();
361 <            t.join();
362 <        } catch(Exception e){
363 <            unexpectedException();
341 <        }
342 <    }
353 >    public void testLockInterruptibly1() throws InterruptedException {
354 >        final ReentrantLock lock = new ReentrantLock();
355 >        lock.lock();
356 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
357 >        t.start();
358 >        Thread.sleep(SHORT_DELAY_MS);
359 >        t.interrupt();
360 >        Thread.sleep(SHORT_DELAY_MS);
361 >        lock.unlock();
362 >        t.join();
363 >    }
364  
365      /**
366       * lockInterruptibly succeeds when unlocked, else is interruptible
367       */
368 <    public void testLockInterruptibly2() {
369 <        final ReentrantLock lock = new ReentrantLock();
370 <        try {
371 <            lock.lockInterruptibly();
372 <        } catch(Exception e) {
373 <            unexpectedException();
374 <        }
375 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
376 <        try {
356 <            t.start();
357 <            t.interrupt();
358 <            assertTrue(lock.isLocked());
359 <            assertTrue(lock.isHeldByCurrentThread());
360 <            t.join();
361 <        } catch(Exception e){
362 <            unexpectedException();
363 <        }
368 >    public void testLockInterruptibly2() throws InterruptedException {
369 >        final ReentrantLock lock = new ReentrantLock();
370 >        lock.lockInterruptibly();
371 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
372 >        t.start();
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 <        }
376 <        catch (IllegalMonitorStateException success) {
377 <        }
378 <        catch (Exception ex) {
379 <            unexpectedException();
380 <        }
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 <        }
393 <        catch (IllegalMonitorStateException success) {
394 <        }
395 <        catch (Exception ex) {
396 <            unexpectedException();
397 <        }
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);
410 <            lock.unlock();
411 <        }
412 <        catch (Exception ex) {
413 <            unexpectedException();
414 <        }
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
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 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
426 <            lock.unlock();
427 <        }
428 <        catch (Exception ex) {
429 <            unexpectedException();
430 <        }
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 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
443 <            lock.unlock();
444 <        }
445 <        catch (Exception ex) {
446 <            unexpectedException();
447 <        }
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();
450 <                    }
451 <                    catch(InterruptedException e) {
452 <                        threadUnexpectedException();
453 <                    }
454 <                }
455 <            });
456 <
457 <        try {
470 <            t.start();
471 <            Thread.sleep(SHORT_DELAY_MS);
472 <            lock.lock();
473 <            c.signal();
474 <            lock.unlock();
475 <            t.join(SHORT_DELAY_MS);
476 <            assertFalse(t.isAlive());
477 <        }
478 <        catch (Exception ex) {
479 <            unexpectedException();
480 <        }
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 >        t.start();
452 >        Thread.sleep(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) {
492 <        } catch (Exception ex) {
493 <            unexpectedException();
494 <        }
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) {
506 <        } catch (Exception ex) {
507 <            unexpectedException();
508 <        }
479 >        } catch (NullPointerException success) {}
480      }
481  
482  
# Line 513 | 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) {
521 <        } catch (Exception ex) {
522 <            unexpectedException();
523 <        }
491 >        } catch (NullPointerException success) {}
492      }
493  
494  
# Line 528 | 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) {
538 <        } catch (Exception ex) {
539 <            unexpectedException();
540 <        }
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) {
553 <        } catch (Exception ex) {
554 <            unexpectedException();
555 <        }
517 >        } catch (IllegalMonitorStateException success) {}
518      }
519  
520  
# Line 560 | 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) {
570 <        } catch (Exception ex) {
571 <            unexpectedException();
572 <        }
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) {
585 <        } catch (Exception ex) {
586 <            unexpectedException();
587 <        }
543 >        } catch (IllegalMonitorStateException success) {}
544      }
545  
546  
# Line 592 | 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) {
602 <        } catch (Exception ex) {
603 <            unexpectedException();
604 <        }
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) {
617 <        } catch (Exception ex) {
618 <            unexpectedException();
619 <        }
569 >        } catch (IllegalMonitorStateException success) {}
570      }
571  
572  
623
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();
587 <                    }
588 <                    catch(InterruptedException e) {
589 <                        threadUnexpectedException();
590 <                    }
591 <                }
592 <            });
593 <
594 <        try {
595 <            t.start();
596 <            Thread.sleep(SHORT_DELAY_MS);
597 <            lock.lock();
598 <            assertTrue(lock.hasWaiters(c));
599 <            assertEquals(1, lock.getWaitQueueLength(c));
600 <            c.signal();
601 <            lock.unlock();
653 <            Thread.sleep(SHORT_DELAY_MS);
654 <            lock.lock();
655 <            assertFalse(lock.hasWaiters(c));
656 <            assertEquals(0, lock.getWaitQueueLength(c));
657 <            lock.unlock();
658 <            t.join(SHORT_DELAY_MS);
659 <            assertFalse(t.isAlive());
660 <        }
661 <        catch (Exception ex) {
662 <            unexpectedException();
663 <        }
579 >        Thread t = new Thread(new CheckedRunnable() {
580 >            public void realRun() throws InterruptedException {
581 >                lock.lock();
582 >                threadAssertFalse(lock.hasWaiters(c));
583 >                threadAssertEquals(0, lock.getWaitQueueLength(c));
584 >                c.await();
585 >                lock.unlock();
586 >            }});
587 >
588 >        t.start();
589 >        Thread.sleep(SHORT_DELAY_MS);
590 >        lock.lock();
591 >        assertTrue(lock.hasWaiters(c));
592 >        assertEquals(1, lock.getWaitQueueLength(c));
593 >        c.signal();
594 >        lock.unlock();
595 >        Thread.sleep(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();
618 <                    }
619 <                    catch(InterruptedException e) {
620 <                        threadUnexpectedException();
621 <                    }
622 <                }
623 <            });
624 <
625 <        Thread t2 = new Thread(new Runnable() {
626 <                public void run() {
627 <                    try {
628 <                        lock.lock();
629 <                        threadAssertTrue(lock.hasWaiters(c));
630 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
631 <                        c.await();
632 <                        lock.unlock();
633 <                    }
634 <                    catch(InterruptedException e) {
635 <                        threadUnexpectedException();
636 <                    }
637 <                }
638 <            });
639 <
640 <        try {
641 <            t1.start();
642 <            Thread.sleep(SHORT_DELAY_MS);
643 <            t2.start();
644 <            Thread.sleep(SHORT_DELAY_MS);
645 <            lock.lock();
708 <            assertTrue(lock.hasWaiters(c));
709 <            assertEquals(2, lock.getWaitQueueLength(c));
710 <            c.signalAll();
711 <            lock.unlock();
712 <            Thread.sleep(SHORT_DELAY_MS);
713 <            lock.lock();
714 <            assertFalse(lock.hasWaiters(c));
715 <            assertEquals(0, lock.getWaitQueueLength(c));
716 <            lock.unlock();
717 <            t1.join(SHORT_DELAY_MS);
718 <            t2.join(SHORT_DELAY_MS);
719 <            assertFalse(t1.isAlive());
720 <            assertFalse(t2.isAlive());
721 <        }
722 <        catch (Exception ex) {
723 <            unexpectedException();
724 <        }
610 >        Thread t1 = new Thread(new CheckedRunnable() {
611 >            public void realRun() throws InterruptedException {
612 >                lock.lock();
613 >                threadAssertFalse(lock.hasWaiters(c));
614 >                threadAssertEquals(0, lock.getWaitQueueLength(c));
615 >                c.await();
616 >                lock.unlock();
617 >            }});
618 >
619 >        Thread t2 = new Thread(new CheckedRunnable() {
620 >            public void realRun() throws InterruptedException {
621 >                lock.lock();
622 >                threadAssertTrue(lock.hasWaiters(c));
623 >                threadAssertEquals(1, lock.getWaitQueueLength(c));
624 >                c.await();
625 >                lock.unlock();
626 >            }});
627 >
628 >        t1.start();
629 >        Thread.sleep(SHORT_DELAY_MS);
630 >        t2.start();
631 >        Thread.sleep(SHORT_DELAY_MS);
632 >        lock.lock();
633 >        assertTrue(lock.hasWaiters(c));
634 >        assertEquals(2, lock.getWaitQueueLength(c));
635 >        c.signalAll();
636 >        lock.unlock();
637 >        Thread.sleep(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();
661 <                    }
662 <                    catch(InterruptedException e) {
663 <                        threadUnexpectedException();
664 <                    }
665 <                }
666 <            });
667 <
668 <        Thread t2 = new Thread(new Runnable() {
669 <                public void run() {
670 <                    try {
671 <                        lock.lock();
672 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
673 <                        c.await();
674 <                        lock.unlock();
675 <                    }
676 <                    catch(InterruptedException e) {
677 <                        threadUnexpectedException();
678 <                    }
679 <                }
680 <            });
654 >        Thread t1 = new Thread(new CheckedRunnable() {
655 >            public void realRun() throws InterruptedException {
656 >                lock.lock();
657 >                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
658 >                c.await();
659 >                lock.unlock();
660 >            }});
661 >
662 >        Thread t2 = new Thread(new CheckedRunnable() {
663 >            public void realRun() throws InterruptedException {
664 >                lock.lock();
665 >                threadAssertFalse(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 >        Thread.sleep(SHORT_DELAY_MS);
675 >        t2.start();
676 >        Thread.sleep(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 >        Thread.sleep(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 UninterruptibleThread extends Thread {
696 >        private ReentrantLock lock;
697 >        private Condition c;
698 >
699 >        public volatile boolean canAwake = false;
700 >        public volatile boolean interrupted = false;
701 >        public volatile boolean lockStarted = false;
702 >
703 >        public UninterruptibleThread(ReentrantLock lock, Condition c) {
704 >            this.lock = lock;
705 >            this.c = c;
706 >        }
707 >
708 >        public synchronized void run() {
709 >            lock.lock();
710 >            lockStarted = true;
711 >
712 >            while (!canAwake) {
713 >                c.awaitUninterruptibly();
714 >            }
715  
716 <        try {
762 <            lock.lock();
763 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
764 <            lock.unlock();
765 <            t1.start();
766 <            Thread.sleep(SHORT_DELAY_MS);
767 <            t2.start();
768 <            Thread.sleep(SHORT_DELAY_MS);
769 <            lock.lock();
770 <            assertTrue(lock.hasWaiters(c));
771 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
772 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
773 <            c.signalAll();
716 >            interrupted = isInterrupted();
717              lock.unlock();
775            Thread.sleep(SHORT_DELAY_MS);
776            lock.lock();
777            assertFalse(lock.hasWaiters(c));
778            assertTrue(lock.getWaitingThreads(c).isEmpty());
779            lock.unlock();
780            t1.join(SHORT_DELAY_MS);
781            t2.join(SHORT_DELAY_MS);
782            assertFalse(t1.isAlive());
783            assertFalse(t2.isAlive());
784        }
785        catch (Exception ex) {
786            unexpectedException();
718          }
719      }
720  
790
791
721      /**
722       * awaitUninterruptibly doesn't abort on interrupt
723       */
724 <    public void testAwaitUninterruptibly() {
725 <        final ReentrantLock lock = new ReentrantLock();
724 >    public void testAwaitUninterruptibly() throws InterruptedException {
725 >        final ReentrantLock lock = new ReentrantLock();
726          final Condition c = lock.newCondition();
727 <        Thread t = new Thread(new Runnable() {
799 <                public void run() {
800 <                    lock.lock();
801 <                    c.awaitUninterruptibly();
802 <                    lock.unlock();
803 <                }
804 <            });
727 >        UninterruptibleThread thread = new UninterruptibleThread(lock, c);
728  
729 +        thread.start();
730 +
731 +        while (!thread.lockStarted) {
732 +            Thread.sleep(100);
733 +        }
734 +
735 +        lock.lock();
736          try {
737 <            t.start();
738 <            Thread.sleep(SHORT_DELAY_MS);
809 <            t.interrupt();
810 <            lock.lock();
737 >            thread.interrupt();
738 >            thread.canAwake = true;
739              c.signal();
740 +        } finally {
741              lock.unlock();
813            assert(t.isInterrupted());
814            t.join(SHORT_DELAY_MS);
815            assertFalse(t.isAlive());
816        }
817        catch (Exception ex) {
818            unexpectedException();
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 <                }
839 <            });
840 <
841 <        try {
842 <            t.start();
843 <            Thread.sleep(SHORT_DELAY_MS);
844 <            t.interrupt();
845 <            t.join(SHORT_DELAY_MS);
846 <            assertFalse(t.isAlive());
847 <        }
848 <        catch (Exception ex) {
849 <            unexpectedException();
850 <        }
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 >        Thread.sleep(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(SHORT_DELAY_MS * 2 * 1000000);
779 <                        lock.unlock();
780 <                        threadShouldThrow();
781 <                    }
782 <                    catch(InterruptedException success) {
783 <                    }
784 <                }
870 <            });
871 <
872 <        try {
873 <            t.start();
874 <            Thread.sleep(SHORT_DELAY_MS);
875 <            t.interrupt();
876 <            t.join(SHORT_DELAY_MS);
877 <            assertFalse(t.isAlive());
878 <        }
879 <        catch (Exception ex) {
880 <            unexpectedException();
881 <        }
774 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
775 >            public void realRun() throws InterruptedException {
776 >                lock.lock();
777 >                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
778 >            }});
779 >
780 >        t.start();
781 >        Thread.sleep(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 <                }
902 <            });
903 <
904 <        try {
905 <            t.start();
906 <            Thread.sleep(SHORT_DELAY_MS);
907 <            t.interrupt();
908 <            t.join(SHORT_DELAY_MS);
909 <            assertFalse(t.isAlive());
910 <        }
911 <        catch (Exception ex) {
912 <            unexpectedException();
913 <        }
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 >        Thread.sleep(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();
819 <                    }
820 <                    catch(InterruptedException e) {
821 <                        threadUnexpectedException();
822 <                    }
823 <                }
824 <            });
825 <
826 <        Thread t2 = new Thread(new Runnable() {
827 <                public void run() {
828 <                    try {
829 <                        lock.lock();
830 <                        c.await();
831 <                        lock.unlock();
832 <                    }
833 <                    catch(InterruptedException e) {
834 <                        threadUnexpectedException();
835 <                    }
836 <                }
837 <            });
838 <
839 <        try {
840 <            t1.start();
841 <            t2.start();
842 <            Thread.sleep(SHORT_DELAY_MS);
843 <            lock.lock();
844 <            c.signalAll();
845 <            lock.unlock();
846 <            t1.join(SHORT_DELAY_MS);
847 <            t2.join(SHORT_DELAY_MS);
848 <            assertFalse(t1.isAlive());
849 <            assertFalse(t2.isAlive());
850 <        }
851 <        catch (Exception ex) {
852 <            unexpectedException();
853 <        }
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 >        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 >        Thread.sleep(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() throws InterruptedException {
843 >        final ReentrantLock lock = new ReentrantLock();
844 >        final Condition c = lock.newCondition();
845 >        Thread t1 = new Thread(new CheckedRunnable() {
846 >            public void realRun() throws InterruptedException {
847 >                lock.lock();
848 >                threadAssertEquals(1, lock.getHoldCount());
849 >                c.await();
850 >                threadAssertEquals(1, lock.getHoldCount());
851 >                lock.unlock();
852 >            }});
853 >
854 >        Thread t2 = new Thread(new CheckedRunnable() {
855 >            public void realRun() throws InterruptedException {
856 >                lock.lock();
857 >                lock.lock();
858 >                threadAssertEquals(2, lock.getHoldCount());
859 >                c.await();
860 >                threadAssertEquals(2, lock.getHoldCount());
861 >                lock.unlock();
862 >                lock.unlock();
863 >            }});
864 >
865 >        t1.start();
866 >        t2.start();
867 >        Thread.sleep(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();
898 <            unexpectedException();
899 <        }
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 >    /**
901 >     * toString indicates current lock state
902 >     */
903 >    public void testToString() {
904 >        ReentrantLock lock = new ReentrantLock();
905 >        String us = lock.toString();
906 >        assertTrue(us.indexOf("Unlocked") >= 0);
907 >        lock.lock();
908 >        String ls = lock.toString();
909 >        assertTrue(ls.indexOf("Locked") >= 0);
910      }
911  
912   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines