@@ -0,0 +1,1902 @@
+package org.luyue.examples.java.concurrent;%0A%0Aimport java.util.concurrent.BrokenBarrierException;%0Aimport java.util.concurrent.CyclicBarrier;%0A%0A/**%0A * %3Cpre%3E%0A * When a set of threads must wait at a certain execution point until all threads in the set have reached that point.%0A * 1). create a CyclicBarrier instance and specify the number of threads that will be waiting for%0A * 2). each thread reaches the barrier, have it call await() on that object. This will pause execution of the thread%0A *     until all of the other threads also called await()%0A * 3). once the specified number of threads has reached the barrier, await() will return and execution will resume%0A * %0A * %3C/pre%3E%0A * %0A * @author jlu%0A *%0A */%0Apublic class CyclicBarrierExample %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        int numOfThreads = 3;%0A        CyclicBarrier cBar = new CyclicBarrier(numOfThreads, new Runnable() %7B%0A%0A            @Override%0A            public void run() %7B%0A                // this line will be printed out once numOfThreads called await()%0A                System.out.println(%22yeah! Barrier reached! %22);%0A            %7D%0A        %7D);%0A%0A        for (int i = 0; i %3C numOfThreads; i++)%0A            new SayHiThread(%22Thread %22 + i, cBar);%0A    %7D%0A%7D%0A%0Aclass SayHiThread implements Runnable %7B%0A%0A    private final String name;%0A    private final CyclicBarrier cBar;%0A%0A    SayHiThread(String name, CyclicBarrier cBar) %7B%0A        this.name = name;%0A        this.cBar = cBar;%0A        new Thread(this).start();%0A    %7D%0A%0A    @Override%0A    public void run() %7B%0A        System.out.println(name + %22 enter run()...%22);%0A        try %7B%0A            // waiting for other threads to reach the barrier%0A            cBar.await();%0A        %7D catch (InterruptedException %7C BrokenBarrierException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        // resumes after all threads reach the barrier%0A        System.out.println(%22Hi from %5B%22 + name + %22%5D%22);%0A    %7D%0A%0A%7D%0A

add a CycliBarrierExample