JAVA Multithread-Cyclic Barrier

Posted by les48 on Fri, 12 Jul 2019 23:21:39 +0200

CyclicBarrier

Cyclic Barrier: N threads wait for each other, and all threads have to wait before any one thread completes.
CountDownLatch is a thread waiting for one or N threads.
The principal of CountDownLatch is different, and the principal of CountDownLatch is a thread. It requires one or more threads to perform an action on their own. These threads do not wait for each other.
Cyclic Baarier principal body is N threads, that is to say, everyone waits for each other at a certain point. Only when everyone has reached this point can everyone do their own next thing.
Or use the import example to say:

public class CyclicBarrierTest {
    static CyclicBarrier  ct = new CyclicBarrier(3);

    public static void main(String[] args) throws InterruptedException {

         new Thread(new ImportExcel(), "t1").start(); 
         new Thread(new ImportExcel(), "t2").start();
         new Thread(new ImportExcel(), "t3").start();
         System.out.println("Import success...");


    }

    static class ImportExcel implements Runnable{
        @Override
        public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() +"Import success");
                        ct.await();
                        System.out.println(Thread.currentThread().getName() +"Logging");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }

        }

    }
}

One operation result:

Import success...
Successful introduction of t3
 Successful introduction of t1
 Successful introduction of t2
 t2 logging
 t3 log
 t1 log

As you can see, first of all, the main thread does not wait for other threads. Secondly, no matter how it works, t1,t2,t3 must be imported successfully before we can make our own log records. This is when everyone waits for each other to finish their respective tasks before they can continue to do something. Of course, it can also inform users of successful imports after successful imports:

public class CyclicBarrierTest {
    static CyclicBarrier  ct = new CyclicBarrier(3,new  Runnable() {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() +"Notify the user of successful import");

        }
    });

    public static void main(String[] args) throws InterruptedException {

         new Thread(new ImportExcel(), "t1").start(); 
         new Thread(new ImportExcel(), "t2").start();
         new Thread(new ImportExcel(), "t3").start();


    }

    static class ImportExcel implements Runnable{
        @Override
        public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() +"Import success");
                        ct.await();
                        System.out.println(Thread.currentThread().getName() +"Logging");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }

        }

    }
}

Notify the user only after the import is successful. Use another constructor, public Cyclic Barrier (int parties, Runnable barrier action). This constructor is to execute the run method in Runable after all threads have reached a certain point.