Java Reference
In-Depth Information
Listing 6-35. A Class That Demonstrates How to Use a CyclicBarrier in a Program
// MeetAtBarrier.java
package com.jdojo.threads;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.BrokenBarrierException;
public class MeetAtBarrier extends Thread {
private CyclicBarrier barrier;
private int ID;
private static Random random = new Random();
public MeetAtBarrier(int ID, CyclicBarrier barrier) {
this.ID = ID;
this.barrier = barrier;
}
public void run() {
try {
// Generate a random number between 1 and 30 to wait
int workTime = random.nextInt(30) + 1;
System.out.println("Thread #" + ID + " is going to work for " +
workTime + " seconds");
// Yes. Sleeping is working for this thread!!!
Thread.sleep(workTime * 1000);
System.out.println("Thread #" + ID + " is waiting at the barrier...");
// Wait at barrier for other threads in group to arrive
this.barrier.await();
System.out.println("Thread #" + ID + " passed the barrier...");
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (BrokenBarrierException e) {
System.out.println("Barrier is broken...");
}
}
public static void main(String[] args) {
// Create a barrier for a group of three threads with a barrier action
Runnable barrierAction
= () -> System.out.println("We are all together. It's party time...");
CyclicBarrier barrier = new CyclicBarrier(3, barrierAction);
Search WWH ::




Custom Search