Java Multithreaded Part 5 - Cyclic Barrier

preview_player
Показать описание
How to spawn multiple threads and then wait at an execution point for all the threads using CyclicBarrier
Рекомендации по теме
Комментарии
Автор

Amazing and clear explaination! Please share video for countdownlatch. Also, please share videos for Algorithms if possible.

rachitshah
Автор

nice presentation. Can you please show a video where the same can be done without the help of java concurrency. This will help us where the CyclicBarrier as a advantage over traditional multithreading approach.

AshishMishra-jmjq
Автор

package

import
import

class Aggregator extends Thread{
public void run() {
System.out.println("In Aggregator");
}
}

class WorkerThread extends Thread{
CyclicBarrier synchPoint;

public WorkerThread(CyclicBarrier cyclicBarrier, String name){
this.setName(name);
this.synchPoint = cyclicBarrier;
this.start();
}

public void run() {
System.out.println("Entered: " + getName());

try {
synchPoint.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


public class CyclicBarrierThread {

public static void main(String[] args) {
System.out.println("Entered Main");

CyclicBarrier cyclicBarrier = new CyclicBarrier(4, new Aggregator());

new WorkerThread(cyclicBarrier, "2016");
snooze();
new WorkerThread(cyclicBarrier, "2015");
snooze();
new WorkerThread(cyclicBarrier, "2014");
snooze();
new WorkerThread(cyclicBarrier, "2013");

System.out.println("Ended Main");


}

private static void snooze() {

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

ghoshalacademy