CyclicBarrier allows you to assign an ORDER stream:
Assigning a stream returning in SPECIFIC order is possible if, as you say, you enclose the logic for terminating the barrier in a conditional expression that is specific to the stream index. Thus, your implementation above will work in accordance with the documentation you specify.
However, the point of confusion here is that the documentation speaks of the identity of the flows in terms of the order of return to the barrier, and not in the identifier of the stream object. Thus, thread 0 refers to the 0th thread to complete.
Alternative: stream assignment using other mechanisms.
If you want a specific thread to perform a specific action after other work has been completed, you can use a different mechanism — for example, a semaphore. If you want this behavior, you may not need a cyclic barrier.
To check what is meant by the documentation, run the class (modified by http://programmingexamples.wikidot.com/cyclicbarrier ), where ive included your snippet.
An example of what is understood in the docs for CyclicBarrier
packet stream; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample { private static int matrix[][] = { { 1 }, { 2, 2 }, { 3, 3, 3 }, { 4, 4, 4, 4 }, { 5, 5, 5, 5, 5 } }; static final int rows = matrix.length; private static int results[]=new int[rows]; static int threadId=0; private static class Summer extends Thread { int row; CyclicBarrier barrier; Summer(CyclicBarrier barrier, int row) { this.barrier = barrier; this.row = row; } 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);