Thursday, October 5, 2017

CyclicBarrier Real time Usage


In continuation to what we've learnt in CyclicBarrier , find below code snippet explaining real time scenario which is a perfect analogy.

package com.concurrent.jdk5;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/* 4 bikers starting from Manali and reaching to Leh after
 * crossing 3 checkpoints */
public class CyclicBarrierDemo {

     public static void main(String[] args) {
          final CyclicBarrier checkPoint = new CyclicBarrier(4, new BarrierAction());
    
          Thread t1 = new Thread(new Biker(checkPoint), "Biker 1");
          Thread t2 = new Thread(new Biker(checkPoint), "Biker 2");
          Thread t3 = new Thread(new Biker(checkPoint), "Biker 3");
          Thread t4 = new Thread(new Biker(checkPoint), "Biker 4");
          t1.start();
          t2.start();
          t3.start();
          t4.start();
     }
}

/* Worker thread*/
class Biker implements Runnable {

     private CyclicBarrier checkPoint;

     public Biker(CyclicBarrier checkPoint) {
          this.checkPoint = checkPoint;
         
     }

    
     public void run() {
          try {
              /* All bikers called await() and waiting to end the barrier. Once number of threads
               * crossing the barrier is equal to number mentioned in above construtor, then
               * last thread entering will call the barrier action which has seperate implementation*/
             
              System.out.println(Thread.currentThread().getName() + 
              " has left Manali");
              checkPoint.await();

              System.out.println(Thread.currentThread().getName() + 
              " has left the first checkpoint / barrier");

              /* Again, all bikers called await() and waiting to end the barrier. */
              checkPoint.await();
              System.out.println(Thread.currentThread().getName() + 
              " has left the second checkpoint / barrier");

              checkPoint.await();
              System.out.println(Thread.currentThread().getName() + 
           " has reached Leh");

          } catch (InterruptedException | BrokenBarrierException e) {
              e.printStackTrace();
          }
     }
}

/* barrierAction-the action to execute when all threads crossed the barrier number */
class BarrierAction implements Runnable {

     public void run() {
          System.out.println("Barrier Action run by Thread :"+Thread.currentThread().getName());
          System.out.println("\nAll bikers have arrived to checkpoint. Lets refill the petrol\n");
     }
}


Output

Biker 1 has left Manali
Biker 4 has left Manali
Biker 3 has left Manali
Biker 2 has left Manali
Barrier Action run by Thread :Biker 2

All bikers have arrived to checkpoint. Lets refill the petrol

Biker 2 has left the first checkpoint / barrier
Biker 3 has left the first checkpoint / barrier
Biker 1 has left the first checkpoint / barrier
Biker 4 has left the first checkpoint / barrier
Barrier Action run by Thread :Biker 4

All bikers have arrived to checkpoint. Lets refill the petrol

Biker 4 has left the second checkpoint / barrier
Biker 1 has left the second checkpoint / barrier
Biker 2 has left the second checkpoint / barrier
Biker 3 has left the second checkpoint / barrier
Barrier Action run by Thread :Biker 3

All bikers have arrived to checkpoint. Lets refill the petrol

Biker 3 has reached Leh
Biker 1 has reached Leh
Biker 2 has reached Leh
Biker 4 has reached Leh

References : http://codepumpkin.com/cyclicbarrier-java-concurrency-utilities/

No comments:

Post a Comment