package no.uio.ifi.cyclic; import java.util.ArrayList; import java.util.List; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; class Solver { final int N; final float[][] data; final CyclicBarrier barrier; class Worker implements Runnable { int myRow; float res = 0; Worker(int row) { myRow = row; } public void run() { int i = 0; while( i < N ) res += data[myRow][i++]; try { barrier.await(); } catch (InterruptedException | BrokenBarrierException ignored) { } } } public Solver(float[][] matrix) throws InterruptedException { data = matrix; N = matrix.length; barrier = new CyclicBarrier(N); List threads = new ArrayList<>(N); for (int i = 0; i < N; i++) { Thread thread = new Thread(new Worker(i)); threads.add(thread); thread.start(); } // wait until done for (Thread thread : threads) thread.join(); } }