Thread synchronization class: CyclicBarrier

CyclicBarrier: Fences

Construction method:

// 一起执行的线程数
public CyclicBarrier(int parties)
// 一起执行的线程数,barrierAction优先开始执行的线程
public CyclicBarrier(int parties, Runnable barrierAction)

The main methods:
the await method: Let the thread into the barrier state, when the number of threads to reach the state into the barrier structure CyclicBarrier number of parties, which thread will perform together

scenes to be used

8 athletes to prepare in place, and then run with
the idea: set up a number of waiting for the CyclicBarrier 8, and then each thread calls await, allowed to enter the barrier state, when the barrier 8 reaches the state, will be executed with
the code:
thread class:

class JymCyclicBarrierThread implements Runnable{

    private CyclicBarrier cyclicBarrier;
    private String name;

    public JymCyclicBarrierThread(CyclicBarrier cyclicBarrier, String name) {
        this.cyclicBarrier = cyclicBarrier;
        this.name = name;
    }

    public void run() {
        System.out.println(name+"就位");
        try {
            cyclicBarrier.await();
            int time = new Random().nextInt(5);
            Thread.sleep(time*1000);
            System.out.println(name+"用时:"+time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

Thread Pool:

class JymExecutor{
    private CyclicBarrier cyclicBarrier = new CyclicBarrier(8);

    public void start(){
        List<JymCyclicBarrierThread> jymCyclicBarrierThreadList = new ArrayList<JymCyclicBarrierThread>();
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"亚索"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"盖伦"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"佐伊"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"薇恩"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"奎因"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"赵信"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"皇子"));
        jymCyclicBarrierThreadList.add(new JymCyclicBarrierThread(cyclicBarrier,"剑圣"));
        ExecutorService executor = Executors.newFixedThreadPool(8);
        for (JymCyclicBarrierThread jymCyclicBarrierThread : jymCyclicBarrierThreadList) {
            executor.submit(jymCyclicBarrierThread);
        }
        executor.shutdown();
    }
}

Test code:

    public static void main(String[] args) {
        JymExecutor jymExecutor = new JymExecutor();
        jymExecutor.start();

    }

result:
Here Insert Picture Description

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104121592