@@ -0,0 +1,1954 @@
+package learn_to_code.java_api.concurency.synchronization;%0A%0Aimport java.util.concurrent.Phaser;%0A%0A/**%0A * An example of Phaser usage.%0A *%0A * We will create 2 threads each with 3 phases. This thread will only proceed with their work%0A * when the main thread calls arriveAdnAwaitAdvanced() method. This will happen 3 times. After that%0A * both threads and main thread will be deregistered.%0A */%0Apublic class PhaserExample %7B%0A    public static void main(String%5B%5D args) throws InterruptedException %7B%0A%0A        Phaser phaser = new Phaser(1);%0A%0A        Thread member1 = new Thread(new phaserMember(phaser, %22Thread_1%22));%0A        Thread member2 = new Thread(new phaserMember(phaser, %22Thread_2%22));%0A%0A        member1.start();%0A        member2.start();%0A%0A        System.out.println(%22Starting phase 1 from main thread%22);%0A        phaser.arriveAndAwaitAdvance();%0A        Thread.sleep(1000);%0A%0A        System.out.println(%22Starting phase 2 from main thread%22);%0A        phaser.arriveAndAwaitAdvance();%0A        Thread.sleep(1000);%0A%0A        System.out.println(%22Starting phase 3 from main thread%22);%0A        phaser.arriveAndAwaitAdvance();%0A        Thread.sleep(1000);%0A%0A        System.out.println(%22Deregistering phaser%22);%0A        phaser.arriveAndAwaitAdvance();%0A    %7D%0A%7D%0A%0Aclass phaserMember implements Runnable %7B%0A%0A    private Phaser phaser;%0A    private String name;%0A%0A    public phaserMember(Phaser phaser, String threadName) %7B%0A        this.phaser = phaser;%0A        this.name = threadName;%0A        this.phaser.register();%0A    %7D%0A%0A    @Override%0A    public void run() %7B%0A%0A        phaser.arriveAndAwaitAdvance();%0A        System.out.println(name + %22 arrived to first phase %22);%0A%0A        phaser.arriveAndAwaitAdvance();%0A        System.out.println(name + %22 arrived to second phase %22);%0A%0A        phaser.arriveAndAwaitAdvance();%0A        System.out.println(name + %22 arrived to third phase %22);%0A%0A        phaser.arriveAndDeregister();%0A        System.out.println(name + %22 arrived and deregistered %22);%0A    %7D%0A%7D%0A

Add Phaser example