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.15 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.42 by jsr166, Sat May 7 03:12:48 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines