In previous thread concurrency tutorial we read how to Phaser in java now we will learn how to use parentPhaser and childPhaser in java.
Example/ Program to demonstrate usage of parentPhaser and childPhaser in java >
Program shows that parentPhaser does not terminates until childPhaser is not terminated.
package PhaserParent;
import java.util.concurrent.Phaser;
public class PhaserParentChildTest {
public static void main(String[] args) {
/*
* Creates a new phaser with no registered unArrived parties.
*/
Phaser parentPhaser = new Phaser();
/*
* Creates a new phaser with the given parent &
* no registered unArrived parties.
*/
Phaser childPhaser = new Phaser(parentPhaser,0);
childPhaser.register();
System.out.println("parentPhaser isTerminated : "+parentPhaser.isTerminated());
System.out.println("childPhaser isTerminated : "+childPhaser.isTerminated());
childPhaser.arriveAndDeregister();
System.out.println("\n--childPhaser has called arriveAndDeregister()-- \n");
System.out.println("parentPhaser isTerminated : "+parentPhaser.isTerminated());
System.out.println("childPhaser isTerminated : "+childPhaser.isTerminated());
}
}
/* OUTPUT
parentPhaser isTerminated : false
childPhaser isTerminated : false
--childPhaser has called arriveAndDeregister()--
parentPhaser isTerminated : true
childPhaser isTerminated : true
*/
|
We created a parentPhaser with no registered unArrived parties. Than we created a childPhaser with the given parentPhaser & no registered unArrived parties.
Then registered childPhaser . Then parentPhaser didn’t terminated until childPhaser was not terminated.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
CyclicBarrier >
CyclicBarrier in java
Implementation of custom/own CyclicBarrier in java
Thread concurrency Interviews >
Labels:
Core Java
Thread Concurrency