|
| 1 | +package com.baeldung.concurrent.cyclicbarrier; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Random; |
| 7 | +import java.util.concurrent.BrokenBarrierException; |
| 8 | +import java.util.concurrent.CyclicBarrier; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by cv on 24/6/17. |
| 12 | + */ |
| 13 | +public class CyclicBarrierDemo { |
| 14 | + |
| 15 | + private CyclicBarrier cyclicBarrier; |
| 16 | + private List<List<Integer>> partialResults = Collections.synchronizedList(new ArrayList<>()); |
| 17 | + private Random random = new Random(); |
| 18 | + private int NUM_PARTIAL_RESULTS; |
| 19 | + private int NUM_WORKERS; |
| 20 | + |
| 21 | + |
| 22 | + public void runSimulation(int numWorkers, int numberOfPartialResults) { |
| 23 | + NUM_PARTIAL_RESULTS = numberOfPartialResults; |
| 24 | + NUM_WORKERS = numWorkers; |
| 25 | + |
| 26 | + cyclicBarrier = new CyclicBarrier(NUM_WORKERS, new AggregatorThread()); |
| 27 | + System.out.println("Spawning " + NUM_WORKERS + " worker threads to compute " |
| 28 | + + NUM_PARTIAL_RESULTS + " partial results each"); |
| 29 | + for (int i = 0; i < NUM_WORKERS; i++) { |
| 30 | + Thread worker = new Thread(new NumberCruncherThread()); |
| 31 | + worker.setName("Thread " + i); |
| 32 | + worker.start(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + class NumberCruncherThread implements Runnable { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + String thisThreadName = Thread.currentThread().getName(); |
| 41 | + List<Integer> partialResult = new ArrayList<>(); |
| 42 | + for (int i = 0; i < NUM_PARTIAL_RESULTS; i++) { |
| 43 | + Integer num = random.nextInt(10); |
| 44 | + System.out.println(thisThreadName |
| 45 | + + ": Crunching some numbers! Final result - " + num); |
| 46 | + partialResult.add(num); |
| 47 | + } |
| 48 | + partialResults.add(partialResult); |
| 49 | + try { |
| 50 | + System.out.println(thisThreadName + " waiting for others to reach barrier."); |
| 51 | + cyclicBarrier.await(); |
| 52 | + } catch (InterruptedException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } catch (BrokenBarrierException e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + class AggregatorThread implements Runnable { |
| 61 | + |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + String thisThreadName = Thread.currentThread().getName(); |
| 65 | + System.out.println(thisThreadName + ": Computing final sum of " + NUM_WORKERS |
| 66 | + + " workers, having " + NUM_PARTIAL_RESULTS + " results each."); |
| 67 | + int sum = 0; |
| 68 | + for (List<Integer> threadResult : partialResults) { |
| 69 | + System.out.print("Adding "); |
| 70 | + for (Integer partialResult : threadResult) { |
| 71 | + System.out.print(partialResult+" "); |
| 72 | + sum += partialResult; |
| 73 | + } |
| 74 | + System.out.println(); |
| 75 | + } |
| 76 | + System.out.println(Thread.currentThread().getName() + ": Final result = " + sum); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + CyclicBarrierDemo play = new CyclicBarrierDemo(); |
| 83 | + play.runSimulation(5, 3); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments