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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.16 by dl, Sun Jan 11 16:02:33 2004 UTC vs.
Revision 1.39 by jsr166, Tue Mar 15 19:47:06 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  
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.util.concurrent.locks.*;
15   import java.io.*;
16  
17   public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(AbstractQueuedSynchronizerTest.class);
# Line 31 | Line 32 | public class AbstractQueuedSynchronizerT
32       */
33      static class Mutex extends AbstractQueuedSynchronizer {
34          public boolean isHeldExclusively() { return getState() == 1; }
35 <        
35 >
36          public boolean tryAcquire(int acquires) {
37 <            assertTrue(acquires == 1);
37 >            assertEquals(1, acquires);
38              return compareAndSetState(0, 1);
39          }
40 <        
40 >
41          public boolean tryRelease(int releases) {
42              if (getState() == 0) throw new IllegalMonitorStateException();
43              setState(0);
44              return true;
45          }
46 <        
47 <        public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
48 <        
48 <        public void lock() {
49 <            acquire(1);
46 >
47 >        public AbstractQueuedSynchronizer.ConditionObject newCondition() {
48 >            return new AbstractQueuedSynchronizer.ConditionObject();
49          }
50  
51      }
52  
53 <    
53 >
54      /**
55       * A simple latch class, to test shared mode.
56       */
57 <    static class BooleanLatch extends AbstractQueuedSynchronizer {
57 >    static class BooleanLatch extends AbstractQueuedSynchronizer {
58          public boolean isSignalled() { return getState() != 0; }
59  
60          public int tryAcquireShared(int ignore) {
61 <            return isSignalled()? 1 : -1;
61 >            return isSignalled() ? 1 : -1;
62          }
63 <        
63 >
64          public boolean tryReleaseShared(int ignore) {
65              setState(1);
66              return true;
# Line 69 | Line 68 | public class AbstractQueuedSynchronizerT
68      }
69  
70      /**
71 <     * A runnable calling acquireInterruptibly
71 >     * A runnable calling acquireInterruptibly that does not expect to
72 >     * be interrupted.
73       */
74 <    class InterruptibleLockRunnable implements Runnable {
75 <        final Mutex lock;
76 <        InterruptibleLockRunnable(Mutex l) { lock = l; }
77 <        public void run() {
78 <            try {
79 <                lock.acquireInterruptibly(1);
80 <            } catch(InterruptedException success){}
74 >    class InterruptibleSyncRunnable extends CheckedRunnable {
75 >        final Mutex sync;
76 >        InterruptibleSyncRunnable(Mutex l) { sync = l; }
77 >        public void realRun() throws InterruptedException {
78 >            sync.acquireInterruptibly(1);
79          }
80      }
81  
82  
83      /**
84       * A runnable calling acquireInterruptibly that expects to be
85 <     * interrupted
85 >     * interrupted.
86       */
87 <    class InterruptedLockRunnable implements Runnable {
88 <        final Mutex lock;
89 <        InterruptedLockRunnable(Mutex l) { lock = l; }
90 <        public void run() {
91 <            try {
94 <                lock.acquireInterruptibly(1);
95 <                threadShouldThrow();
96 <            } catch(InterruptedException success){}
87 >    class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
88 >        final Mutex sync;
89 >        InterruptedSyncRunnable(Mutex l) { sync = l; }
90 >        public void realRun() throws InterruptedException {
91 >            sync.acquireInterruptibly(1);
92          }
93      }
94  
95      /**
96       * isHeldExclusively is false upon construction
97       */
98 <    public void testIsHeldExclusively() {
99 <        Mutex rl = new Mutex();
98 >    public void testIsHeldExclusively() {
99 >        Mutex rl = new Mutex();
100          assertFalse(rl.isHeldExclusively());
101      }
102 <    
102 >
103      /**
104 <     * acquiring released lock succeeds
104 >     * acquiring released sync succeeds
105       */
106 <    public void testAcquire() {
107 <        Mutex rl = new Mutex();
106 >    public void testAcquire() {
107 >        Mutex rl = new Mutex();
108          rl.acquire(1);
109          assertTrue(rl.isHeldExclusively());
110          rl.release(1);
111 +        assertFalse(rl.isHeldExclusively());
112      }
113  
114      /**
115 <     * tryAcquire on an released lock succeeds
115 >     * tryAcquire on an released sync succeeds
116       */
117 <    public void testTryAcquire() {
118 <        Mutex rl = new Mutex();
117 >    public void testTryAcquire() {
118 >        Mutex rl = new Mutex();
119          assertTrue(rl.tryAcquire(1));
120          assertTrue(rl.isHeldExclusively());
121          rl.release(1);
# Line 128 | Line 124 | public class AbstractQueuedSynchronizerT
124      /**
125       * hasQueuedThreads reports whether there are waiting threads
126       */
127 <    public void testhasQueuedThreads() {
128 <        final Mutex lock = new Mutex();
129 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
130 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
131 <        try {
132 <            assertFalse(lock.hasQueuedThreads());
133 <            lock.acquire(1);
134 <            t1.start();
135 <            Thread.sleep(SHORT_DELAY_MS);
136 <            assertTrue(lock.hasQueuedThreads());
137 <            t2.start();
138 <            Thread.sleep(SHORT_DELAY_MS);
139 <            assertTrue(lock.hasQueuedThreads());
140 <            t1.interrupt();
141 <            Thread.sleep(SHORT_DELAY_MS);
142 <            assertTrue(lock.hasQueuedThreads());
143 <            lock.release(1);
144 <            Thread.sleep(SHORT_DELAY_MS);
145 <            assertFalse(lock.hasQueuedThreads());
146 <            t1.join();
147 <            t2.join();
152 <        } catch(Exception e){
153 <            unexpectedException();
154 <        }
155 <    }
127 >    public void testhasQueuedThreads() throws InterruptedException {
128 >        final Mutex sync = new Mutex();
129 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
130 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
131 >        assertFalse(sync.hasQueuedThreads());
132 >        sync.acquire(1);
133 >        t1.start();
134 >        Thread.sleep(SHORT_DELAY_MS);
135 >        assertTrue(sync.hasQueuedThreads());
136 >        t2.start();
137 >        Thread.sleep(SHORT_DELAY_MS);
138 >        assertTrue(sync.hasQueuedThreads());
139 >        t1.interrupt();
140 >        Thread.sleep(SHORT_DELAY_MS);
141 >        assertTrue(sync.hasQueuedThreads());
142 >        sync.release(1);
143 >        Thread.sleep(SHORT_DELAY_MS);
144 >        assertFalse(sync.hasQueuedThreads());
145 >        t1.join();
146 >        t2.join();
147 >    }
148  
149      /**
150       * isQueued(null) throws NPE
151       */
152 <    public void testIsQueuedNPE() {
153 <        final Mutex lock = new Mutex();
152 >    public void testIsQueuedNPE() {
153 >        final Mutex sync = new Mutex();
154          try {
155 <            lock.isQueued(null);
155 >            sync.isQueued(null);
156              shouldThrow();
157 <        } catch (NullPointerException success) {
166 <        }
157 >        } catch (NullPointerException success) {}
158      }
159  
160      /**
161       * isQueued reports whether a thread is queued.
162       */
163 <    public void testIsQueued() {
164 <        final Mutex lock = new Mutex();
165 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
166 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
167 <        try {
168 <            assertFalse(lock.isQueued(t1));
169 <            assertFalse(lock.isQueued(t2));
170 <            lock.acquire(1);
171 <            t1.start();
172 <            Thread.sleep(SHORT_DELAY_MS);
173 <            assertTrue(lock.isQueued(t1));
174 <            t2.start();
175 <            Thread.sleep(SHORT_DELAY_MS);
176 <            assertTrue(lock.isQueued(t1));
177 <            assertTrue(lock.isQueued(t2));
178 <            t1.interrupt();
179 <            Thread.sleep(SHORT_DELAY_MS);
180 <            assertFalse(lock.isQueued(t1));
181 <            assertTrue(lock.isQueued(t2));
182 <            lock.release(1);
183 <            Thread.sleep(SHORT_DELAY_MS);
184 <            assertFalse(lock.isQueued(t1));
185 <            assertFalse(lock.isQueued(t2));
186 <            t1.join();
187 <            t2.join();
188 <        } catch(Exception e){
198 <            unexpectedException();
199 <        }
200 <    }
163 >    public void testIsQueued() throws InterruptedException {
164 >        final Mutex sync = new Mutex();
165 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
166 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
167 >        assertFalse(sync.isQueued(t1));
168 >        assertFalse(sync.isQueued(t2));
169 >        sync.acquire(1);
170 >        t1.start();
171 >        Thread.sleep(SHORT_DELAY_MS);
172 >        assertTrue(sync.isQueued(t1));
173 >        t2.start();
174 >        Thread.sleep(SHORT_DELAY_MS);
175 >        assertTrue(sync.isQueued(t1));
176 >        assertTrue(sync.isQueued(t2));
177 >        t1.interrupt();
178 >        Thread.sleep(SHORT_DELAY_MS);
179 >        assertFalse(sync.isQueued(t1));
180 >        assertTrue(sync.isQueued(t2));
181 >        sync.release(1);
182 >        Thread.sleep(SHORT_DELAY_MS);
183 >        assertFalse(sync.isQueued(t1));
184 >        Thread.sleep(SHORT_DELAY_MS);
185 >        assertFalse(sync.isQueued(t2));
186 >        t1.join();
187 >        t2.join();
188 >    }
189  
190      /**
191 <     * getFirstQueuedThread returns first waiting thread or null is none
192 <     */
193 <    public void testGetFirstQueuedThread() {
194 <        final Mutex lock = new Mutex();
195 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
196 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
197 <        try {
198 <            assertNull(lock.getFirstQueuedThread());
199 <            lock.acquire(1);
200 <            t1.start();
201 <            Thread.sleep(SHORT_DELAY_MS);
202 <            assertEquals(t1, lock.getFirstQueuedThread());
203 <            t2.start();
204 <            Thread.sleep(SHORT_DELAY_MS);
205 <            assertEquals(t1, lock.getFirstQueuedThread());
206 <            t1.interrupt();
207 <            Thread.sleep(SHORT_DELAY_MS);
208 <            assertEquals(t2, lock.getFirstQueuedThread());
209 <            lock.release(1);
210 <            Thread.sleep(SHORT_DELAY_MS);
211 <            assertNull(lock.getFirstQueuedThread());
212 <            t1.join();
213 <            t2.join();
214 <        } catch(Exception e){
227 <            unexpectedException();
228 <        }
229 <    }
191 >     * getFirstQueuedThread returns first waiting thread or null if none
192 >     */
193 >    public void testGetFirstQueuedThread() throws InterruptedException {
194 >        final Mutex sync = new Mutex();
195 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
196 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
197 >        assertNull(sync.getFirstQueuedThread());
198 >        sync.acquire(1);
199 >        t1.start();
200 >        Thread.sleep(SHORT_DELAY_MS);
201 >        assertEquals(t1, sync.getFirstQueuedThread());
202 >        t2.start();
203 >        Thread.sleep(SHORT_DELAY_MS);
204 >        assertEquals(t1, sync.getFirstQueuedThread());
205 >        t1.interrupt();
206 >        Thread.sleep(SHORT_DELAY_MS);
207 >        Thread.sleep(SHORT_DELAY_MS);
208 >        assertEquals(t2, sync.getFirstQueuedThread());
209 >        sync.release(1);
210 >        Thread.sleep(SHORT_DELAY_MS);
211 >        assertNull(sync.getFirstQueuedThread());
212 >        t1.join();
213 >        t2.join();
214 >    }
215  
216  
217      /**
218       * hasContended reports false if no thread has ever blocked, else true
219       */
220 <    public void testHasContended() {
221 <        final Mutex lock = new Mutex();
222 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
223 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
224 <        try {
225 <            assertFalse(lock.hasContended());
226 <            lock.acquire(1);
227 <            t1.start();
228 <            Thread.sleep(SHORT_DELAY_MS);
229 <            assertTrue(lock.hasContended());
230 <            t2.start();
231 <            Thread.sleep(SHORT_DELAY_MS);
232 <            assertTrue(lock.hasContended());
233 <            t1.interrupt();
234 <            Thread.sleep(SHORT_DELAY_MS);
235 <            assertTrue(lock.hasContended());
236 <            lock.release(1);
237 <            Thread.sleep(SHORT_DELAY_MS);
238 <            assertTrue(lock.hasContended());
239 <            t1.join();
240 <            t2.join();
241 <        } catch(Exception e){
242 <            unexpectedException();
243 <        }
244 <    }
220 >    public void testHasContended() throws InterruptedException {
221 >        final Mutex sync = new Mutex();
222 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
223 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
224 >        assertFalse(sync.hasContended());
225 >        sync.acquire(1);
226 >        t1.start();
227 >        Thread.sleep(SHORT_DELAY_MS);
228 >        assertTrue(sync.hasContended());
229 >        t2.start();
230 >        Thread.sleep(SHORT_DELAY_MS);
231 >        assertTrue(sync.hasContended());
232 >        t1.interrupt();
233 >        Thread.sleep(SHORT_DELAY_MS);
234 >        assertTrue(sync.hasContended());
235 >        sync.release(1);
236 >        Thread.sleep(SHORT_DELAY_MS);
237 >        assertTrue(sync.hasContended());
238 >        t1.join();
239 >        t2.join();
240 >    }
241 >
242 >    /**
243 >     * getQueuedThreads includes waiting threads
244 >     */
245 >    public void testGetQueuedThreads() throws InterruptedException {
246 >        final Mutex sync = new Mutex();
247 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
248 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
249 >        assertTrue(sync.getQueuedThreads().isEmpty());
250 >        sync.acquire(1);
251 >        assertTrue(sync.getQueuedThreads().isEmpty());
252 >        t1.start();
253 >        Thread.sleep(SHORT_DELAY_MS);
254 >        assertTrue(sync.getQueuedThreads().contains(t1));
255 >        t2.start();
256 >        Thread.sleep(SHORT_DELAY_MS);
257 >        assertTrue(sync.getQueuedThreads().contains(t1));
258 >        assertTrue(sync.getQueuedThreads().contains(t2));
259 >        t1.interrupt();
260 >        Thread.sleep(SHORT_DELAY_MS);
261 >        assertFalse(sync.getQueuedThreads().contains(t1));
262 >        assertTrue(sync.getQueuedThreads().contains(t2));
263 >        sync.release(1);
264 >        Thread.sleep(SHORT_DELAY_MS);
265 >        assertTrue(sync.getQueuedThreads().isEmpty());
266 >        t1.join();
267 >        t2.join();
268 >    }
269 >
270 >    /**
271 >     * getExclusiveQueuedThreads includes waiting threads
272 >     */
273 >    public void testGetExclusiveQueuedThreads() throws InterruptedException {
274 >        final Mutex sync = new Mutex();
275 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
276 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
277 >        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
278 >        sync.acquire(1);
279 >        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
280 >        t1.start();
281 >        Thread.sleep(SHORT_DELAY_MS);
282 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
283 >        t2.start();
284 >        Thread.sleep(SHORT_DELAY_MS);
285 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
286 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
287 >        t1.interrupt();
288 >        Thread.sleep(SHORT_DELAY_MS);
289 >        assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
290 >        assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
291 >        sync.release(1);
292 >        Thread.sleep(SHORT_DELAY_MS);
293 >        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
294 >        t1.join();
295 >        t2.join();
296 >    }
297 >
298 >    /**
299 >     * getSharedQueuedThreads does not include exclusively waiting threads
300 >     */
301 >    public void testGetSharedQueuedThreads() throws InterruptedException {
302 >        final Mutex sync = new Mutex();
303 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
304 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
305 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
306 >        sync.acquire(1);
307 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
308 >        t1.start();
309 >        Thread.sleep(SHORT_DELAY_MS);
310 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
311 >        t2.start();
312 >        Thread.sleep(SHORT_DELAY_MS);
313 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
314 >        t1.interrupt();
315 >        Thread.sleep(SHORT_DELAY_MS);
316 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
317 >        sync.release(1);
318 >        Thread.sleep(SHORT_DELAY_MS);
319 >        assertTrue(sync.getSharedQueuedThreads().isEmpty());
320 >        t1.join();
321 >        t2.join();
322 >    }
323  
324      /**
325       * tryAcquireNanos is interruptible.
326       */
327 <    public void testInterruptedException2() {
328 <        final Mutex lock = new Mutex();
329 <        lock.acquire(1);
330 <        Thread t = new Thread(new Runnable() {
331 <                public void run() {
332 <                    try {
333 <                        lock.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
334 <                        threadShouldThrow();
335 <                    } catch(InterruptedException success){}
336 <                }
337 <            });
338 <        try {
276 <            t.start();
277 <            t.interrupt();
278 <        } catch(Exception e){
279 <            unexpectedException();
280 <        }
327 >    public void testInterruptedException2() throws InterruptedException {
328 >        final Mutex sync = new Mutex();
329 >        sync.acquire(1);
330 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
331 >            public void realRun() throws InterruptedException {
332 >                sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS));
333 >            }});
334 >
335 >        t.start();
336 >        Thread.sleep(SHORT_DELAY_MS);
337 >        t.interrupt();
338 >        t.join();
339      }
340  
341  
342      /**
343 <     * TryAcquire on a locked lock fails
343 >     * TryAcquire on exclusively held sync fails
344       */
345 <    public void testTryAcquireWhenLocked() {
346 <        final Mutex lock = new Mutex();
347 <        lock.acquire(1);
348 <        Thread t = new Thread(new Runnable() {
349 <                public void run() {
350 <                    threadAssertFalse(lock.tryAcquire(1));
351 <                }
352 <            });
353 <        try {
354 <            t.start();
355 <            t.join();
356 <            lock.release(1);
299 <        } catch(Exception e){
300 <            unexpectedException();
301 <        }
302 <    }
345 >    public void testTryAcquireWhenSynced() throws InterruptedException {
346 >        final Mutex sync = new Mutex();
347 >        sync.acquire(1);
348 >        Thread t = new Thread(new CheckedRunnable() {
349 >            public void realRun() {
350 >                assertFalse(sync.tryAcquire(1));
351 >            }});
352 >
353 >        t.start();
354 >        t.join();
355 >        sync.release(1);
356 >    }
357  
358      /**
359 <     * tryAcquireNanos on a locked lock times out
359 >     * tryAcquireNanos on an exclusively held sync times out
360       */
361 <    public void testAcquireNanos_Timeout() {
362 <        final Mutex lock = new Mutex();
363 <        lock.acquire(1);
364 <        Thread t = new Thread(new Runnable() {
365 <                public void run() {
366 <                    try {
367 <                        threadAssertFalse(lock.tryAcquireNanos(1, 1000 * 1000));
368 <                    } catch (Exception ex) {
369 <                        threadUnexpectedException();
370 <                    }
371 <                }
372 <            });
373 <        try {
374 <            t.start();
375 <            t.join();
322 <            lock.release(1);
323 <        } catch(Exception e){
324 <            unexpectedException();
325 <        }
326 <    }
327 <    
328 <  
361 >    public void testAcquireNanos_Timeout() throws InterruptedException {
362 >        final Mutex sync = new Mutex();
363 >        sync.acquire(1);
364 >        Thread t = new Thread(new CheckedRunnable() {
365 >            public void realRun() throws InterruptedException {
366 >                long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS);
367 >                assertFalse(sync.tryAcquireNanos(1, nanos));
368 >            }});
369 >
370 >        t.start();
371 >        t.join();
372 >        sync.release(1);
373 >    }
374 >
375 >
376      /**
377       * getState is true when acquired and false when not
378       */
379 <    public void testGetState() {
380 <        final Mutex lock = new Mutex();
381 <        lock.acquire(1);
382 <        assertTrue(lock.isHeldExclusively());
383 <        lock.release(1);
384 <        assertFalse(lock.isHeldExclusively());
385 <        Thread t = new Thread(new Runnable() {
386 <                public void run() {
387 <                    lock.acquire(1);
388 <                    try {
389 <                        Thread.sleep(SMALL_DELAY_MS);
390 <                    }
391 <                    catch(Exception e) {
392 <                        threadUnexpectedException();
393 <                    }
394 <                    lock.release(1);
395 <                }
396 <            });
350 <        try {
351 <            t.start();
352 <            Thread.sleep(SHORT_DELAY_MS);
353 <            assertTrue(lock.isHeldExclusively());
354 <            t.join();
355 <            assertFalse(lock.isHeldExclusively());
356 <        } catch(Exception e){
357 <            unexpectedException();
358 <        }
379 >    public void testGetState() throws InterruptedException {
380 >        final Mutex sync = new Mutex();
381 >        sync.acquire(1);
382 >        assertTrue(sync.isHeldExclusively());
383 >        sync.release(1);
384 >        assertFalse(sync.isHeldExclusively());
385 >        Thread t = new Thread(new CheckedRunnable() {
386 >            public void realRun() throws InterruptedException {
387 >                sync.acquire(1);
388 >                Thread.sleep(SMALL_DELAY_MS);
389 >                sync.release(1);
390 >            }});
391 >
392 >        t.start();
393 >        Thread.sleep(SHORT_DELAY_MS);
394 >        assertTrue(sync.isHeldExclusively());
395 >        t.join();
396 >        assertFalse(sync.isHeldExclusively());
397      }
398  
399  
400      /**
401       * acquireInterruptibly is interruptible.
402       */
403 <    public void testAcquireInterruptibly1() {
404 <        final Mutex lock = new Mutex();
405 <        lock.acquire(1);
406 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
407 <        try {
408 <            t.start();
409 <            t.interrupt();
410 <            lock.release(1);
411 <            t.join();
412 <        } catch(Exception e){
413 <            unexpectedException();
414 <        }
377 <    }
403 >    public void testAcquireInterruptibly1() throws InterruptedException {
404 >        final Mutex sync = new Mutex();
405 >        sync.acquire(1);
406 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
407 >
408 >        t.start();
409 >        Thread.sleep(SHORT_DELAY_MS);
410 >        t.interrupt();
411 >        Thread.sleep(SHORT_DELAY_MS);
412 >        sync.release(1);
413 >        t.join();
414 >    }
415  
416      /**
417       * acquireInterruptibly succeeds when released, else is interruptible
418       */
419 <    public void testAcquireInterruptibly2() {
420 <        final Mutex lock = new Mutex();
421 <        try {
422 <            lock.acquireInterruptibly(1);
423 <        } catch(Exception e) {
424 <            unexpectedException();
425 <        }
426 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
427 <        try {
391 <            t.start();
392 <            t.interrupt();
393 <            assertTrue(lock.isHeldExclusively());
394 <            t.join();
395 <        } catch(Exception e){
396 <            unexpectedException();
397 <        }
419 >    public void testAcquireInterruptibly2() throws InterruptedException {
420 >        final Mutex sync = new Mutex();
421 >        sync.acquireInterruptibly(1);
422 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
423 >        t.start();
424 >        Thread.sleep(SHORT_DELAY_MS);
425 >        t.interrupt();
426 >        assertTrue(sync.isHeldExclusively());
427 >        t.join();
428      }
429  
430      /**
431 <     * owns is true for a condition created by lock else false
431 >     * owns is true for a condition created by sync else false
432       */
433      public void testOwns() {
434 <        final Mutex lock = new Mutex();
435 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
436 <        final Mutex lock2 = new Mutex();
437 <        assertTrue(lock.owns(c));
438 <        assertFalse(lock2.owns(c));
434 >        final Mutex sync = new Mutex();
435 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
436 >        final Mutex sync2 = new Mutex();
437 >        assertTrue(sync.owns(c));
438 >        assertFalse(sync2.owns(c));
439      }
440  
441      /**
442 <     * Calling await without holding lock throws IllegalMonitorStateException
442 >     * Calling await without holding sync throws IllegalMonitorStateException
443       */
444 <    public void testAwait_IllegalMonitor() {
445 <        final Mutex lock = new Mutex();
446 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
444 >    public void testAwait_IllegalMonitor() throws InterruptedException {
445 >        final Mutex sync = new Mutex();
446 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
447          try {
448              c.await();
449              shouldThrow();
450 <        }
421 <        catch (IllegalMonitorStateException success) {
422 <        }
423 <        catch (Exception ex) {
424 <            unexpectedException();
425 <        }
450 >        } catch (IllegalMonitorStateException success) {}
451      }
452  
453      /**
454 <     * Calling signal without holding lock throws IllegalMonitorStateException
454 >     * Calling signal without holding sync throws IllegalMonitorStateException
455       */
456      public void testSignal_IllegalMonitor() {
457 <        final Mutex lock = new Mutex();
458 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
457 >        final Mutex sync = new Mutex();
458 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
459          try {
460              c.signal();
461              shouldThrow();
462 <        }
438 <        catch (IllegalMonitorStateException success) {
439 <        }
440 <        catch (Exception ex) {
441 <            unexpectedException();
442 <        }
462 >        } catch (IllegalMonitorStateException success) {}
463      }
464  
465      /**
466       * awaitNanos without a signal times out
467       */
468 <    public void testAwaitNanos_Timeout() {
469 <        final Mutex lock = new Mutex();
470 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
471 <        try {
472 <            lock.acquire(1);
473 <            long t = c.awaitNanos(100);
474 <            assertTrue(t <= 0);
455 <            lock.release(1);
456 <        }
457 <        catch (Exception ex) {
458 <            unexpectedException();
459 <        }
468 >    public void testAwaitNanos_Timeout() throws InterruptedException {
469 >        final Mutex sync = new Mutex();
470 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
471 >        sync.acquire(1);
472 >        long t = c.awaitNanos(100);
473 >        assertTrue(t <= 0);
474 >        sync.release(1);
475      }
476  
477      /**
478 <     *  timed await without a signal times out
479 <     */
480 <    public void testAwait_Timeout() {
481 <        final Mutex lock = new Mutex();
482 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
483 <        try {
484 <            lock.acquire(1);
485 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
471 <            lock.release(1);
472 <        }
473 <        catch (Exception ex) {
474 <            unexpectedException();
475 <        }
478 >     * Timed await without a signal times out
479 >     */
480 >    public void testAwait_Timeout() throws InterruptedException {
481 >        final Mutex sync = new Mutex();
482 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
483 >        sync.acquire(1);
484 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
485 >        sync.release(1);
486      }
487  
488      /**
489       * awaitUntil without a signal times out
490       */
491 <    public void testAwaitUntil_Timeout() {
492 <        final Mutex lock = new Mutex();
493 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
494 <        try {
495 <            lock.acquire(1);
496 <            java.util.Date d = new java.util.Date();
497 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
488 <            lock.release(1);
489 <        }
490 <        catch (Exception ex) {
491 <            unexpectedException();
492 <        }
491 >    public void testAwaitUntil_Timeout() throws InterruptedException {
492 >        final Mutex sync = new Mutex();
493 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
494 >        sync.acquire(1);
495 >        java.util.Date d = new java.util.Date();
496 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
497 >        sync.release(1);
498      }
499  
500      /**
501       * await returns when signalled
502       */
503 <    public void testAwait() {
504 <        final Mutex lock = new Mutex();
505 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
506 <        Thread t = new Thread(new Runnable() {
507 <                public void run() {
508 <                    try {
509 <                        lock.acquire(1);
510 <                        c.await();
511 <                        lock.release(1);
507 <                    }
508 <                    catch(InterruptedException e) {
509 <                        threadUnexpectedException();
510 <                    }
511 <                }
512 <            });
503 >    public void testAwait() throws InterruptedException {
504 >        final Mutex sync = new Mutex();
505 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
506 >        Thread t = new Thread(new CheckedRunnable() {
507 >            public void realRun() throws InterruptedException {
508 >                sync.acquire(1);
509 >                c.await();
510 >                sync.release(1);
511 >            }});
512  
513 <        try {
514 <            t.start();
515 <            Thread.sleep(SHORT_DELAY_MS);
516 <            lock.acquire(1);
517 <            c.signal();
518 <            lock.release(1);
519 <            t.join(SHORT_DELAY_MS);
521 <            assertFalse(t.isAlive());
522 <        }
523 <        catch (Exception ex) {
524 <            unexpectedException();
525 <        }
513 >        t.start();
514 >        Thread.sleep(SHORT_DELAY_MS);
515 >        sync.acquire(1);
516 >        c.signal();
517 >        sync.release(1);
518 >        t.join(SHORT_DELAY_MS);
519 >        assertFalse(t.isAlive());
520      }
521  
522  
# Line 531 | Line 525 | public class AbstractQueuedSynchronizerT
525       * hasWaiters throws NPE if null
526       */
527      public void testHasWaitersNPE() {
528 <        final Mutex lock = new Mutex();
528 >        final Mutex sync = new Mutex();
529          try {
530 <            lock.hasWaiters(null);
530 >            sync.hasWaiters(null);
531              shouldThrow();
532 <        } catch (NullPointerException success) {
539 <        } catch (Exception ex) {
540 <            unexpectedException();
541 <        }
532 >        } catch (NullPointerException success) {}
533      }
534  
535      /**
536       * getWaitQueueLength throws NPE if null
537       */
538      public void testGetWaitQueueLengthNPE() {
539 <        final Mutex lock = new Mutex();
539 >        final Mutex sync = new Mutex();
540          try {
541 <            lock.getWaitQueueLength(null);
541 >            sync.getWaitQueueLength(null);
542              shouldThrow();
543 <        } catch (NullPointerException success) {
553 <        } catch (Exception ex) {
554 <            unexpectedException();
555 <        }
543 >        } catch (NullPointerException success) {}
544      }
545  
546  
# Line 560 | Line 548 | public class AbstractQueuedSynchronizerT
548       * getWaitingThreads throws NPE if null
549       */
550      public void testGetWaitingThreadsNPE() {
551 <        final Mutex lock = new Mutex();
551 >        final Mutex sync = new Mutex();
552          try {
553 <            lock.getWaitingThreads(null);
553 >            sync.getWaitingThreads(null);
554              shouldThrow();
555 <        } catch (NullPointerException success) {
568 <        } catch (Exception ex) {
569 <            unexpectedException();
570 <        }
555 >        } catch (NullPointerException success) {}
556      }
557  
558  
# Line 575 | Line 560 | public class AbstractQueuedSynchronizerT
560       * hasWaiters throws IAE if not owned
561       */
562      public void testHasWaitersIAE() {
563 <        final Mutex lock = new Mutex();
564 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
565 <        final Mutex lock2 = new Mutex();
563 >        final Mutex sync = new Mutex();
564 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
565 >        final Mutex sync2 = new Mutex();
566          try {
567 <            lock2.hasWaiters(c);
567 >            sync2.hasWaiters(c);
568              shouldThrow();
569 <        } catch (IllegalArgumentException success) {
585 <        } catch (Exception ex) {
586 <            unexpectedException();
587 <        }
569 >        } catch (IllegalArgumentException success) {}
570      }
571  
572      /**
573 <     * hasWaiters throws IMSE if not locked
573 >     * hasWaiters throws IMSE if not synced
574       */
575      public void testHasWaitersIMSE() {
576 <        final Mutex lock = new Mutex();
577 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
576 >        final Mutex sync = new Mutex();
577 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
578          try {
579 <            lock.hasWaiters(c);
579 >            sync.hasWaiters(c);
580              shouldThrow();
581 <        } catch (IllegalMonitorStateException success) {
600 <        } catch (Exception ex) {
601 <            unexpectedException();
602 <        }
581 >        } catch (IllegalMonitorStateException success) {}
582      }
583  
584  
# Line 607 | Line 586 | public class AbstractQueuedSynchronizerT
586       * getWaitQueueLength throws IAE if not owned
587       */
588      public void testGetWaitQueueLengthIAE() {
589 <        final Mutex lock = new Mutex();
590 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
591 <        final Mutex lock2 = new Mutex();
589 >        final Mutex sync = new Mutex();
590 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
591 >        final Mutex sync2 = new Mutex();
592          try {
593 <            lock2.getWaitQueueLength(c);
593 >            sync2.getWaitQueueLength(c);
594              shouldThrow();
595 <        } catch (IllegalArgumentException success) {
617 <        } catch (Exception ex) {
618 <            unexpectedException();
619 <        }
595 >        } catch (IllegalArgumentException success) {}
596      }
597  
598      /**
599 <     * getWaitQueueLength throws IMSE if not locked
599 >     * getWaitQueueLength throws IMSE if not synced
600       */
601      public void testGetWaitQueueLengthIMSE() {
602 <        final Mutex lock = new Mutex();
603 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
602 >        final Mutex sync = new Mutex();
603 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
604          try {
605 <            lock.getWaitQueueLength(c);
605 >            sync.getWaitQueueLength(c);
606              shouldThrow();
607 <        } catch (IllegalMonitorStateException success) {
632 <        } catch (Exception ex) {
633 <            unexpectedException();
634 <        }
607 >        } catch (IllegalMonitorStateException success) {}
608      }
609  
610  
# Line 639 | Line 612 | public class AbstractQueuedSynchronizerT
612       * getWaitingThreads throws IAE if not owned
613       */
614      public void testGetWaitingThreadsIAE() {
615 <        final Mutex lock = new Mutex();
616 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
617 <        final Mutex lock2 = new Mutex();        
615 >        final Mutex sync = new Mutex();
616 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
617 >        final Mutex sync2 = new Mutex();
618          try {
619 <            lock2.getWaitingThreads(c);
619 >            sync2.getWaitingThreads(c);
620              shouldThrow();
621 <        } catch (IllegalArgumentException success) {
649 <        } catch (Exception ex) {
650 <            unexpectedException();
651 <        }
621 >        } catch (IllegalArgumentException success) {}
622      }
623  
624      /**
625 <     * getWaitingThreads throws IMSE if not locked
625 >     * getWaitingThreads throws IMSE if not synced
626       */
627      public void testGetWaitingThreadsIMSE() {
628 <        final Mutex lock = new Mutex();
629 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
628 >        final Mutex sync = new Mutex();
629 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
630          try {
631 <            lock.getWaitingThreads(c);
631 >            sync.getWaitingThreads(c);
632              shouldThrow();
633 <        } catch (IllegalMonitorStateException success) {
664 <        } catch (Exception ex) {
665 <            unexpectedException();
666 <        }
633 >        } catch (IllegalMonitorStateException success) {}
634      }
635  
636  
# Line 671 | Line 638 | public class AbstractQueuedSynchronizerT
638      /**
639       * hasWaiters returns true when a thread is waiting, else false
640       */
641 <    public void testHasWaiters() {
642 <        final Mutex lock = new Mutex();
643 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
644 <        Thread t = new Thread(new Runnable() {
645 <                public void run() {
646 <                    try {
647 <                        lock.acquire(1);
648 <                        threadAssertFalse(lock.hasWaiters(c));
649 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
650 <                        c.await();
651 <                        lock.release(1);
685 <                    }
686 <                    catch(InterruptedException e) {
687 <                        threadUnexpectedException();
688 <                    }
689 <                }
690 <            });
641 >    public void testHasWaiters() throws InterruptedException {
642 >        final Mutex sync = new Mutex();
643 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
644 >        Thread t = new Thread(new CheckedRunnable() {
645 >            public void realRun() throws InterruptedException {
646 >                sync.acquire(1);
647 >                assertFalse(sync.hasWaiters(c));
648 >                assertEquals(0, sync.getWaitQueueLength(c));
649 >                c.await();
650 >                sync.release(1);
651 >            }});
652  
653 <        try {
654 <            t.start();
655 <            Thread.sleep(SHORT_DELAY_MS);
656 <            lock.acquire(1);
657 <            assertTrue(lock.hasWaiters(c));
658 <            assertEquals(1, lock.getWaitQueueLength(c));
659 <            c.signal();
660 <            lock.release(1);
661 <            Thread.sleep(SHORT_DELAY_MS);
662 <            lock.acquire(1);
663 <            assertFalse(lock.hasWaiters(c));
664 <            assertEquals(0, lock.getWaitQueueLength(c));
665 <            lock.release(1);
666 <            t.join(SHORT_DELAY_MS);
706 <            assertFalse(t.isAlive());
707 <        }
708 <        catch (Exception ex) {
709 <            unexpectedException();
710 <        }
653 >        t.start();
654 >        Thread.sleep(SHORT_DELAY_MS);
655 >        sync.acquire(1);
656 >        assertTrue(sync.hasWaiters(c));
657 >        assertEquals(1, sync.getWaitQueueLength(c));
658 >        c.signal();
659 >        sync.release(1);
660 >        Thread.sleep(SHORT_DELAY_MS);
661 >        sync.acquire(1);
662 >        assertFalse(sync.hasWaiters(c));
663 >        assertEquals(0, sync.getWaitQueueLength(c));
664 >        sync.release(1);
665 >        t.join(SHORT_DELAY_MS);
666 >        assertFalse(t.isAlive());
667      }
668  
669      /**
670       * getWaitQueueLength returns number of waiting threads
671       */
672 <    public void testGetWaitQueueLength() {
673 <        final Mutex lock = new Mutex();
674 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
675 <        Thread t1 = new Thread(new Runnable() {
676 <                public void run() {
677 <                    try {
678 <                        lock.acquire(1);
679 <                        threadAssertFalse(lock.hasWaiters(c));
680 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
681 <                        c.await();
682 <                        lock.release(1);
683 <                    }
684 <                    catch(InterruptedException e) {
685 <                        threadUnexpectedException();
686 <                    }
687 <                }
688 <            });
689 <
690 <        Thread t2 = new Thread(new Runnable() {
691 <                public void run() {
692 <                    try {
693 <                        lock.acquire(1);
694 <                        threadAssertTrue(lock.hasWaiters(c));
695 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
696 <                        c.await();
697 <                        lock.release(1);
698 <                    }
699 <                    catch(InterruptedException e) {
700 <                        threadUnexpectedException();
701 <                    }
702 <                }
703 <            });
704 <
705 <        try {
706 <            t1.start();
707 <            Thread.sleep(SHORT_DELAY_MS);
708 <            t2.start();
709 <            Thread.sleep(SHORT_DELAY_MS);
710 <            lock.acquire(1);
755 <            assertTrue(lock.hasWaiters(c));
756 <            assertEquals(2, lock.getWaitQueueLength(c));
757 <            c.signalAll();
758 <            lock.release(1);
759 <            Thread.sleep(SHORT_DELAY_MS);
760 <            lock.acquire(1);
761 <            assertFalse(lock.hasWaiters(c));
762 <            assertEquals(0, lock.getWaitQueueLength(c));
763 <            lock.release(1);
764 <            t1.join(SHORT_DELAY_MS);
765 <            t2.join(SHORT_DELAY_MS);
766 <            assertFalse(t1.isAlive());
767 <            assertFalse(t2.isAlive());
768 <        }
769 <        catch (Exception ex) {
770 <            unexpectedException();
771 <        }
672 >    public void testGetWaitQueueLength() throws InterruptedException {
673 >        final Mutex sync = new Mutex();
674 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
675 >        Thread t1 = new Thread(new CheckedRunnable() {
676 >            public void realRun() throws InterruptedException {
677 >                sync.acquire(1);
678 >                assertFalse(sync.hasWaiters(c));
679 >                assertEquals(0, sync.getWaitQueueLength(c));
680 >                c.await();
681 >                sync.release(1);
682 >            }});
683 >
684 >        Thread t2 = new Thread(new CheckedRunnable() {
685 >            public void realRun() throws InterruptedException {
686 >                sync.acquire(1);
687 >                assertTrue(sync.hasWaiters(c));
688 >                assertEquals(1, sync.getWaitQueueLength(c));
689 >                c.await();
690 >                sync.release(1);
691 >            }});
692 >
693 >        t1.start();
694 >        Thread.sleep(SHORT_DELAY_MS);
695 >        t2.start();
696 >        Thread.sleep(SHORT_DELAY_MS);
697 >        sync.acquire(1);
698 >        assertTrue(sync.hasWaiters(c));
699 >        assertEquals(2, sync.getWaitQueueLength(c));
700 >        c.signalAll();
701 >        sync.release(1);
702 >        Thread.sleep(SHORT_DELAY_MS);
703 >        sync.acquire(1);
704 >        assertFalse(sync.hasWaiters(c));
705 >        assertEquals(0, sync.getWaitQueueLength(c));
706 >        sync.release(1);
707 >        t1.join(SHORT_DELAY_MS);
708 >        t2.join(SHORT_DELAY_MS);
709 >        assertFalse(t1.isAlive());
710 >        assertFalse(t2.isAlive());
711      }
712  
713      /**
714       * getWaitingThreads returns only and all waiting threads
715       */
716 <    public void testGetWaitingThreads() {
717 <        final Mutex lock = new Mutex();
718 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
719 <        Thread t1 = new Thread(new Runnable() {
720 <                public void run() {
721 <                    try {
722 <                        lock.acquire(1);
723 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
724 <                        c.await();
725 <                        lock.release(1);
726 <                    }
727 <                    catch(InterruptedException e) {
728 <                        threadUnexpectedException();
729 <                    }
730 <                }
731 <            });
732 <
733 <        Thread t2 = new Thread(new Runnable() {
734 <                public void run() {
735 <                    try {
736 <                        lock.acquire(1);
737 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
738 <                        c.await();
739 <                        lock.release(1);
740 <                    }
741 <                    catch(InterruptedException e) {
742 <                        threadUnexpectedException();
743 <                    }
744 <                }
745 <            });
746 <
747 <        try {
748 <            lock.acquire(1);
749 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
750 <            lock.release(1);
751 <            t1.start();
752 <            Thread.sleep(SHORT_DELAY_MS);
753 <            t2.start();
754 <            Thread.sleep(SHORT_DELAY_MS);
755 <            lock.acquire(1);
756 <            assertTrue(lock.hasWaiters(c));
818 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
819 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
820 <            c.signalAll();
821 <            lock.release(1);
822 <            Thread.sleep(SHORT_DELAY_MS);
823 <            lock.acquire(1);
824 <            assertFalse(lock.hasWaiters(c));
825 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
826 <            lock.release(1);
827 <            t1.join(SHORT_DELAY_MS);
828 <            t2.join(SHORT_DELAY_MS);
829 <            assertFalse(t1.isAlive());
830 <            assertFalse(t2.isAlive());
831 <        }
832 <        catch (Exception ex) {
833 <            unexpectedException();
834 <        }
716 >    public void testGetWaitingThreads() throws InterruptedException {
717 >        final Mutex sync = new Mutex();
718 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
719 >        Thread t1 = new Thread(new CheckedRunnable() {
720 >            public void realRun() throws InterruptedException {
721 >                sync.acquire(1);
722 >                assertTrue(sync.getWaitingThreads(c).isEmpty());
723 >                c.await();
724 >                sync.release(1);
725 >            }});
726 >
727 >        Thread t2 = new Thread(new CheckedRunnable() {
728 >            public void realRun() throws InterruptedException {
729 >                sync.acquire(1);
730 >                assertFalse(sync.getWaitingThreads(c).isEmpty());
731 >                c.await();
732 >                sync.release(1);
733 >            }});
734 >
735 >        sync.acquire(1);
736 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
737 >        sync.release(1);
738 >        t1.start();
739 >        Thread.sleep(SHORT_DELAY_MS);
740 >        t2.start();
741 >        Thread.sleep(SHORT_DELAY_MS);
742 >        sync.acquire(1);
743 >        assertTrue(sync.hasWaiters(c));
744 >        assertTrue(sync.getWaitingThreads(c).contains(t1));
745 >        assertTrue(sync.getWaitingThreads(c).contains(t2));
746 >        c.signalAll();
747 >        sync.release(1);
748 >        Thread.sleep(SHORT_DELAY_MS);
749 >        sync.acquire(1);
750 >        assertFalse(sync.hasWaiters(c));
751 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
752 >        sync.release(1);
753 >        t1.join(SHORT_DELAY_MS);
754 >        t2.join(SHORT_DELAY_MS);
755 >        assertFalse(t1.isAlive());
756 >        assertFalse(t2.isAlive());
757      }
758  
759  
# Line 839 | Line 761 | public class AbstractQueuedSynchronizerT
761      /**
762       * awaitUninterruptibly doesn't abort on interrupt
763       */
764 <    public void testAwaitUninterruptibly() {
765 <        final Mutex lock = new Mutex();
766 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
767 <        Thread t = new Thread(new Runnable() {
768 <                public void run() {
769 <                    lock.acquire(1);
770 <                    c.awaitUninterruptibly();
771 <                    lock.release(1);
772 <                }
851 <            });
764 >    public void testAwaitUninterruptibly() throws InterruptedException {
765 >        final Mutex sync = new Mutex();
766 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
767 >        Thread t = new Thread(new CheckedRunnable() {
768 >            public void realRun() {
769 >                sync.acquire(1);
770 >                c.awaitUninterruptibly();
771 >                sync.release(1);
772 >            }});
773  
774 <        try {
775 <            t.start();
776 <            Thread.sleep(SHORT_DELAY_MS);
777 <            t.interrupt();
778 <            lock.acquire(1);
779 <            c.signal();
780 <            lock.release(1);
781 <            assert(t.isInterrupted());
861 <            t.join(SHORT_DELAY_MS);
862 <            assertFalse(t.isAlive());
863 <        }
864 <        catch (Exception ex) {
865 <            unexpectedException();
866 <        }
774 >        t.start();
775 >        Thread.sleep(SHORT_DELAY_MS);
776 >        t.interrupt();
777 >        sync.acquire(1);
778 >        c.signal();
779 >        sync.release(1);
780 >        t.join(SHORT_DELAY_MS);
781 >        assertFalse(t.isAlive());
782      }
783  
784      /**
785       * await is interruptible
786       */
787 <    public void testAwait_Interrupt() {
788 <        final Mutex lock = new Mutex();
789 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
790 <        Thread t = new Thread(new Runnable() {
791 <                public void run() {
792 <                    try {
793 <                        lock.acquire(1);
794 <                        c.await();
880 <                        lock.release(1);
881 <                        threadShouldThrow();
882 <                    }
883 <                    catch(InterruptedException success) {
884 <                    }
885 <                }
886 <            });
787 >    public void testAwait_Interrupt() throws InterruptedException {
788 >        final Mutex sync = new Mutex();
789 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
790 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
791 >            public void realRun() throws InterruptedException {
792 >                sync.acquire(1);
793 >                c.await();
794 >            }});
795  
796 <        try {
797 <            t.start();
798 <            Thread.sleep(SHORT_DELAY_MS);
799 <            t.interrupt();
800 <            t.join(SHORT_DELAY_MS);
893 <            assertFalse(t.isAlive());
894 <        }
895 <        catch (Exception ex) {
896 <            unexpectedException();
897 <        }
796 >        t.start();
797 >        Thread.sleep(SHORT_DELAY_MS);
798 >        t.interrupt();
799 >        t.join(SHORT_DELAY_MS);
800 >        assertFalse(t.isAlive());
801      }
802  
803      /**
804       * awaitNanos is interruptible
805       */
806 <    public void testAwaitNanos_Interrupt() {
807 <        final Mutex lock = new Mutex();
808 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
809 <        Thread t = new Thread(new Runnable() {
810 <                public void run() {
811 <                    try {
812 <                        lock.acquire(1);
813 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
911 <                        lock.release(1);
912 <                        threadShouldThrow();
913 <                    }
914 <                    catch(InterruptedException success) {
915 <                    }
916 <                }
917 <            });
806 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
807 >        final Mutex sync = new Mutex();
808 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
809 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
810 >            public void realRun() throws InterruptedException {
811 >                sync.acquire(1);
812 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
813 >            }});
814  
815 <        try {
816 <            t.start();
817 <            Thread.sleep(SHORT_DELAY_MS);
818 <            t.interrupt();
819 <            t.join(SHORT_DELAY_MS);
924 <            assertFalse(t.isAlive());
925 <        }
926 <        catch (Exception ex) {
927 <            unexpectedException();
928 <        }
815 >        t.start();
816 >        Thread.sleep(SHORT_DELAY_MS);
817 >        t.interrupt();
818 >        t.join(SHORT_DELAY_MS);
819 >        assertFalse(t.isAlive());
820      }
821  
822      /**
823       * awaitUntil is interruptible
824       */
825 <    public void testAwaitUntil_Interrupt() {
826 <        final Mutex lock = new Mutex();
827 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
828 <        Thread t = new Thread(new Runnable() {
829 <                public void run() {
830 <                    try {
831 <                        lock.acquire(1);
832 <                        java.util.Date d = new java.util.Date();
833 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
943 <                        lock.release(1);
944 <                        threadShouldThrow();
945 <                    }
946 <                    catch(InterruptedException success) {
947 <                    }
948 <                }
949 <            });
825 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
826 >        final Mutex sync = new Mutex();
827 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
828 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
829 >            public void realRun() throws InterruptedException {
830 >                sync.acquire(1);
831 >                java.util.Date d = new java.util.Date();
832 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
833 >            }});
834  
835 <        try {
836 <            t.start();
837 <            Thread.sleep(SHORT_DELAY_MS);
838 <            t.interrupt();
839 <            t.join(SHORT_DELAY_MS);
956 <            assertFalse(t.isAlive());
957 <        }
958 <        catch (Exception ex) {
959 <            unexpectedException();
960 <        }
835 >        t.start();
836 >        Thread.sleep(SHORT_DELAY_MS);
837 >        t.interrupt();
838 >        t.join(SHORT_DELAY_MS);
839 >        assertFalse(t.isAlive());
840      }
841  
842      /**
843       * signalAll wakes up all threads
844       */
845 <    public void testSignalAll() {
846 <        final Mutex lock = new Mutex();
847 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
848 <        Thread t1 = new Thread(new Runnable() {
849 <                public void run() {
850 <                    try {
851 <                        lock.acquire(1);
852 <                        c.await();
853 <                        lock.release(1);
854 <                    }
855 <                    catch(InterruptedException e) {
856 <                        threadUnexpectedException();
857 <                    }
858 <                }
859 <            });
860 <
861 <        Thread t2 = new Thread(new Runnable() {
862 <                public void run() {
863 <                    try {
864 <                        lock.acquire(1);
865 <                        c.await();
866 <                        lock.release(1);
867 <                    }
868 <                    catch(InterruptedException e) {
869 <                        threadUnexpectedException();
870 <                    }
871 <                }
993 <            });
994 <
995 <        try {
996 <            t1.start();
997 <            t2.start();
998 <            Thread.sleep(SHORT_DELAY_MS);
999 <            lock.acquire(1);
1000 <            c.signalAll();
1001 <            lock.release(1);
1002 <            t1.join(SHORT_DELAY_MS);
1003 <            t2.join(SHORT_DELAY_MS);
1004 <            assertFalse(t1.isAlive());
1005 <            assertFalse(t2.isAlive());
1006 <        }
1007 <        catch (Exception ex) {
1008 <            unexpectedException();
1009 <        }
845 >    public void testSignalAll() throws InterruptedException {
846 >        final Mutex sync = new Mutex();
847 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
848 >        Thread t1 = new Thread(new CheckedRunnable() {
849 >            public void realRun() throws InterruptedException {
850 >                sync.acquire(1);
851 >                c.await();
852 >                sync.release(1);
853 >            }});
854 >
855 >        Thread t2 = new Thread(new CheckedRunnable() {
856 >            public void realRun() throws InterruptedException {
857 >                sync.acquire(1);
858 >                c.await();
859 >                sync.release(1);
860 >            }});
861 >
862 >        t1.start();
863 >        t2.start();
864 >        Thread.sleep(SHORT_DELAY_MS);
865 >        sync.acquire(1);
866 >        c.signalAll();
867 >        sync.release(1);
868 >        t1.join(SHORT_DELAY_MS);
869 >        t2.join(SHORT_DELAY_MS);
870 >        assertFalse(t1.isAlive());
871 >        assertFalse(t2.isAlive());
872      }
873  
874  
# Line 1014 | Line 876 | public class AbstractQueuedSynchronizerT
876       * toString indicates current state
877       */
878      public void testToString() {
879 <        Mutex lock = new Mutex();
880 <        String us = lock.toString();
879 >        Mutex sync = new Mutex();
880 >        String us = sync.toString();
881          assertTrue(us.indexOf("State = 0") >= 0);
882 <        lock.acquire(1);
883 <        String ls = lock.toString();
882 >        sync.acquire(1);
883 >        String ls = sync.toString();
884          assertTrue(ls.indexOf("State = 1") >= 0);
885      }
886  
887      /**
888       * A serialized AQS deserializes with current state
889       */
890 <    public void testSerialization() {
890 >    public void testSerialization() throws Exception {
891          Mutex l = new Mutex();
892          l.acquire(1);
893          assertTrue(l.isHeldExclusively());
894  
895 <        try {
896 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
897 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
898 <            out.writeObject(l);
899 <            out.close();
900 <
901 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
902 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
903 <            Mutex r = (Mutex) in.readObject();
1042 <            assertTrue(r.isHeldExclusively());
1043 <        } catch(Exception e){
1044 <            e.printStackTrace();
1045 <            unexpectedException();
1046 <        }
895 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
896 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
897 >        out.writeObject(l);
898 >        out.close();
899 >
900 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
901 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
902 >        Mutex r = (Mutex) in.readObject();
903 >        assertTrue(r.isHeldExclusively());
904      }
905  
906  
# Line 1051 | Line 908 | public class AbstractQueuedSynchronizerT
908       * tryReleaseShared setting state changes getState
909       */
910      public void testGetStateWithReleaseShared() {
911 <        final BooleanLatch l = new BooleanLatch();
912 <        assertFalse(l.isSignalled());
913 <        l.releaseShared(0);
914 <        assertTrue(l.isSignalled());
911 >        final BooleanLatch l = new BooleanLatch();
912 >        assertFalse(l.isSignalled());
913 >        l.releaseShared(0);
914 >        assertTrue(l.isSignalled());
915      }
916  
917      /**
918 <     * release and has no effect when already signalled
918 >     * releaseShared has no effect when already signalled
919       */
920      public void testReleaseShared() {
921 <        final BooleanLatch l = new BooleanLatch();
922 <        assertFalse(l.isSignalled());
923 <        l.releaseShared(0);
924 <        assertTrue(l.isSignalled());
925 <        l.releaseShared(0);
926 <        assertTrue(l.isSignalled());
921 >        final BooleanLatch l = new BooleanLatch();
922 >        assertFalse(l.isSignalled());
923 >        l.releaseShared(0);
924 >        assertTrue(l.isSignalled());
925 >        l.releaseShared(0);
926 >        assertTrue(l.isSignalled());
927      }
928  
929      /**
930       * acquireSharedInterruptibly returns after release, but not before
931       */
932 <    public void testAcquireSharedInterruptibly() {
933 <        final BooleanLatch l = new BooleanLatch();
932 >    public void testAcquireSharedInterruptibly() throws InterruptedException {
933 >        final BooleanLatch l = new BooleanLatch();
934  
935 <        Thread t = new Thread(new Runnable() {
936 <                public void run() {
937 <                    try {
938 <                        threadAssertFalse(l.isSignalled());
939 <                        l.acquireSharedInterruptibly(0);
940 <                        threadAssertTrue(l.isSignalled());
941 <                    } catch(InterruptedException e){
942 <                        threadUnexpectedException();
943 <                    }
944 <                }
945 <            });
946 <        try {
947 <            t.start();
1091 <            assertFalse(l.isSignalled());
1092 <            Thread.sleep(SHORT_DELAY_MS);
1093 <            l.releaseShared(0);
1094 <            assertTrue(l.isSignalled());
1095 <            t.join();
1096 <        } catch (InterruptedException e){
1097 <            unexpectedException();
1098 <        }
935 >        Thread t = new Thread(new CheckedRunnable() {
936 >            public void realRun() throws InterruptedException {
937 >                assertFalse(l.isSignalled());
938 >                l.acquireSharedInterruptibly(0);
939 >                assertTrue(l.isSignalled());
940 >            }});
941 >
942 >        t.start();
943 >        assertFalse(l.isSignalled());
944 >        Thread.sleep(SHORT_DELAY_MS);
945 >        l.releaseShared(0);
946 >        assertTrue(l.isSignalled());
947 >        t.join();
948      }
949 <    
949 >
950  
951      /**
952       * acquireSharedTimed returns after release
953       */
954 <    public void testAsquireSharedTimed() {
955 <        final BooleanLatch l = new BooleanLatch();
954 >    public void testAcquireSharedTimed() throws InterruptedException {
955 >        final BooleanLatch l = new BooleanLatch();
956  
957 <        Thread t = new Thread(new Runnable() {
958 <                public void run() {
959 <                    try {
960 <                        threadAssertFalse(l.isSignalled());
961 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
962 <                        threadAssertTrue(l.isSignalled());
963 <
964 <                    } catch(InterruptedException e){
965 <                        threadUnexpectedException();
966 <                    }
967 <                }
968 <            });
969 <        try {
970 <            t.start();
1122 <            assertFalse(l.isSignalled());
1123 <            Thread.sleep(SHORT_DELAY_MS);
1124 <            l.releaseShared(0);
1125 <            assertTrue(l.isSignalled());
1126 <            t.join();
1127 <        } catch (InterruptedException e){
1128 <            unexpectedException();
1129 <        }
957 >        Thread t = new Thread(new CheckedRunnable() {
958 >            public void realRun() throws InterruptedException {
959 >                assertFalse(l.isSignalled());
960 >                long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS);
961 >                assertTrue(l.tryAcquireSharedNanos(0, nanos));
962 >                assertTrue(l.isSignalled());
963 >            }});
964 >
965 >        t.start();
966 >        assertFalse(l.isSignalled());
967 >        Thread.sleep(SHORT_DELAY_MS);
968 >        l.releaseShared(0);
969 >        assertTrue(l.isSignalled());
970 >        t.join();
971      }
972 <    
972 >
973      /**
974       * acquireSharedInterruptibly throws IE if interrupted before released
975       */
976 <    public void testAcquireSharedInterruptibly_InterruptedException() {
976 >    public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
977          final BooleanLatch l = new BooleanLatch();
978 <        Thread t = new Thread(new Runnable() {
979 <                public void run() {
980 <                    try {
981 <                        threadAssertFalse(l.isSignalled());
982 <                        l.acquireSharedInterruptibly(0);
983 <                        threadShouldThrow();
984 <                    } catch(InterruptedException success){}
985 <                }
986 <            });
987 <        t.start();
1147 <        try {
1148 <            assertFalse(l.isSignalled());
1149 <            t.interrupt();
1150 <            t.join();
1151 <        } catch (InterruptedException e){
1152 <            unexpectedException();
1153 <        }
978 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
979 >            public void realRun() throws InterruptedException {
980 >                assertFalse(l.isSignalled());
981 >                l.acquireSharedInterruptibly(0);
982 >            }});
983 >
984 >        t.start();
985 >        assertFalse(l.isSignalled());
986 >        t.interrupt();
987 >        t.join();
988      }
989  
990      /**
991       * acquireSharedTimed throws IE if interrupted before released
992       */
993 <    public void testAcquireSharedNanos_InterruptedException() {
993 >    public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
994          final BooleanLatch l = new BooleanLatch();
995 <        Thread t = new Thread(new Runnable() {
996 <                public void run() {
997 <                    try {
998 <                        threadAssertFalse(l.isSignalled());
999 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
1000 <                        threadShouldThrow();                        
1001 <                    } catch(InterruptedException success){}
1168 <                }
1169 <            });
995 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
996 >            public void realRun() throws InterruptedException {
997 >                assertFalse(l.isSignalled());
998 >                long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
999 >                l.tryAcquireSharedNanos(0, nanos);
1000 >            }});
1001 >
1002          t.start();
1003 <        try {
1004 <            Thread.sleep(SHORT_DELAY_MS);
1005 <            assertFalse(l.isSignalled());
1006 <            t.interrupt();
1175 <            t.join();
1176 <        } catch (InterruptedException e){
1177 <            unexpectedException();
1178 <        }
1003 >        Thread.sleep(SHORT_DELAY_MS);
1004 >        assertFalse(l.isSignalled());
1005 >        t.interrupt();
1006 >        t.join();
1007      }
1008  
1009      /**
1010       * acquireSharedTimed times out if not released before timeout
1011       */
1012 <    public void testAcquireSharedNanos_Timeout() {
1012 >    public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1013          final BooleanLatch l = new BooleanLatch();
1014 <        Thread t = new Thread(new Runnable() {
1015 <                public void run() {
1016 <                    try {
1017 <                        threadAssertFalse(l.isSignalled());
1018 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1019 <                    } catch(InterruptedException ie){
1020 <                        threadUnexpectedException();
1193 <                    }
1194 <                }
1195 <            });
1014 >        Thread t = new Thread(new CheckedRunnable() {
1015 >            public void realRun() throws InterruptedException {
1016 >                assertFalse(l.isSignalled());
1017 >                long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS);
1018 >                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1019 >            }});
1020 >
1021          t.start();
1022 <        try {
1023 <            Thread.sleep(SHORT_DELAY_MS);
1024 <            assertFalse(l.isSignalled());
1200 <            t.join();
1201 <        } catch (InterruptedException e){
1202 <            unexpectedException();
1203 <        }
1022 >        Thread.sleep(SHORT_DELAY_MS);
1023 >        assertFalse(l.isSignalled());
1024 >        t.join();
1025      }
1026  
1206    
1027   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines