jdk/test/java/util/concurrent/Phaser/Basic.java
changeset 7976 f273c0d04215
parent 5506 202f599c92aa
child 9242 ef138d47df58
equal deleted inserted replaced
7975:f0de2d05f34c 7976:f273c0d04215
    50 
    50 
    51     private static void checkTerminated(final Phaser phaser) {
    51     private static void checkTerminated(final Phaser phaser) {
    52         check(phaser.isTerminated());
    52         check(phaser.isTerminated());
    53         int unarriverParties = phaser.getUnarrivedParties();
    53         int unarriverParties = phaser.getUnarrivedParties();
    54         int registeredParties = phaser.getRegisteredParties();
    54         int registeredParties = phaser.getRegisteredParties();
    55         equal(phaser.arrive(), -1);
    55         int phase = phaser.getPhase();
    56         equal(phaser.arriveAndDeregister(), -1);
    56         check(phase < 0);
    57         equal(phaser.arriveAndAwaitAdvance(), -1);
    57         equal(phase, phaser.arrive());
    58         equal(phaser.bulkRegister(10), -1);
    58         equal(phase, phaser.arriveAndDeregister());
    59         equal(phaser.getPhase(), -1);
    59         equal(phase, phaser.arriveAndAwaitAdvance());
    60         equal(phaser.register(), -1);
    60         equal(phase, phaser.bulkRegister(10));
    61         try {
    61         equal(phase, phaser.register());
    62             equal(phaser.awaitAdvanceInterruptibly(0), -1);
    62         try {
    63             equal(phaser.awaitAdvanceInterruptibly(0, 10, SECONDS), -1);
    63             equal(phase, phaser.awaitAdvanceInterruptibly(0));
       
    64             equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS));
    64         } catch (Exception ie) {
    65         } catch (Exception ie) {
    65             unexpected(ie);
    66             unexpected(ie);
    66         }
    67         }
    67         equal(phaser.getUnarrivedParties(), unarriverParties);
    68         equal(phaser.getUnarrivedParties(), unarriverParties);
    68         equal(phaser.getRegisteredParties(), registeredParties);
    69         equal(phaser.getRegisteredParties(), registeredParties);
    92             if (atTheStartingGate.getUnarrivedParties() == 1) {
    93             if (atTheStartingGate.getUnarrivedParties() == 1) {
    93                 expectNextPhase = true;
    94                 expectNextPhase = true;
    94             }
    95             }
    95             int phase = atTheStartingGate.getPhase();
    96             int phase = atTheStartingGate.getPhase();
    96             equal(phase, atTheStartingGate.arrive());
    97             equal(phase, atTheStartingGate.arrive());
    97             int AwaitPhase = atTheStartingGate.awaitAdvanceInterruptibly(phase,
    98             int awaitPhase = atTheStartingGate.awaitAdvanceInterruptibly
    98                                                         10,
    99                 (phase, 10, SECONDS);
    99                                                         SECONDS);
   100             if (expectNextPhase) check(awaitPhase == (phase + 1));
   100             if (expectNextPhase) check(AwaitPhase == (phase + 1));
       
   101 
   101 
   102             pass();
   102             pass();
   103         } catch (Throwable t) {
   103         } catch (Throwable t) {
   104             unexpected(t);
   104             unexpected(t);
   105            // reset(atTheStartingGate);
   105            // reset(atTheStartingGate);
   269 
   269 
   270         //----------------------------------------------------------------
   270         //----------------------------------------------------------------
   271         // Phaser is terminated while threads are waiting
   271         // Phaser is terminated while threads are waiting
   272         //----------------------------------------------------------------
   272         //----------------------------------------------------------------
   273         try {
   273         try {
   274             Phaser phaser = new Phaser(3);
       
   275             Iterator<Awaiter> awaiters = awaiterIterator(phaser);
       
   276             for (int i = 0; i < 4; i++) {
   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();
   277                 Arriver a1 = awaiters.next(); a1.start();
   278                 Arriver a2 = awaiters.next(); a2.start();
   278                 Arriver a2 = awaiters.next(); a2.start();
   279                 toTheStartingGate();
   279                 toTheStartingGate();
   280                 while (phaser.getArrivedParties() < 2) Thread.yield();
   280                 while (phaser.getArrivedParties() < 2) Thread.yield();
       
   281                 equal(0, phaser.getPhase());
   281                 phaser.forceTermination();
   282                 phaser.forceTermination();
   282                 a1.join();
   283                 a1.join();
   283                 a2.join();
   284                 a2.join();
   284                 check(a1.phase == -1);
   285                 equal(0 + Integer.MIN_VALUE, a1.phase);
   285                 check(a2.phase == -1);
   286                 equal(0 + Integer.MIN_VALUE, a2.phase);
   286                 int arrivedParties = phaser.getArrivedParties();
   287                 int arrivedParties = phaser.getArrivedParties();
   287                 checkTerminated(phaser);
   288                 checkTerminated(phaser);
   288                 equal(phaser.getArrivedParties(), arrivedParties);
   289                 equal(phaser.getArrivedParties(), arrivedParties);
   289             }
   290             }
   290         } catch (Throwable t) { unexpected(t); }
   291         } catch (Throwable t) { unexpected(t); }