Showing posts with label CyclicBarrier. Show all posts
Showing posts with label CyclicBarrier. Show all posts

Tuesday, August 16, 2016

How to make use of CyclicBarrier

The CyclicBarrier allows you to run some tasks on the condition that certain prior tasks complete. Let's suppose a mixed tennis game which requires the presence of 4 players in order to start, here is how you might implement this in Java using the predefined CyclicBarrier class:

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

// The run() method in this thread should be called only when 
// four players are ready to start the game 
class MixedDoubleTennisGame extends Thread {
@Override
public void run() {
System.out.println("All four players ready, game starts \n Love all...");
}
}

// This thread simulates arrival of a player.
// Once a player arrives, he/she should wait for other players to arrive
class Player extends Thread {
CyclicBarrier waitPoint;

public Player(CyclicBarrier barrier, String name) {
this.setName(name);
waitPoint = barrier;
this.start();
}

@Override
public void run() {
System.out.println("Player " + getName() + " is ready ");
try {
waitPoint.await(); // await for all four players to arrive
} catch (BrokenBarrierException | InterruptedException exception) {
System.out.println("An exception occurred while waiting... " + exception);
}
}
}

// Creates a CyclicBarrier object by passing the number of threads and the
// thread to run
// when all the threads reach the barrier
class CyclicBarrierTest {
public static void main(String[] args) {
// a mixed-double tennis game requires four players;
// so wait for four players
// (i.e., four threads) to join to start the game
System.out.println("Reserving tennis court \n" + "As soon as four players arrive, game will start");
CyclicBarrier barrier = new CyclicBarrier(4, new MixedDoubleTennisGame());
new Player(barrier, "G I Joe");
new Player(barrier, "Dora");
new Player(barrier, "Tintin");
new Player(barrier, "Barbie");
}
}

Running this outputs:

Reserving tennis court 
As soon as four players arrive, game will start
Player Dora is ready 
Player G I Joe is ready 
Player Tintin is ready 
Player Barbie is ready 
All four players ready, game starts 
 Love all...