Thursday, August 8, 2024

CyclicBarrier in Java Concurrency package...

 A CyclicBarrier is a synchronization tool in Java that allows a group of threads to wait for each other to reach a common point before proceeding. Once all threads reach this point (the barrier), they are released simultaneously. The term "cyclic" implies that the barrier can be reused multiple times.

Key Features of CyclicBarrier

  1. Barrier Point: The point where all participating threads must wait before any of them can proceed. Once the last thread reaches this barrier, the barrier is broken, and all threads continue execution.

  2. Cyclic Nature: The barrier is "cyclic" because it can be reused after the waiting threads are released. This allows the same CyclicBarrier instance to be used for multiple cycles of synchronization.

  3. Optional Runnable Action: You can specify a Runnable action that will be executed once the last thread reaches the barrier. This action is executed before the threads are released.

Source Code:


Class StudentTask

package com.somitsolutions.java.cyclicbarrier; import java.time.LocalTime; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class StudentTask implements Runnable { private String name = ""; private int timeLapseBeforeStarting = 0; private CyclicBarrier cyclicBarrier = null; public StudentTask(String name, int timeLapseBeforeStarting, CyclicBarrier cyclicBarrier) { this.name = name; this.timeLapseBeforeStarting = timeLapseBeforeStarting; this.cyclicBarrier = cyclicBarrier; } @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(timeLapseBeforeStarting); System.out.println("Thread " + this.name + " has reached the barrier and will wait"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { this.cyclicBarrier.await(); LocalTime localTime = LocalTime.now(); System.out.println(this.name + " is starting at " + localTime .getHour() + " : " + localTime.getMinute() +" : " + localTime.getSecond()); } catch (InterruptedException | BrokenBarrierException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }


Class classEight:


package com.somitsolutions.java.cyclicbarrier;


import java.util.concurrent.CyclicBarrier;


public class ClassEight {

static final int NUMBER_OF_STUDENTS = 3;

static final CyclicBarrier cb = new CyclicBarrier(NUMBER_OF_STUDENTS, ()->{

System.out.println("All threads have reached the barrier. All of them will now proceed...");

});

Thread ridit = new Thread(new StudentTask("Ridit", 1000, cb));

Thread ishan = new Thread(new StudentTask("Ishan", 2000, cb));

Thread manav = new Thread(new StudentTask("Manav", 3000, cb));

public void giveTaskToStudent() {

ridit.start();

ishan.start();

manav.start();

}


}



class Main :


package com.somitsolutions.java.cyclicbarrier;


public class Main {


public static void main(String[] args) {

// TODO Auto-generated method stub

ClassEight etc = new ClassEight();

etc.giveTaskToStudent();


}


}

The Output:

Thread Ridit has reached the barrier and will wait

Thread Ishan has reached the barrier and will wait

Thread Manav has reached the barrier and will wait

All threads have reached the barrier. All of them will now proceed

Ishan is starting at 20 : 21 : 31

Manav is starting at 20 : 21 : 31

Ridit is starting at 20 : 21 : 31


Here's my discussion on Barrier in C++ 20.


No comments: