CyclicBarrier学习

CyclicBarrier一个下辅助类,它允许一组线程互相等待,走到到达某个公共屏障点(common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时CyclicBarrier很有用。因为该barrier在释放等待线程后可以重用,所以称它为循环的barrier。

应用场景:当需要所有的子任务都执行完后,才执行主线程的任务时,这时就可以选择用CyclicBarrier。

第一个例子


package com.didispace;

import java.io.IOException;

import java.util.Random;

import java.util.concurrent.BrokenBarrierException;

import java.util.concurrent.CyclicBarrier;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

* @author: Kevin

* @官网: www.mimaxueyuan.com

* @Q Q群:      660567408

* @Email: [email protected]

* [每天进步一点点、人生带来大改变...]

* [本代码对应视频地址:http://study.163.com/course/introduction/1004176043.htm]

*/

public class CyclicBarrierTest1 {

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

        //如果将参数改为4,但是下面只加入了3个选手,这永远等待下去

        //Waits until all parties have invoked await on this barrier.

        CyclicBarrier barrier = new CyclicBarrier(3);

        ExecutorService executor = Executors.newFixedThreadPool(3);

        executor.submit(new Thread(new Runner(barrier, "1号选手")));

        executor.submit(new Thread(new Runner(barrier, "2号选手")));

        executor.submit(new Thread(new Runner(barrier, "3号选手")));

        executor.shutdown();

    }

}

class Runner implements Runnable {

    // 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)

    private CyclicBarrier barrier;

    private String name;

    public Runner(CyclicBarrier barrier, String name) {

        super();

        this.barrier = barrier;

        this.name = name;

    }

    @Override

    public void run() {

        try {

            Thread.sleep(1000 * (new Random()).nextInt(8));

            System.out.println(name + " 准备好了...");

            // barrier的await方法,在所有参与者都已经在此 barrier 上调用 await 方法之前,将一直等待。

            barrier.await();

            //设置等待时间,如果等待了1秒,最后一个线程还没有就位,则自己继续运行,但是会导致Barrier被标记为一个已经破坏的Barrier

            //barrier.await(1,TimeUnit.SECONDS);

        } catch (InterruptedException e) {

            System.out.println(name + " 中断异常!");

        } catch (BrokenBarrierException e) {

            System.out.println(name + " Barrier损坏异常!");

        }

        System.out.println(name + " 起跑!");

    }

}

第二个例子:


package com.didispace;

import java.io.IOException;

import java.util.concurrent.BrokenBarrierException;

import java.util.concurrent.CyclicBarrier;

/**

* @author: Kevin

* @官网: www.mimaxueyuan.com

* @Q Q群:      660567408

* @Email: [email protected]

* [每天进步一点点、人生带来大改变...]

* [本代码对应视频地址:http://study.163.com/course/introduction/1004176043.htm]

*/

public class CyclicBarrierTest2 {

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

        CyclicBarrier barrier = new CyclicBarrier(5);

        new Thread(new Worker(barrier, "worker1")).start();

        new Thread(new Worker(barrier, "worker2")).start();

        new Thread(new Worker(barrier, "worker3")).start();

        new Thread(new Worker(barrier, "worker4")).start();

        System.out.println("................");

        barrier.await();

        System.out.println("所有的线程都工作完毕了, main线程继续执行!");

    }

}

class Worker implements Runnable {

    // 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)

    private CyclicBarrier barrier;

    private String name;

    public Worker(CyclicBarrier barrier, String name) {

        this.barrier = barrier;

        this.name = name;

    }

    @Override

    public void run() {

        try {

            Thread.sleep(2000);

            System.out.println(name + "运行完毕!");

            barrier.await();

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (BrokenBarrierException e) {

            e.printStackTrace();

        }

    }

}

你可能感兴趣的:(CyclicBarrier学习)