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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines