ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.27 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.56 by jsr166, Mon Feb 2 04:50:55 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines