CyclicBarrier in Java

CyclicBarrier literally means a recyclable barrier. All it does is block a group of threads when they reach a barrier, and the barrier will not open until the last thread reaches the barrier, and all threads blocked by the barrier will continue to run.

Like CountDownLatch, the CycliBarrier constructor accepts an int value representing the number of threads blocked by the barrier. Each thread calls the await method to tell CyclicBarrier that I have reached the barrier, and the current thread is blocked. The sample code is as follows

public static void main(String[] args) {
    CyclicBarrier c = new CyclicBarrier(2);
    Thread t1 = new Thread(){
        @Override
public void run() {        
            System.out.println("我到了");
            try {
                c.await();
            }catch (Exception e){
                e.printStackTrace () ;
            }
        }
    } ;
     t1.start() ;
 System.out     .println ( " I'm here " ) ​​;
     try {
        c.await();
    }catch (Exception e){
        e.printStackTrace () ;
    }
    System.out.println ( " Door is open " ) ;
 }
If we change the parameter to 3, the main thread and the child thread will wait forever, because without a third thread executing the await method, the barrier will never open.


In addition, CyclicBarrier also provides a more advanced constructor CyclicBarrier (int parties, Runnable barrier), which is used to execute the barrier first when all threads reach the barrier.

public class Test1 {
    public static void main(String[] args) {
        CyclicBarrier c = new CyclicBarrier(2,new Run());
Thread t1 = new Thread(){
            @Override
public void run() {
                try {
                    
                    System.out .println ( " I'm at A" ) ;
 c .await () ;
 System.out .println ( " Crossing the barrier, where am I at A" ) ;
 } catch (Exception e){                                                        
                    e.printStackTrace () ;
                }
            }
        } ;
         t1.start() ;
 System.out         .println ( " I'm at B" ) ;
         try {
            c.await() ;
 System.out             .println ( " Crossing the barrier, who am I? " ) ;
 } catch (Exception e){        
            e.printStackTrace () ;
        }
    }
}
class Run implements Runnable{

    @Override
public void run() {    
        System.out.println ( " I 'm the first to execute " ) ;
 }    
}

The execution result is as follows



You can see that across the barrier, the first execution is always the incoming Runnable, not A or B.

reset method

    Unlike the CountDownLatch class, which can only be used once, the CyclicBarrier can be used cyclically, using the reset method to reset the barrier to its initial state.

getNumberWaiting method

    The getNumberWaiting method can get the number of threads blocked by CyclicBarrier

finally:

    Difference between CountDownLatch and CycliBarrier

    1. The counter of CountDownLatch can only be used once, while the counter of CyclicBarrer can be reset using the reset method

    2. The function of CountDownLatch is to allow 1 or N threads to wait for other threads to complete execution; while CyclicBarrier allows N threads to wait for each other

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689943&siteId=291194637