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.23 by jsr166, Mon Nov 16 04:57:09 2009 UTC vs.
Revision 1.34 by jsr166, Wed Aug 25 00:07:02 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines