test/java/util/concurrent/Phaser/Basic.java

Print this page




  35  * @test
  36  * @bug 6445158
  37  * @summary Basic tests for Phaser
  38  * @author Chris Hegarty
  39  */
  40 
  41 import java.util.Iterator;
  42 import java.util.LinkedList;
  43 import java.util.concurrent.Phaser;
  44 import java.util.concurrent.TimeUnit;
  45 import java.util.concurrent.TimeoutException;
  46 import java.util.concurrent.atomic.AtomicInteger;
  47 import static java.util.concurrent.TimeUnit.*;
  48 
  49 public class Basic {
  50 
  51     private static void checkTerminated(final Phaser phaser) {
  52         check(phaser.isTerminated());
  53         int unarriverParties = phaser.getUnarrivedParties();
  54         int registeredParties = phaser.getRegisteredParties();
  55         equal(phaser.arrive(), -1);
  56         equal(phaser.arriveAndDeregister(), -1);
  57         equal(phaser.arriveAndAwaitAdvance(), -1);
  58         equal(phaser.bulkRegister(10), -1);
  59         equal(phaser.getPhase(), -1);
  60         equal(phaser.register(), -1);

  61         try {
  62             equal(phaser.awaitAdvanceInterruptibly(0), -1);
  63             equal(phaser.awaitAdvanceInterruptibly(0, 10, SECONDS), -1);
  64         } catch (Exception ie) {
  65             unexpected(ie);
  66         }
  67         equal(phaser.getUnarrivedParties(), unarriverParties);
  68         equal(phaser.getRegisteredParties(), registeredParties);
  69     }
  70 
  71     private static void checkResult(Arriver a, Class<? extends Throwable> c) {
  72         Throwable t = a.result();
  73         if (! ((t == null && c == null) || (c != null && c.isInstance(t)))) {
  74             //      t.printStackTrace();
  75             fail("Mismatch in thread " +
  76                  a.getName() + ": " +
  77                  t + ", " +
  78                  (c == null ? "<null>" : c.getName()));
  79         } else {
  80             pass();
  81         }
  82     }
  83 
  84     //----------------------------------------------------------------
  85     // Mechanism to get all test threads into "running" mode.
  86     //----------------------------------------------------------------
  87     private static Phaser atTheStartingGate = new Phaser(3);
  88 
  89     private static void toTheStartingGate() {
  90         try {
  91             boolean expectNextPhase = false;
  92             if (atTheStartingGate.getUnarrivedParties() == 1) {
  93                 expectNextPhase = true;
  94             }
  95             int phase = atTheStartingGate.getPhase();
  96             equal(phase, atTheStartingGate.arrive());
  97             int AwaitPhase = atTheStartingGate.awaitAdvanceInterruptibly(phase,
  98                                                         10,
  99                                                         SECONDS);
 100             if (expectNextPhase) check(AwaitPhase == (phase + 1));
 101 
 102             pass();
 103         } catch (Throwable t) {
 104             unexpected(t);
 105            // reset(atTheStartingGate);
 106             throw new Error(t);
 107         }
 108     }
 109 
 110     //----------------------------------------------------------------
 111     // Convenience methods for creating threads that call arrive,
 112     // awaitAdvance, arriveAndAwaitAdvance, awaitAdvanceInterruptibly
 113     //----------------------------------------------------------------
 114     private static abstract class Arriver extends Thread {
 115         static AtomicInteger count = new AtomicInteger(1);
 116 
 117         Arriver() {
 118             this("Arriver");
 119         }
 120 


 254                 Awaiter a1 = awaiter(phaser, 10, SECONDS); a1.start();
 255                 Arriver a2 = arrivers.next(); a2.start();
 256                 toTheStartingGate();
 257                 a1.interrupt();
 258                 a1.join();
 259                 phaser.arriveAndAwaitAdvance();
 260                 a2.join();
 261                 checkResult(a1, InterruptedException.class);
 262                 checkResult(a2, null);
 263                 check(!phaser.isTerminated());
 264                 equal(phaser.getRegisteredParties(), 3);
 265                 equal(phaser.getArrivedParties(), 0);
 266                 phase++;
 267             }
 268         } catch (Throwable t) { unexpected(t); }
 269 
 270         //----------------------------------------------------------------
 271         // Phaser is terminated while threads are waiting
 272         //----------------------------------------------------------------
 273         try {

 274             Phaser phaser = new Phaser(3);
 275             Iterator<Awaiter> awaiters = awaiterIterator(phaser);
 276             for (int i = 0; i < 4; i++) {
 277                 Arriver a1 = awaiters.next(); a1.start();
 278                 Arriver a2 = awaiters.next(); a2.start();
 279                 toTheStartingGate();
 280                 while (phaser.getArrivedParties() < 2) Thread.yield();

 281                 phaser.forceTermination();
 282                 a1.join();
 283                 a2.join();
 284                 check(a1.phase == -1);
 285                 check(a2.phase == -1);
 286                 int arrivedParties = phaser.getArrivedParties();
 287                 checkTerminated(phaser);
 288                 equal(phaser.getArrivedParties(), arrivedParties);
 289             }
 290         } catch (Throwable t) { unexpected(t); }
 291 
 292         //----------------------------------------------------------------
 293         // Adds new unarrived parties to this phaser
 294         //----------------------------------------------------------------
 295         try {
 296             Phaser phaser = new Phaser(1);
 297             Iterator<Arriver> arrivers = arriverIterator(phaser);
 298             LinkedList<Arriver> arriverList = new LinkedList<Arriver>();
 299             int phase = phaser.getPhase();
 300             for (int i = 1; i < 5; i++) {
 301                 atTheStartingGate = new Phaser(1+(3*i));
 302                 check(phaser.getPhase() == phase);
 303                 // register 3 more
 304                 phaser.register(); phaser.register(); phaser.register();
 305                 for (int z=0; z<(3*i); z++) {




  35  * @test
  36  * @bug 6445158
  37  * @summary Basic tests for Phaser
  38  * @author Chris Hegarty
  39  */
  40 
  41 import java.util.Iterator;
  42 import java.util.LinkedList;
  43 import java.util.concurrent.Phaser;
  44 import java.util.concurrent.TimeUnit;
  45 import java.util.concurrent.TimeoutException;
  46 import java.util.concurrent.atomic.AtomicInteger;
  47 import static java.util.concurrent.TimeUnit.*;
  48 
  49 public class Basic {
  50 
  51     private static void checkTerminated(final Phaser phaser) {
  52         check(phaser.isTerminated());
  53         int unarriverParties = phaser.getUnarrivedParties();
  54         int registeredParties = phaser.getRegisteredParties();
  55         int phase = phaser.getPhase();
  56         check(phase < 0);
  57         equal(phase, phaser.arrive());
  58         equal(phase, phaser.arriveAndDeregister());
  59         equal(phase, phaser.arriveAndAwaitAdvance());
  60         equal(phase, phaser.bulkRegister(10));
  61         equal(phase, phaser.register());
  62         try {
  63             equal(phase, phaser.awaitAdvanceInterruptibly(0));
  64             equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS));
  65         } catch (Exception ie) {
  66             unexpected(ie);
  67         }
  68         equal(phaser.getUnarrivedParties(), unarriverParties);
  69         equal(phaser.getRegisteredParties(), registeredParties);
  70     }
  71 
  72     private static void checkResult(Arriver a, Class<? extends Throwable> c) {
  73         Throwable t = a.result();
  74         if (! ((t == null && c == null) || (c != null && c.isInstance(t)))) {
  75             //      t.printStackTrace();
  76             fail("Mismatch in thread " +
  77                  a.getName() + ": " +
  78                  t + ", " +
  79                  (c == null ? "<null>" : c.getName()));
  80         } else {
  81             pass();
  82         }
  83     }
  84 
  85     //----------------------------------------------------------------
  86     // Mechanism to get all test threads into "running" mode.
  87     //----------------------------------------------------------------
  88     private static Phaser atTheStartingGate = new Phaser(3);
  89 
  90     private static void toTheStartingGate() {
  91         try {
  92             boolean expectNextPhase = false;
  93             if (atTheStartingGate.getUnarrivedParties() == 1) {
  94                 expectNextPhase = true;
  95             }
  96             int phase = atTheStartingGate.getPhase();
  97             equal(phase, atTheStartingGate.arrive());
  98             int awaitPhase = atTheStartingGate.awaitAdvanceInterruptibly
  99                 (phase, 10, SECONDS);
 100             if (expectNextPhase) check(awaitPhase == (phase + 1));

 101 
 102             pass();
 103         } catch (Throwable t) {
 104             unexpected(t);
 105            // reset(atTheStartingGate);
 106             throw new Error(t);
 107         }
 108     }
 109 
 110     //----------------------------------------------------------------
 111     // Convenience methods for creating threads that call arrive,
 112     // awaitAdvance, arriveAndAwaitAdvance, awaitAdvanceInterruptibly
 113     //----------------------------------------------------------------
 114     private static abstract class Arriver extends Thread {
 115         static AtomicInteger count = new AtomicInteger(1);
 116 
 117         Arriver() {
 118             this("Arriver");
 119         }
 120 


 254                 Awaiter a1 = awaiter(phaser, 10, SECONDS); a1.start();
 255                 Arriver a2 = arrivers.next(); a2.start();
 256                 toTheStartingGate();
 257                 a1.interrupt();
 258                 a1.join();
 259                 phaser.arriveAndAwaitAdvance();
 260                 a2.join();
 261                 checkResult(a1, InterruptedException.class);
 262                 checkResult(a2, null);
 263                 check(!phaser.isTerminated());
 264                 equal(phaser.getRegisteredParties(), 3);
 265                 equal(phaser.getArrivedParties(), 0);
 266                 phase++;
 267             }
 268         } catch (Throwable t) { unexpected(t); }
 269 
 270         //----------------------------------------------------------------
 271         // Phaser is terminated while threads are waiting
 272         //----------------------------------------------------------------
 273         try {
 274             for (int i = 0; i < 4; i++) {
 275                 Phaser phaser = new Phaser(3);
 276                 Iterator<Awaiter> awaiters = awaiterIterator(phaser);

 277                 Arriver a1 = awaiters.next(); a1.start();
 278                 Arriver a2 = awaiters.next(); a2.start();
 279                 toTheStartingGate();
 280                 while (phaser.getArrivedParties() < 2) Thread.yield();
 281                 equal(0, phaser.getPhase());
 282                 phaser.forceTermination();
 283                 a1.join();
 284                 a2.join();
 285                 equal(0 + Integer.MIN_VALUE, a1.phase);
 286                 equal(0 + Integer.MIN_VALUE, a2.phase);
 287                 int arrivedParties = phaser.getArrivedParties();
 288                 checkTerminated(phaser);
 289                 equal(phaser.getArrivedParties(), arrivedParties);
 290             }
 291         } catch (Throwable t) { unexpected(t); }
 292 
 293         //----------------------------------------------------------------
 294         // Adds new unarrived parties to this phaser
 295         //----------------------------------------------------------------
 296         try {
 297             Phaser phaser = new Phaser(1);
 298             Iterator<Arriver> arrivers = arriverIterator(phaser);
 299             LinkedList<Arriver> arriverList = new LinkedList<Arriver>();
 300             int phase = phaser.getPhase();
 301             for (int i = 1; i < 5; i++) {
 302                 atTheStartingGate = new Phaser(1+(3*i));
 303                 check(phaser.getPhase() == phase);
 304                 // register 3 more
 305                 phaser.register(); phaser.register(); phaser.register();
 306                 for (int z=0; z<(3*i); z++) {