@@ -0,0 +1,1838 @@
+package learn_to_code.java_api.concurency.synchronization;%0A%0Aimport java.util.concurrent.BrokenBarrierException;%0Aimport java.util.concurrent.CyclicBarrier;%0A%0A/**%0A * An example of CyclicBarrier usage.%0A * First we create a cyclic barrier which will be waiting for 3 threads to get to the barrier.%0A * When each of this threads calls await(), the barrier will be broken and all 3 threads will execute%0A * their last line of code (Print out 'await finished!')%0A */%0Apublic class CyclicBarrierExample %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        int numOfThreads = 3;%0A        CyclicBarrier barrier = new CyclicBarrier(numOfThreads);%0A%0A        System.out.println(%22Threads are gathering on barrier%22);%0A%0A        Thread thread1 = new Thread(new BarrierAwaiter(barrier, 1000, %22Thread 1%22));%0A        Thread thread2 = new Thread(new BarrierAwaiter(barrier, 2000, %22Thread 2%22));%0A        Thread thread3 = new Thread(new BarrierAwaiter(barrier, 4000, %22Thread 3%22));%0A%0A        thread1.start();%0A        thread2.start();%0A        thread3.start();%0A    %7D%0A%7D%0A%0Aclass BarrierAwaiter implements Runnable %7B%0A%0A    private CyclicBarrier barrier;%0A    private int waitTime;%0A    private String threadName;%0A%0A    BarrierAwaiter(CyclicBarrier barrier, int waitTime, String threadName) %7B%0A        this.barrier = barrier;%0A        this.waitTime = waitTime;%0A        this.threadName = threadName;%0A    %7D%0A%0A    @Override%0A    public void run() %7B%0A        System.out.println(threadName + %22 started%22);%0A        try %7B%0A            Thread.sleep(waitTime);%0A            System.out.println(threadName + %22 awaiting barrier%22);%0A            barrier.await();%0A            System.out.println(threadName + %22 awaiting finished!%22);%0A%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D catch (BrokenBarrierException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%7D%0A

Add CyclicBarrier example