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.25 by jsr166, Tue Nov 17 13:40:14 2009 UTC vs.
Revision 1.31 by jsr166, Sat Nov 21 17:54:04 2009 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  
# 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 <            });
368 <        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, MEDIUM_DELAY_MS * 1000 * 1000);
331 >            }});
332 >
333 >        t.start();
334 >        t.interrupt();
335 >        t.join();
336      }
337  
338  
339      /**
340       * TryAcquire on exclusively held sync fails
341       */
342 <    public void testTryAcquireWhenSynced() {
343 <        final Mutex sync = new Mutex();
344 <        sync.acquire(1);
345 <        Thread t = new Thread(new Runnable() {
346 <                public void run() {
347 <                    threadAssertFalse(sync.tryAcquire(1));
348 <                }
349 <            });
350 <        try {
351 <            t.start();
352 <            t.join();
391 <            sync.release(1);
392 <        } catch (Exception e) {
393 <            unexpectedException();
394 <        }
342 >    public void testTryAcquireWhenSynced() throws InterruptedException {
343 >        final Mutex sync = new Mutex();
344 >        sync.acquire(1);
345 >        Thread t = new Thread(new CheckedRunnable() {
346 >            public void realRun() {
347 >                threadAssertFalse(sync.tryAcquire(1));
348 >            }});
349 >
350 >        t.start();
351 >        t.join();
352 >        sync.release(1);
353      }
354  
355      /**
356       * tryAcquireNanos on an exclusively held sync times out
357       */
358 <    public void testAcquireNanos_Timeout() {
359 <        final Mutex sync = new Mutex();
360 <        sync.acquire(1);
361 <        Thread t = new Thread(new Runnable() {
362 <                public void run() {
363 <                    try {
364 <                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
365 <                    } catch (Exception ex) {
366 <                        threadUnexpectedException();
367 <                    }
368 <                }
411 <            });
412 <        try {
413 <            t.start();
414 <            t.join();
415 <            sync.release(1);
416 <        } catch (Exception e) {
417 <            unexpectedException();
418 <        }
358 >    public void testAcquireNanos_Timeout() throws InterruptedException {
359 >        final Mutex sync = new Mutex();
360 >        sync.acquire(1);
361 >        Thread t = new Thread(new CheckedRunnable() {
362 >            public void realRun() throws InterruptedException {
363 >                threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
364 >            }});
365 >
366 >        t.start();
367 >        t.join();
368 >        sync.release(1);
369      }
370  
371  
372      /**
373       * getState is true when acquired and false when not
374       */
375 <    public void testGetState() {
376 <        final Mutex sync = new Mutex();
377 <        sync.acquire(1);
378 <        assertTrue(sync.isHeldExclusively());
379 <        sync.release(1);
380 <        assertFalse(sync.isHeldExclusively());
381 <        Thread t = new Thread(new Runnable() {
382 <                public void run() {
383 <                    sync.acquire(1);
384 <                    try {
385 <                        Thread.sleep(SMALL_DELAY_MS);
386 <                    }
387 <                    catch (Exception e) {
388 <                        threadUnexpectedException();
389 <                    }
390 <                    sync.release(1);
391 <                }
392 <            });
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 <        }
375 >    public void testGetState() throws InterruptedException {
376 >        final Mutex sync = new Mutex();
377 >        sync.acquire(1);
378 >        assertTrue(sync.isHeldExclusively());
379 >        sync.release(1);
380 >        assertFalse(sync.isHeldExclusively());
381 >        Thread t = new Thread(new CheckedRunnable() {
382 >            public void realRun() throws InterruptedException {
383 >                sync.acquire(1);
384 >                Thread.sleep(SMALL_DELAY_MS);
385 >                sync.release(1);
386 >            }});
387 >
388 >        t.start();
389 >        Thread.sleep(SHORT_DELAY_MS);
390 >        assertTrue(sync.isHeldExclusively());
391 >        t.join();
392 >        assertFalse(sync.isHeldExclusively());
393      }
394  
395  
396      /**
397       * acquireInterruptibly is interruptible.
398       */
399 <    public void testAcquireInterruptibly1() {
400 <        final Mutex sync = new Mutex();
401 <        sync.acquire(1);
402 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
403 <        try {
404 <            t.start();
405 <            Thread.sleep(SHORT_DELAY_MS);
406 <            t.interrupt();
407 <            Thread.sleep(SHORT_DELAY_MS);
408 <            sync.release(1);
409 <            t.join();
469 <        } catch (Exception e) {
470 <            unexpectedException();
471 <        }
399 >    public void testAcquireInterruptibly1() throws InterruptedException {
400 >        final Mutex sync = new Mutex();
401 >        sync.acquire(1);
402 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
403 >
404 >        t.start();
405 >        Thread.sleep(SHORT_DELAY_MS);
406 >        t.interrupt();
407 >        Thread.sleep(SHORT_DELAY_MS);
408 >        sync.release(1);
409 >        t.join();
410      }
411  
412      /**
413       * acquireInterruptibly succeeds when released, else is interruptible
414       */
415 <    public void testAcquireInterruptibly2() {
416 <        final Mutex sync = new Mutex();
417 <        try {
418 <            sync.acquireInterruptibly(1);
419 <        } catch (Exception e) {
420 <            unexpectedException();
421 <        }
422 <        Thread t = new Thread(new InterruptedSyncRunnable(sync));
485 <        try {
486 <            t.start();
487 <            t.interrupt();
488 <            assertTrue(sync.isHeldExclusively());
489 <            t.join();
490 <        } catch (Exception e) {
491 <            unexpectedException();
492 <        }
415 >    public void testAcquireInterruptibly2() throws InterruptedException {
416 >        final Mutex sync = new Mutex();
417 >        sync.acquireInterruptibly(1);
418 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
419 >        t.start();
420 >        t.interrupt();
421 >        assertTrue(sync.isHeldExclusively());
422 >        t.join();
423      }
424  
425      /**
426       * owns is true for a condition created by sync else false
427       */
428      public void testOwns() {
429 <        final Mutex sync = new Mutex();
429 >        final Mutex sync = new Mutex();
430          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
431          final Mutex sync2 = new Mutex();
432          assertTrue(sync.owns(c));
# Line 506 | Line 436 | public class AbstractQueuedSynchronizerT
436      /**
437       * Calling await without holding sync throws IllegalMonitorStateException
438       */
439 <    public void testAwait_IllegalMonitor() {
440 <        final Mutex sync = new Mutex();
439 >    public void testAwait_IllegalMonitor() throws InterruptedException {
440 >        final Mutex sync = new Mutex();
441          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
442          try {
443              c.await();
444              shouldThrow();
445 <        }
516 <        catch (IllegalMonitorStateException success) {
517 <        }
518 <        catch (Exception ex) {
519 <            unexpectedException();
520 <        }
445 >        } catch (IllegalMonitorStateException success) {}
446      }
447  
448      /**
449       * Calling signal without holding sync throws IllegalMonitorStateException
450       */
451      public void testSignal_IllegalMonitor() {
452 <        final Mutex sync = new Mutex();
452 >        final Mutex sync = new Mutex();
453          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
454          try {
455              c.signal();
456              shouldThrow();
457 <        }
533 <        catch (IllegalMonitorStateException success) {
534 <        }
535 <        catch (Exception ex) {
536 <            unexpectedException();
537 <        }
457 >        } catch (IllegalMonitorStateException success) {}
458      }
459  
460      /**
461       * awaitNanos without a signal times out
462       */
463 <    public void testAwaitNanos_Timeout() {
464 <        final Mutex sync = new Mutex();
463 >    public void testAwaitNanos_Timeout() throws InterruptedException {
464 >        final Mutex sync = new Mutex();
465          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
466 <        try {
467 <            sync.acquire(1);
468 <            long t = c.awaitNanos(100);
469 <            assertTrue(t <= 0);
550 <            sync.release(1);
551 <        }
552 <        catch (Exception ex) {
553 <            unexpectedException();
554 <        }
466 >        sync.acquire(1);
467 >        long t = c.awaitNanos(100);
468 >        assertTrue(t <= 0);
469 >        sync.release(1);
470      }
471  
472      /**
473       *  Timed await without a signal times out
474       */
475 <    public void testAwait_Timeout() {
476 <        final Mutex sync = new Mutex();
475 >    public void testAwait_Timeout() throws InterruptedException {
476 >        final Mutex sync = new Mutex();
477          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
478 <        try {
479 <            sync.acquire(1);
480 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
566 <            sync.release(1);
567 <        }
568 <        catch (Exception ex) {
569 <            unexpectedException();
570 <        }
478 >        sync.acquire(1);
479 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
480 >        sync.release(1);
481      }
482  
483      /**
484       * awaitUntil without a signal times out
485       */
486 <    public void testAwaitUntil_Timeout() {
487 <        final Mutex sync = new Mutex();
486 >    public void testAwaitUntil_Timeout() throws InterruptedException {
487 >        final Mutex sync = new Mutex();
488          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
489 <        try {
490 <            sync.acquire(1);
491 <            java.util.Date d = new java.util.Date();
492 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
583 <            sync.release(1);
584 <        }
585 <        catch (Exception ex) {
586 <            unexpectedException();
587 <        }
489 >        sync.acquire(1);
490 >        java.util.Date d = new java.util.Date();
491 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
492 >        sync.release(1);
493      }
494  
495      /**
496       * await returns when signalled
497       */
498 <    public void testAwait() {
499 <        final Mutex sync = new Mutex();
498 >    public void testAwait() throws InterruptedException {
499 >        final Mutex sync = new Mutex();
500          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
501 <        Thread t = new Thread(new Runnable() {
502 <                public void run() {
503 <                    try {
504 <                        sync.acquire(1);
505 <                        c.await();
506 <                        sync.release(1);
602 <                    }
603 <                    catch (InterruptedException e) {
604 <                        threadUnexpectedException();
605 <                    }
606 <                }
607 <            });
501 >        Thread t = new Thread(new CheckedRunnable() {
502 >            public void realRun() throws InterruptedException {
503 >                sync.acquire(1);
504 >                c.await();
505 >                sync.release(1);
506 >            }});
507  
508 <        try {
509 <            t.start();
510 <            Thread.sleep(SHORT_DELAY_MS);
511 <            sync.acquire(1);
512 <            c.signal();
513 <            sync.release(1);
514 <            t.join(SHORT_DELAY_MS);
616 <            assertFalse(t.isAlive());
617 <        }
618 <        catch (Exception ex) {
619 <            unexpectedException();
620 <        }
508 >        t.start();
509 >        Thread.sleep(SHORT_DELAY_MS);
510 >        sync.acquire(1);
511 >        c.signal();
512 >        sync.release(1);
513 >        t.join(SHORT_DELAY_MS);
514 >        assertFalse(t.isAlive());
515      }
516  
517  
# Line 626 | Line 520 | public class AbstractQueuedSynchronizerT
520       * hasWaiters throws NPE if null
521       */
522      public void testHasWaitersNPE() {
523 <        final Mutex sync = new Mutex();
523 >        final Mutex sync = new Mutex();
524          try {
525              sync.hasWaiters(null);
526              shouldThrow();
527 <        } catch (NullPointerException success) {
634 <        } catch (Exception ex) {
635 <            unexpectedException();
636 <        }
527 >        } catch (NullPointerException success) {}
528      }
529  
530      /**
531       * getWaitQueueLength throws NPE if null
532       */
533      public void testGetWaitQueueLengthNPE() {
534 <        final Mutex sync = new Mutex();
534 >        final Mutex sync = new Mutex();
535          try {
536              sync.getWaitQueueLength(null);
537              shouldThrow();
538 <        } catch (NullPointerException success) {
648 <        } catch (Exception ex) {
649 <            unexpectedException();
650 <        }
538 >        } catch (NullPointerException success) {}
539      }
540  
541  
# Line 655 | Line 543 | public class AbstractQueuedSynchronizerT
543       * getWaitingThreads throws NPE if null
544       */
545      public void testGetWaitingThreadsNPE() {
546 <        final Mutex sync = new Mutex();
546 >        final Mutex sync = new Mutex();
547          try {
548              sync.getWaitingThreads(null);
549              shouldThrow();
550 <        } catch (NullPointerException success) {
663 <        } catch (Exception ex) {
664 <            unexpectedException();
665 <        }
550 >        } catch (NullPointerException success) {}
551      }
552  
553  
# Line 670 | Line 555 | public class AbstractQueuedSynchronizerT
555       * hasWaiters throws IAE if not owned
556       */
557      public void testHasWaitersIAE() {
558 <        final Mutex sync = new Mutex();
558 >        final Mutex sync = new Mutex();
559          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
560 <        final Mutex sync2 = new Mutex();
560 >        final Mutex sync2 = new Mutex();
561          try {
562              sync2.hasWaiters(c);
563              shouldThrow();
564 <        } catch (IllegalArgumentException success) {
680 <        } catch (Exception ex) {
681 <            unexpectedException();
682 <        }
564 >        } catch (IllegalArgumentException success) {}
565      }
566  
567      /**
568       * hasWaiters throws IMSE if not synced
569       */
570      public void testHasWaitersIMSE() {
571 <        final Mutex sync = new Mutex();
571 >        final Mutex sync = new Mutex();
572          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
573          try {
574              sync.hasWaiters(c);
575              shouldThrow();
576 <        } catch (IllegalMonitorStateException success) {
695 <        } catch (Exception ex) {
696 <            unexpectedException();
697 <        }
576 >        } catch (IllegalMonitorStateException success) {}
577      }
578  
579  
# Line 702 | Line 581 | public class AbstractQueuedSynchronizerT
581       * getWaitQueueLength throws IAE if not owned
582       */
583      public void testGetWaitQueueLengthIAE() {
584 <        final Mutex sync = new Mutex();
584 >        final Mutex sync = new Mutex();
585          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
586 <        final Mutex sync2 = new Mutex();
586 >        final Mutex sync2 = new Mutex();
587          try {
588              sync2.getWaitQueueLength(c);
589              shouldThrow();
590 <        } catch (IllegalArgumentException success) {
712 <        } catch (Exception ex) {
713 <            unexpectedException();
714 <        }
590 >        } catch (IllegalArgumentException success) {}
591      }
592  
593      /**
594       * getWaitQueueLength throws IMSE if not synced
595       */
596      public void testGetWaitQueueLengthIMSE() {
597 <        final Mutex sync = new Mutex();
597 >        final Mutex sync = new Mutex();
598          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
599          try {
600              sync.getWaitQueueLength(c);
601              shouldThrow();
602 <        } catch (IllegalMonitorStateException success) {
727 <        } catch (Exception ex) {
728 <            unexpectedException();
729 <        }
602 >        } catch (IllegalMonitorStateException success) {}
603      }
604  
605  
# Line 734 | Line 607 | public class AbstractQueuedSynchronizerT
607       * getWaitingThreads throws IAE if not owned
608       */
609      public void testGetWaitingThreadsIAE() {
610 <        final Mutex sync = new Mutex();
610 >        final Mutex sync = new Mutex();
611          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
612 <        final Mutex sync2 = new Mutex();
612 >        final Mutex sync2 = new Mutex();
613          try {
614              sync2.getWaitingThreads(c);
615              shouldThrow();
616 <        } catch (IllegalArgumentException success) {
744 <        } catch (Exception ex) {
745 <            unexpectedException();
746 <        }
616 >        } catch (IllegalArgumentException success) {}
617      }
618  
619      /**
620       * getWaitingThreads throws IMSE if not synced
621       */
622      public void testGetWaitingThreadsIMSE() {
623 <        final Mutex sync = new Mutex();
623 >        final Mutex sync = new Mutex();
624          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
625          try {
626              sync.getWaitingThreads(c);
627              shouldThrow();
628 <        } catch (IllegalMonitorStateException success) {
759 <        } catch (Exception ex) {
760 <            unexpectedException();
761 <        }
628 >        } catch (IllegalMonitorStateException success) {}
629      }
630  
631  
# Line 766 | Line 633 | public class AbstractQueuedSynchronizerT
633      /**
634       * hasWaiters returns true when a thread is waiting, else false
635       */
636 <    public void testHasWaiters() {
637 <        final Mutex sync = new Mutex();
636 >    public void testHasWaiters() throws InterruptedException {
637 >        final Mutex sync = new Mutex();
638          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
639 <        Thread t = new Thread(new Runnable() {
640 <                public void run() {
641 <                    try {
642 <                        sync.acquire(1);
643 <                        threadAssertFalse(sync.hasWaiters(c));
644 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
645 <                        c.await();
646 <                        sync.release(1);
780 <                    }
781 <                    catch (InterruptedException e) {
782 <                        threadUnexpectedException();
783 <                    }
784 <                }
785 <            });
639 >        Thread t = new Thread(new CheckedRunnable() {
640 >            public void realRun() throws InterruptedException {
641 >                sync.acquire(1);
642 >                threadAssertFalse(sync.hasWaiters(c));
643 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
644 >                c.await();
645 >                sync.release(1);
646 >            }});
647  
648 <        try {
649 <            t.start();
650 <            Thread.sleep(SHORT_DELAY_MS);
651 <            sync.acquire(1);
652 <            assertTrue(sync.hasWaiters(c));
653 <            assertEquals(1, sync.getWaitQueueLength(c));
654 <            c.signal();
655 <            sync.release(1);
656 <            Thread.sleep(SHORT_DELAY_MS);
657 <            sync.acquire(1);
658 <            assertFalse(sync.hasWaiters(c));
659 <            assertEquals(0, sync.getWaitQueueLength(c));
660 <            sync.release(1);
661 <            t.join(SHORT_DELAY_MS);
801 <            assertFalse(t.isAlive());
802 <        }
803 <        catch (Exception ex) {
804 <            unexpectedException();
805 <        }
648 >        t.start();
649 >        Thread.sleep(SHORT_DELAY_MS);
650 >        sync.acquire(1);
651 >        assertTrue(sync.hasWaiters(c));
652 >        assertEquals(1, sync.getWaitQueueLength(c));
653 >        c.signal();
654 >        sync.release(1);
655 >        Thread.sleep(SHORT_DELAY_MS);
656 >        sync.acquire(1);
657 >        assertFalse(sync.hasWaiters(c));
658 >        assertEquals(0, sync.getWaitQueueLength(c));
659 >        sync.release(1);
660 >        t.join(SHORT_DELAY_MS);
661 >        assertFalse(t.isAlive());
662      }
663  
664      /**
665       * getWaitQueueLength returns number of waiting threads
666       */
667 <    public void testGetWaitQueueLength() {
668 <        final Mutex sync = new Mutex();
667 >    public void testGetWaitQueueLength() throws InterruptedException {
668 >        final Mutex sync = new Mutex();
669          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
670 <        Thread t1 = new Thread(new Runnable() {
671 <                public void run() {
672 <                    try {
673 <                        sync.acquire(1);
674 <                        threadAssertFalse(sync.hasWaiters(c));
675 <                        threadAssertEquals(0, sync.getWaitQueueLength(c));
676 <                        c.await();
677 <                        sync.release(1);
678 <                    }
679 <                    catch (InterruptedException e) {
680 <                        threadUnexpectedException();
681 <                    }
682 <                }
683 <            });
684 <
685 <        Thread t2 = new Thread(new Runnable() {
686 <                public void run() {
687 <                    try {
688 <                        sync.acquire(1);
689 <                        threadAssertTrue(sync.hasWaiters(c));
690 <                        threadAssertEquals(1, sync.getWaitQueueLength(c));
691 <                        c.await();
692 <                        sync.release(1);
693 <                    }
694 <                    catch (InterruptedException e) {
695 <                        threadUnexpectedException();
696 <                    }
697 <                }
698 <            });
699 <
700 <        try {
701 <            t1.start();
702 <            Thread.sleep(SHORT_DELAY_MS);
703 <            t2.start();
704 <            Thread.sleep(SHORT_DELAY_MS);
705 <            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 <        }
670 >        Thread t1 = new Thread(new CheckedRunnable() {
671 >            public void realRun() throws InterruptedException {
672 >                sync.acquire(1);
673 >                threadAssertFalse(sync.hasWaiters(c));
674 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
675 >                c.await();
676 >                sync.release(1);
677 >            }});
678 >
679 >        Thread t2 = new Thread(new CheckedRunnable() {
680 >            public void realRun() throws InterruptedException {
681 >                sync.acquire(1);
682 >                threadAssertTrue(sync.hasWaiters(c));
683 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
684 >                c.await();
685 >                sync.release(1);
686 >            }});
687 >
688 >        t1.start();
689 >        Thread.sleep(SHORT_DELAY_MS);
690 >        t2.start();
691 >        Thread.sleep(SHORT_DELAY_MS);
692 >        sync.acquire(1);
693 >        assertTrue(sync.hasWaiters(c));
694 >        assertEquals(2, sync.getWaitQueueLength(c));
695 >        c.signalAll();
696 >        sync.release(1);
697 >        Thread.sleep(SHORT_DELAY_MS);
698 >        sync.acquire(1);
699 >        assertFalse(sync.hasWaiters(c));
700 >        assertEquals(0, sync.getWaitQueueLength(c));
701 >        sync.release(1);
702 >        t1.join(SHORT_DELAY_MS);
703 >        t2.join(SHORT_DELAY_MS);
704 >        assertFalse(t1.isAlive());
705 >        assertFalse(t2.isAlive());
706      }
707  
708      /**
709       * getWaitingThreads returns only and all waiting threads
710       */
711 <    public void testGetWaitingThreads() {
712 <        final Mutex sync = new Mutex();
711 >    public void testGetWaitingThreads() throws InterruptedException {
712 >        final Mutex sync = new Mutex();
713          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
714 <        Thread t1 = new Thread(new Runnable() {
715 <                public void run() {
716 <                    try {
717 <                        sync.acquire(1);
718 <                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
719 <                        c.await();
720 <                        sync.release(1);
721 <                    }
722 <                    catch (InterruptedException e) {
723 <                        threadUnexpectedException();
724 <                    }
725 <                }
726 <            });
727 <
728 <        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 <            });
714 >        Thread t1 = new Thread(new CheckedRunnable() {
715 >            public void realRun() throws InterruptedException {
716 >                sync.acquire(1);
717 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
718 >                c.await();
719 >                sync.release(1);
720 >            }});
721 >
722 >        Thread t2 = new Thread(new CheckedRunnable() {
723 >            public void realRun() throws InterruptedException {
724 >                sync.acquire(1);
725 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
726 >                c.await();
727 >                sync.release(1);
728 >            }});
729  
730 <        try {
731 <            sync.acquire(1);
732 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
733 <            sync.release(1);
734 <            t1.start();
735 <            Thread.sleep(SHORT_DELAY_MS);
736 <            t2.start();
737 <            Thread.sleep(SHORT_DELAY_MS);
738 <            sync.acquire(1);
739 <            assertTrue(sync.hasWaiters(c));
740 <            assertTrue(sync.getWaitingThreads(c).contains(t1));
741 <            assertTrue(sync.getWaitingThreads(c).contains(t2));
742 <            c.signalAll();
743 <            sync.release(1);
744 <            Thread.sleep(SHORT_DELAY_MS);
745 <            sync.acquire(1);
746 <            assertFalse(sync.hasWaiters(c));
747 <            assertTrue(sync.getWaitingThreads(c).isEmpty());
748 <            sync.release(1);
749 <            t1.join(SHORT_DELAY_MS);
750 <            t2.join(SHORT_DELAY_MS);
751 <            assertFalse(t1.isAlive());
925 <            assertFalse(t2.isAlive());
926 <        }
927 <        catch (Exception ex) {
928 <            unexpectedException();
929 <        }
730 >        sync.acquire(1);
731 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
732 >        sync.release(1);
733 >        t1.start();
734 >        Thread.sleep(SHORT_DELAY_MS);
735 >        t2.start();
736 >        Thread.sleep(SHORT_DELAY_MS);
737 >        sync.acquire(1);
738 >        assertTrue(sync.hasWaiters(c));
739 >        assertTrue(sync.getWaitingThreads(c).contains(t1));
740 >        assertTrue(sync.getWaitingThreads(c).contains(t2));
741 >        c.signalAll();
742 >        sync.release(1);
743 >        Thread.sleep(SHORT_DELAY_MS);
744 >        sync.acquire(1);
745 >        assertFalse(sync.hasWaiters(c));
746 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
747 >        sync.release(1);
748 >        t1.join(SHORT_DELAY_MS);
749 >        t2.join(SHORT_DELAY_MS);
750 >        assertFalse(t1.isAlive());
751 >        assertFalse(t2.isAlive());
752      }
753  
754  
# Line 934 | Line 756 | public class AbstractQueuedSynchronizerT
756      /**
757       * awaitUninterruptibly doesn't abort on interrupt
758       */
759 <    public void testAwaitUninterruptibly() {
760 <        final Mutex sync = new Mutex();
759 >    public void testAwaitUninterruptibly() throws InterruptedException {
760 >        final Mutex sync = new Mutex();
761          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
762 <        Thread t = new Thread(new Runnable() {
763 <                public void run() {
764 <                    sync.acquire(1);
765 <                    c.awaitUninterruptibly();
766 <                    sync.release(1);
767 <                }
946 <            });
762 >        Thread t = new Thread(new CheckedRunnable() {
763 >            public void realRun() {
764 >                sync.acquire(1);
765 >                c.awaitUninterruptibly();
766 >                sync.release(1);
767 >            }});
768  
769 <        try {
770 <            t.start();
771 <            Thread.sleep(SHORT_DELAY_MS);
772 <            t.interrupt();
773 <            sync.acquire(1);
774 <            c.signal();
775 <            sync.release(1);
776 <            t.join(SHORT_DELAY_MS);
956 <            assertFalse(t.isAlive());
957 <        }
958 <        catch (Exception ex) {
959 <            unexpectedException();
960 <        }
769 >        t.start();
770 >        Thread.sleep(SHORT_DELAY_MS);
771 >        t.interrupt();
772 >        sync.acquire(1);
773 >        c.signal();
774 >        sync.release(1);
775 >        t.join(SHORT_DELAY_MS);
776 >        assertFalse(t.isAlive());
777      }
778  
779      /**
780       * await is interruptible
781       */
782 <    public void testAwait_Interrupt() {
783 <        final Mutex sync = new Mutex();
782 >    public void testAwait_Interrupt() throws InterruptedException {
783 >        final Mutex sync = new Mutex();
784          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
785 <        Thread t = new Thread(new Runnable() {
786 <                public void run() {
787 <                    try {
788 <                        sync.acquire(1);
789 <                        c.await();
974 <                        sync.release(1);
975 <                        threadShouldThrow();
976 <                    }
977 <                    catch (InterruptedException success) {
978 <                    }
979 <                }
980 <            });
785 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
786 >            public void realRun() throws InterruptedException {
787 >                sync.acquire(1);
788 >                c.await();
789 >            }});
790  
791 <        try {
792 <            t.start();
793 <            Thread.sleep(SHORT_DELAY_MS);
794 <            t.interrupt();
795 <            t.join(SHORT_DELAY_MS);
987 <            assertFalse(t.isAlive());
988 <        }
989 <        catch (Exception ex) {
990 <            unexpectedException();
991 <        }
791 >        t.start();
792 >        Thread.sleep(SHORT_DELAY_MS);
793 >        t.interrupt();
794 >        t.join(SHORT_DELAY_MS);
795 >        assertFalse(t.isAlive());
796      }
797  
798      /**
799       * awaitNanos is interruptible
800       */
801 <    public void testAwaitNanos_Interrupt() {
802 <        final Mutex sync = new Mutex();
801 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
802 >        final Mutex sync = new Mutex();
803          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
804 <        Thread t = new Thread(new Runnable() {
805 <                public void run() {
806 <                    try {
807 <                        sync.acquire(1);
808 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1005 <                        sync.release(1);
1006 <                        threadShouldThrow();
1007 <                    }
1008 <                    catch (InterruptedException success) {
1009 <                    }
1010 <                }
1011 <            });
804 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
805 >            public void realRun() throws InterruptedException {
806 >                sync.acquire(1);
807 >                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
808 >            }});
809  
810 <        try {
811 <            t.start();
812 <            Thread.sleep(SHORT_DELAY_MS);
813 <            t.interrupt();
814 <            t.join(SHORT_DELAY_MS);
1018 <            assertFalse(t.isAlive());
1019 <        }
1020 <        catch (Exception ex) {
1021 <            unexpectedException();
1022 <        }
810 >        t.start();
811 >        Thread.sleep(SHORT_DELAY_MS);
812 >        t.interrupt();
813 >        t.join(SHORT_DELAY_MS);
814 >        assertFalse(t.isAlive());
815      }
816  
817      /**
818       * awaitUntil is interruptible
819       */
820 <    public void testAwaitUntil_Interrupt() {
821 <        final Mutex sync = new Mutex();
820 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
821 >        final Mutex sync = new Mutex();
822          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
823 <        Thread t = new Thread(new Runnable() {
824 <                public void run() {
825 <                    try {
826 <                        sync.acquire(1);
827 <                        java.util.Date d = new java.util.Date();
828 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1037 <                        sync.release(1);
1038 <                        threadShouldThrow();
1039 <                    }
1040 <                    catch (InterruptedException success) {
1041 <                    }
1042 <                }
1043 <            });
823 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
824 >            public void realRun() throws InterruptedException {
825 >                sync.acquire(1);
826 >                java.util.Date d = new java.util.Date();
827 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
828 >            }});
829  
830 <        try {
831 <            t.start();
832 <            Thread.sleep(SHORT_DELAY_MS);
833 <            t.interrupt();
834 <            t.join(SHORT_DELAY_MS);
1050 <            assertFalse(t.isAlive());
1051 <        }
1052 <        catch (Exception ex) {
1053 <            unexpectedException();
1054 <        }
830 >        t.start();
831 >        Thread.sleep(SHORT_DELAY_MS);
832 >        t.interrupt();
833 >        t.join(SHORT_DELAY_MS);
834 >        assertFalse(t.isAlive());
835      }
836  
837      /**
838       * signalAll wakes up all threads
839       */
840 <    public void testSignalAll() {
841 <        final Mutex sync = new Mutex();
840 >    public void testSignalAll() throws InterruptedException {
841 >        final Mutex sync = new Mutex();
842          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
843 <        Thread t1 = new Thread(new Runnable() {
844 <                public void run() {
845 <                    try {
846 <                        sync.acquire(1);
847 <                        c.await();
848 <                        sync.release(1);
849 <                    }
850 <                    catch (InterruptedException e) {
851 <                        threadUnexpectedException();
852 <                    }
853 <                }
854 <            });
855 <
856 <        Thread t2 = new Thread(new Runnable() {
857 <                public void run() {
858 <                    try {
859 <                        sync.acquire(1);
860 <                        c.await();
861 <                        sync.release(1);
862 <                    }
863 <                    catch (InterruptedException e) {
864 <                        threadUnexpectedException();
865 <                    }
866 <                }
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 <        }
843 >        Thread t1 = new Thread(new CheckedRunnable() {
844 >            public void realRun() throws InterruptedException {
845 >                sync.acquire(1);
846 >                c.await();
847 >                sync.release(1);
848 >            }});
849 >
850 >        Thread t2 = new Thread(new CheckedRunnable() {
851 >            public void realRun() throws InterruptedException {
852 >                sync.acquire(1);
853 >                c.await();
854 >                sync.release(1);
855 >            }});
856 >
857 >        t1.start();
858 >        t2.start();
859 >        Thread.sleep(SHORT_DELAY_MS);
860 >        sync.acquire(1);
861 >        c.signalAll();
862 >        sync.release(1);
863 >        t1.join(SHORT_DELAY_MS);
864 >        t2.join(SHORT_DELAY_MS);
865 >        assertFalse(t1.isAlive());
866 >        assertFalse(t2.isAlive());
867      }
868  
869  
# Line 1119 | Line 882 | public class AbstractQueuedSynchronizerT
882      /**
883       * A serialized AQS deserializes with current state
884       */
885 <    public void testSerialization() {
885 >    public void testSerialization() throws Exception {
886          Mutex l = new Mutex();
887          l.acquire(1);
888          assertTrue(l.isHeldExclusively());
889  
890 <        try {
891 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
892 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
893 <            out.writeObject(l);
894 <            out.close();
895 <
896 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
897 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
898 <            Mutex r = (Mutex) in.readObject();
1136 <            assertTrue(r.isHeldExclusively());
1137 <        } catch (Exception e) {
1138 <            e.printStackTrace();
1139 <            unexpectedException();
1140 <        }
890 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
891 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
892 >        out.writeObject(l);
893 >        out.close();
894 >
895 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
896 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
897 >        Mutex r = (Mutex) in.readObject();
898 >        assertTrue(r.isHeldExclusively());
899      }
900  
901  
# Line 1145 | Line 903 | public class AbstractQueuedSynchronizerT
903       * tryReleaseShared setting state changes getState
904       */
905      public void testGetStateWithReleaseShared() {
906 <        final BooleanLatch l = new BooleanLatch();
907 <        assertFalse(l.isSignalled());
908 <        l.releaseShared(0);
909 <        assertTrue(l.isSignalled());
906 >        final BooleanLatch l = new BooleanLatch();
907 >        assertFalse(l.isSignalled());
908 >        l.releaseShared(0);
909 >        assertTrue(l.isSignalled());
910      }
911  
912      /**
913       * releaseShared has no effect when already signalled
914       */
915      public void testReleaseShared() {
916 <        final BooleanLatch l = new BooleanLatch();
917 <        assertFalse(l.isSignalled());
918 <        l.releaseShared(0);
919 <        assertTrue(l.isSignalled());
920 <        l.releaseShared(0);
921 <        assertTrue(l.isSignalled());
916 >        final BooleanLatch l = new BooleanLatch();
917 >        assertFalse(l.isSignalled());
918 >        l.releaseShared(0);
919 >        assertTrue(l.isSignalled());
920 >        l.releaseShared(0);
921 >        assertTrue(l.isSignalled());
922      }
923  
924      /**
925       * acquireSharedInterruptibly returns after release, but not before
926       */
927 <    public void testAcquireSharedInterruptibly() {
928 <        final BooleanLatch l = new BooleanLatch();
927 >    public void testAcquireSharedInterruptibly() throws InterruptedException {
928 >        final BooleanLatch l = new BooleanLatch();
929  
930 <        Thread t = new Thread(new Runnable() {
931 <                public void run() {
932 <                    try {
933 <                        threadAssertFalse(l.isSignalled());
934 <                        l.acquireSharedInterruptibly(0);
935 <                        threadAssertTrue(l.isSignalled());
936 <                    } catch (InterruptedException e) {
937 <                        threadUnexpectedException();
938 <                    }
939 <                }
940 <            });
941 <        try {
942 <            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 <        }
930 >        Thread t = new Thread(new CheckedRunnable() {
931 >            public void realRun() throws InterruptedException {
932 >                threadAssertFalse(l.isSignalled());
933 >                l.acquireSharedInterruptibly(0);
934 >                threadAssertTrue(l.isSignalled());
935 >            }});
936 >
937 >        t.start();
938 >        assertFalse(l.isSignalled());
939 >        Thread.sleep(SHORT_DELAY_MS);
940 >        l.releaseShared(0);
941 >        assertTrue(l.isSignalled());
942 >        t.join();
943      }
944  
945  
946      /**
947       * acquireSharedTimed returns after release
948       */
949 <    public void testAsquireSharedTimed() {
950 <        final BooleanLatch l = new BooleanLatch();
949 >    public void testAsquireSharedTimed() throws InterruptedException {
950 >        final BooleanLatch l = new BooleanLatch();
951  
952 <        Thread t = new Thread(new Runnable() {
953 <                public void run() {
954 <                    try {
955 <                        threadAssertFalse(l.isSignalled());
956 <                        threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
957 <                        threadAssertTrue(l.isSignalled());
958 <
959 <                    } catch (InterruptedException e) {
960 <                        threadUnexpectedException();
961 <                    }
962 <                }
963 <            });
964 <        try {
1215 <            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 <        }
952 >        Thread t = new Thread(new CheckedRunnable() {
953 >            public void realRun() throws InterruptedException {
954 >                threadAssertFalse(l.isSignalled());
955 >                threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
956 >                threadAssertTrue(l.isSignalled());
957 >            }});
958 >
959 >        t.start();
960 >        assertFalse(l.isSignalled());
961 >        Thread.sleep(SHORT_DELAY_MS);
962 >        l.releaseShared(0);
963 >        assertTrue(l.isSignalled());
964 >        t.join();
965      }
966  
967      /**
968       * acquireSharedInterruptibly throws IE if interrupted before released
969       */
970 <    public void testAcquireSharedInterruptibly_InterruptedException() {
970 >    public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
971          final BooleanLatch l = new BooleanLatch();
972 <        Thread t = new Thread(new Runnable() {
973 <                public void run() {
974 <                    try {
975 <                        threadAssertFalse(l.isSignalled());
976 <                        l.acquireSharedInterruptibly(0);
977 <                        threadShouldThrow();
978 <                    } catch (InterruptedException success) {}
979 <                }
980 <            });
981 <        t.start();
1241 <        try {
1242 <            assertFalse(l.isSignalled());
1243 <            t.interrupt();
1244 <            t.join();
1245 <        } catch (InterruptedException e) {
1246 <            unexpectedException();
1247 <        }
972 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
973 >            public void realRun() throws InterruptedException {
974 >                threadAssertFalse(l.isSignalled());
975 >                l.acquireSharedInterruptibly(0);
976 >            }});
977 >
978 >        t.start();
979 >        assertFalse(l.isSignalled());
980 >        t.interrupt();
981 >        t.join();
982      }
983  
984      /**
985       * acquireSharedTimed throws IE if interrupted before released
986       */
987 <    public void testAcquireSharedNanos_InterruptedException() {
987 >    public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
988          final BooleanLatch l = new BooleanLatch();
989 <        Thread t = new Thread(new Runnable() {
990 <                public void run() {
991 <                    try {
992 <                        threadAssertFalse(l.isSignalled());
993 <                        l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
994 <                        threadShouldThrow();
1261 <                    } catch (InterruptedException success) {}
1262 <                }
1263 <            });
989 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
990 >            public void realRun() throws InterruptedException {
991 >                threadAssertFalse(l.isSignalled());
992 >                l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
993 >            }});
994 >
995          t.start();
996 <        try {
997 <            Thread.sleep(SHORT_DELAY_MS);
998 <            assertFalse(l.isSignalled());
999 <            t.interrupt();
1269 <            t.join();
1270 <        } catch (InterruptedException e) {
1271 <            unexpectedException();
1272 <        }
996 >        Thread.sleep(SHORT_DELAY_MS);
997 >        assertFalse(l.isSignalled());
998 >        t.interrupt();
999 >        t.join();
1000      }
1001  
1002      /**
1003       * acquireSharedTimed times out if not released before timeout
1004       */
1005 <    public void testAcquireSharedNanos_Timeout() {
1005 >    public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1006          final BooleanLatch l = new BooleanLatch();
1007 <        Thread t = new Thread(new Runnable() {
1008 <                public void run() {
1009 <                    try {
1010 <                        threadAssertFalse(l.isSignalled());
1011 <                        threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1012 <                    } catch (InterruptedException ie) {
1286 <                        threadUnexpectedException();
1287 <                    }
1288 <                }
1289 <            });
1007 >        Thread t = new Thread(new CheckedRunnable() {
1008 >            public void realRun() throws InterruptedException {
1009 >                threadAssertFalse(l.isSignalled());
1010 >                threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1011 >            }});
1012 >
1013          t.start();
1014 <        try {
1015 <            Thread.sleep(SHORT_DELAY_MS);
1016 <            assertFalse(l.isSignalled());
1294 <            t.join();
1295 <        } catch (InterruptedException e) {
1296 <            unexpectedException();
1297 <        }
1014 >        Thread.sleep(SHORT_DELAY_MS);
1015 >        assertFalse(l.isSignalled());
1016 >        t.join();
1017      }
1018  
1300
1019   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines