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

Comparing jsr166/src/test/tck/PhaserTest.java (file contents):
Revision 1.4 by jsr166, Sat Aug 1 22:09:13 2009 UTC vs.
Revision 1.5 by jsr166, Mon Aug 3 20:33:57 2009 UTC

# Line 101 | Line 101 | public class PhaserTest extends JSR166Te
101              phaser.register();
102              shouldThrow();
103          } catch (IllegalStateException success) {
104        } catch (Exception ex) {
105            threadUnexpectedException(ex);
104          }
105      }
106  
# Line 200 | Line 198 | public class PhaserTest extends JSR166Te
198          Thread thread = null;
199          for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
200              phaser.register();
201 <            thread = new Thread() {
202 <
205 <                public void run() {
201 >            thread = new Thread(new CheckedRunnable() {
202 >                void realRun() {
203                      r.run();
204                      phaser.arriveAndDeregister();
205 <                }
209 <            };
205 >                }});
206              thread.start();
207          }
208  
# Line 306 | Line 302 | public class PhaserTest extends JSR166Te
302       */
303      public void testArriveAndDeregister6() {
304          final Phaser phaser = new Phaser(2);
305 <        new Thread() {
306 <
311 <            public void run() {
305 >        new Thread(new CheckedRunnable() {
306 >            void realRun() {
307                  getRunnable(SHORT_DELAY_MS).run();
308                  phaser.arrive();
309 <            }
315 <        }.start();
309 >            }}).start();
310          phaser.arriveAndAwaitAdvance();
311          int phase = phaser.arriveAndDeregister();
312          assertEquals(phase, phaser.getPhase());
# Line 344 | Line 338 | public class PhaserTest extends JSR166Te
338       */
339      public void testAwaitAdvance3() {
340          final Phaser phaser = new Phaser();
347        Thread th1 = new Thread() {
341  
342 <            public void run() {
343 <                try {
344 <                    phaser.register();
345 <                    getRunnable(LONG_DELAY_MS).run();
346 <                    phaser.awaitAdvance(phaser.arrive());
347 <                } catch (Exception failure) {
355 <                    threadUnexpectedException(failure);
356 <                }
357 <
358 <            }
359 <        };
342 >        Thread th1 = new Thread(new CheckedRunnable() {
343 >            void realRun() throws InterruptedException {
344 >                phaser.register();
345 >                getRunnable(LONG_DELAY_MS).run();
346 >                phaser.awaitAdvance(phaser.arrive());
347 >            }});
348          phaser.register();
349          th1.start();
350          try {
# Line 364 | Line 352 | public class PhaserTest extends JSR166Te
352              th1.interrupt();
353              Thread.sleep(LONG_DELAY_MS);
354              phaser.arrive();
355 <        } catch (Exception failure) {
356 <            unexpectedException();
355 >        } catch (InterruptedException failure) {
356 >            threadUnexpectedException(failure);
357          }
358          assertFalse(th1.isInterrupted());
359      }
# Line 374 | Line 362 | public class PhaserTest extends JSR166Te
362       * awaitAdvance atomically waits for all parties within the same phase to
363       * complete before continuing
364       */
365 <    public void testAwaitAdvance4() {
366 <        final Phaser phaser = new Phaser(four);
365 >    public void testAwaitAdvance4() throws InterruptedException {
366 >        final Phaser phaser = new Phaser(4);
367          final AtomicInteger phaseCount = new AtomicInteger(0);
368 <        for (int i = 0; i < four; i++) {
369 <            new Thread() {
370 <
371 <                public void run() {
368 >        List<Thread> threads = new ArrayList<Thread>();
369 >        for (int i = 0; i < 4; i++) {
370 >            threads.add(new Thread(new CheckedRunnable() {
371 >                void realRun() {
372                      int phase = phaser.arrive();
373                      phaseCount.incrementAndGet();
374                      getRunnable(LONG_DELAY_MS).run();
375                      phaser.awaitAdvance(phase);
376 <                    assertTrue(phaseCount.get() == four);
377 <                }
390 <            }.start();
376 >                    threadAssertTrue(phaseCount.get() == 4);
377 >                }}));
378          }
379 +        for (Thread thread : threads)
380 +            thread.start();
381 +        for (Thread thread : threads)
382 +            thread.join();
383      }
384  
385      /**
# Line 400 | Line 391 | public class PhaserTest extends JSR166Te
391          assertEquals(phase, phaser.getPhase());
392          phaser.register();
393          for (int i = 0; i < eight; i++) {
394 <            new Thread() {
395 <
405 <                public void run() {
394 >            new Thread(new CheckedRunnable() {
395 >                void realRun() {
396                      getRunnable(SHORT_DELAY_MS).run();
397                      phaser.arrive();
398                  }
399 <            }.start();
399 >                }).start();
400              phase = phaser.awaitAdvance(phaser.arrive());
401 <            assertEquals(phase, phaser.getPhase());
401 >            threadAssertEquals(phase, phaser.getPhase());
402          }
403      }
404  
# Line 423 | Line 413 | public class PhaserTest extends JSR166Te
413           * in the main thread arrives quickly so at best this thread
414           * waits for the second thread's party to arrive
415           */
416 <        new Thread() {
417 <
428 <            public void run() {
416 >        new Thread(new CheckedRunnable() {
417 >            void realRun() {
418                  getRunnable(SMALL_DELAY_MS).run();
419                  int phase = phaser.awaitAdvance(phaser.arrive());
420                  /*
# Line 433 | Line 422 | public class PhaserTest extends JSR166Te
422                   */
423                  threadAssertTrue(phase < 0);
424                  threadAssertTrue(phaser.isTerminated());
425 <            }
437 <        }.start();
425 >            }}).start();
426          /*
427           * This thread will cause the first thread run to wait, in doing so
428           * the main thread will force termination in which the first thread
429           * should exit peacefully as this one
430           */
431 <        new Thread() {
432 <
445 <            public void run() {
431 >        new Thread(new CheckedRunnable() {
432 >            void realRun() {
433                  getRunnable(LONG_DELAY_MS).run();
434                  int p1 = phaser.arrive();
435                  int phase = phaser.awaitAdvance(p1);
436                  threadAssertTrue(phase < 0);
437                  threadAssertTrue(phaser.isTerminated());
438 <            }
452 <        }.start();
438 >            }}).start();
439  
440          phaser.arrive();
441          phaser.forceTermination();
# Line 473 | Line 459 | public class PhaserTest extends JSR166Te
459       */
460      public void testArriveAndAwaitAdvance2() {
461          final Phaser phaser = new Phaser(2);
462 <        Thread th = new Thread() {
463 <            public void run() {
464 <                try {
465 <                    phaser.arriveAndAwaitAdvance();
480 <                } catch (Exception failure) {
481 <                    threadUnexpectedException(failure);
482 <                }
483 <            }
484 <        };
462 >        Thread th = new Thread(new CheckedRunnable() {
463 >            void realRun() {
464 >                phaser.arriveAndAwaitAdvance();
465 >            }});
466  
467          try {
468              th.start();
# Line 504 | Line 485 | public class PhaserTest extends JSR166Te
485          final Phaser phaser = new Phaser(1);
486          final AtomicInteger arrivingCount = new AtomicInteger(0);
487          for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
488 <            new Thread() {
489 <
509 <                public void run() {
488 >            new Thread(new CheckedRunnable() {
489 >                void realRun() {
490                      phaser.register();
491                      run.run();
492                      arrivingCount.getAndIncrement();
493                      phaser.arrive();
494 <                }
515 <            }.start();
494 >                }}).start();
495          }
496          int phaseNumber = phaser.arriveAndAwaitAdvance();
497          arrivingCount.incrementAndGet();
# Line 530 | Line 509 | public class PhaserTest extends JSR166Te
509      }
510  
511      private Runnable getRunnable(final long wait) {
512 <        return new Runnable() {
513 <
535 <            public void run() {
512 >        return new CheckedRunnable() {
513 >            void realRun() {
514                  try {
515                      Thread.sleep(wait);
516                  } catch (InterruptedException noop) {
517                  // sleep interruption isn't a problem case for these example
540                } catch (Exception ex) {
541                    threadUnexpectedException(ex);
518                  }
543
519              }
520          };
521      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines