CyclicBarrier for java Concurrent Programming learning

Keywords: Java

Effect

Barrier interception, structural parameters can transmit the number of threads intercepted, and the Runnable class that is called after interception. Whenever a thread calls the await method, it tells the CyclicBarrier that it has reached the barrier, and then blocks there. When all the threads have reached the barrier, the thread begins to execute.

Main method

  1. await: tell CyclicBarrier that it has reached the barrier

Example

public class CyclicBarrierDemo {
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Thread3());
    static int num = 0;

    static class Thread1 implements Runnable {
        @Override
        public void run() {
            try {
                num += 1;
                Thread.sleep(5000);
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "-" + 1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    static class Thread2 implements Runnable {
        @Override
        public void run() {
            try {
                num += 1;
                Thread.sleep(1000);
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "-" + 2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    static class Thread3 implements Runnable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ":num=" + num);
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread1(), "thread1");
        Thread thread2 = new Thread(new Thread2(), "thread2");
        thread1.start();
        thread2.start();
        System.out.println(Thread.currentThread().getName() + ":" + 0);
    }
}

The operation results are as follows:

Thread 3 can't execute until thread 1 and thread 2 reach the barrier at the same time. At this time, the num taken is 2.
Thread 2 has been dormant for one second, and thread 1 has been dormant for five seconds, but thread 2 has not been executed first, so it is waiting for thread 1 to reach the barrier.

CountDownLatch and CyclicBarrier

  1. CountDownLatch can execute countDown multiple times in a thread, and the execution of CyclicBarrier multiple times is invalid.
  2. CountDownLatch is determined by the external, and CyclicBarrier is determined by multiple threads themselves.

For example, in class roll call, some teachers are very casual. As long as the number of students is full, they start class. No matter some students change their voice and shout, they remember that someone will come to class. This time is CountDownLatch. Some teachers are very serious. They have to see that all the people in their seats have arrived before they start their classes. This time is CyclicBarrier.

Posted by growler123 on Fri, 01 Nov 2019 06:24:35 -0700