// CyclicBarrier4.java multiple uses of threads with await() import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class CyclicBarrier4 { int matrix[][] = { { 1 }, { 2, 2 }, { 3, 3, 3 }, { 4, 4, 4, 4 }, { 5, 5, 5, 5, 5 } }; int results[]; CyclicBarrier barrier; CyclicBarrier4() { final int rows = matrix.length; results = new int[rows]; Merger my_merger = new Merger(rows); // public CyclicBarrier(int parties,Runnable barrierAction) // Creates a new CyclicBarrier that will trip when the given number // of parties (threads) are waiting upon it, and which will execute // the Merger task when the barrier is tripped, performed // by the last thread entering the barrier. barrier = new CyclicBarrier(rows, my_merger); for (int i = 0; i < rows; i++) { new Summer(i).start(); } System.out.println("CyclicBarrier4 constructor finished"); } // end constructor CyclicBarrier4 class Summer extends Thread { int row; Summer(int row) { this.row = row; } // end constructor summer public void run() { int columns = matrix[row].length; int sum = 0; for (int i = 0; i < columns; i++) { sum += matrix[row][i]; } results[row] = sum; System.out.println("Results for row " + row + " are : " + sum); // wait for others try { barrier.await(); // can do more processing sum = 0; for (int i=0; i