publicvoidreset() {finalReentrantLock lock =this.lock;lock.lock();try {breakBarrier(); // break the current generationnextGeneration(); // start a new generation } finally {lock.unlock(); } }
publicclassCyclicBarrierUsageimplementsRunnable {privateCyclicBarrier cyclicBarrier;privateList<List<Integer>> partialResults;privateRandom random =newRandom();publicCyclicBarrierUsage(CyclicBarrier cyclicBarrier,List<List<Integer>> partialResults){this.cyclicBarrier=cyclicBarrier;this.partialResults=partialResults; } @Overridepublicvoidrun() {String thisThreadName =Thread.currentThread().getName();List<Integer> partialResult =newArrayList<>();// Crunch some numbers and store the partial resultfor (int i =0; i <10; i++) {Integer num =random.nextInt(10);System.out.println(thisThreadName+": Crunching some numbers! Final result - "+ num);partialResult.add(num); }partialResults.add(partialResult);try {System.out.println(thisThreadName+" waiting for others to reach barrier.");cyclicBarrier.await(); } catch (InterruptedException e) {// ... } catch (BrokenBarrierException e) {// ... } }}
CyclicBarrier cyclicBarrier=newCyclicBarrier(5,()->{String thisThreadName =Thread.currentThread().getName();System.out.println( thisThreadName +": Computing sum of 5 workers, having 10 results each.");int sum =0;for (List<Integer> threadResult : partialResults) {System.out.print("Adding ");for (Integer partialResult : threadResult) {System.out.print(partialResult+" "); sum += partialResult; }System.out.println(); }System.out.println(thisThreadName +": Final result = "+ sum); });
在CyclicBarrier中,我们定义了一个BarrierAction来做最后数据的汇总处理。
运行:
for (int i =0; i <5; i++) {Thread worker =newThread(new CyclicBarrierUsage(cyclicBarrier,partialResults));worker.setName("Thread "+ i);worker.start(); }
输出结果如下:
Spawning 5 worker threads to compute 10 partial results each
Thread 0: Crunching some numbers! Final result - 5
Thread 0: Crunching some numbers! Final result - 3
Thread 1: Crunching some numbers! Final result - 1
Thread 0: Crunching some numbers! Final result - 7
Thread 1: Crunching some numbers! Final result - 8
Thread 0: Crunching some numbers! Final result - 4
Thread 0: Crunching some numbers! Final result - 6
Thread 0: Crunching some numbers! Final result - 9
Thread 1: Crunching some numbers! Final result - 3
Thread 2: Crunching some numbers! Final result - 1
Thread 0: Crunching some numbers! Final result - 0
Thread 2: Crunching some numbers! Final result - 9
Thread 1: Crunching some numbers! Final result - 3
Thread 2: Crunching some numbers! Final result - 7
Thread 0: Crunching some numbers! Final result - 2
Thread 2: Crunching some numbers! Final result - 6
Thread 1: Crunching some numbers! Final result - 6
Thread 2: Crunching some numbers! Final result - 5
Thread 0: Crunching some numbers! Final result - 0
Thread 2: Crunching some numbers! Final result - 1
Thread 1: Crunching some numbers! Final result - 5
Thread 2: Crunching some numbers! Final result - 1
Thread 0: Crunching some numbers! Final result - 7
Thread 2: Crunching some numbers! Final result - 8
Thread 1: Crunching some numbers! Final result - 2
Thread 2: Crunching some numbers! Final result - 4
Thread 0 waiting for others to reach barrier.
Thread 2: Crunching some numbers! Final result - 0
Thread 2 waiting for others to reach barrier.
Thread 1: Crunching some numbers! Final result - 7
Thread 1: Crunching some numbers! Final result - 6
Thread 1: Crunching some numbers! Final result - 9
Thread 1 waiting for others to reach barrier.
Thread 3: Crunching some numbers! Final result - 9
Thread 3: Crunching some numbers! Final result - 3
Thread 3: Crunching some numbers! Final result - 8
Thread 3: Crunching some numbers! Final result - 8
Thread 3: Crunching some numbers! Final result - 1
Thread 3: Crunching some numbers! Final result - 8
Thread 3: Crunching some numbers! Final result - 0
Thread 3: Crunching some numbers! Final result - 5
Thread 3: Crunching some numbers! Final result - 9
Thread 3: Crunching some numbers! Final result - 1
Thread 3 waiting for others to reach barrier.
Thread 4: Crunching some numbers! Final result - 2
Thread 4: Crunching some numbers! Final result - 2
Thread 4: Crunching some numbers! Final result - 5
Thread 4: Crunching some numbers! Final result - 5
Thread 4: Crunching some numbers! Final result - 3
Thread 4: Crunching some numbers! Final result - 7
Thread 4: Crunching some numbers! Final result - 4
Thread 4: Crunching some numbers! Final result - 8
Thread 4: Crunching some numbers! Final result - 4
Thread 4: Crunching some numbers! Final result - 3
Thread 4 waiting for others to reach barrier.
Thread 4: Computing sum of 5 workers, having 10 results each.
Adding 5 3 7 4 6 9 0 2 0 7
Adding 1 9 7 6 5 1 1 8 4 0
Adding 1 8 3 3 6 5 2 7 6 9
Adding 9 3 8 8 1 8 0 5 9 1
Adding 2 2 5 5 3 7 4 8 4 3
Thread 4: Final result = 230
Process finished with exit code 0