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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines