例示CyclicBarrier的使用

package player.kent.chen.learn.barrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class HelloBarrier {

    /**
     * 大家都到齐后,再一起开火
     */
    private static final class FireTogether implements Runnable {
        private CyclicBarrier barrier;

        public FireTogether(CyclicBarrier barrier) {
            super();
            this.barrier = barrier;
        }

        public void run() {
            try {
                barrier.await(); //等别的线程到达集合点
                System.out.println(Thread.currentThread().getName() + ": 哥开火了!");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (BrokenBarrierException e) {
                System.out.println(Thread.currentThread().getName() + ": 有人放鸽子。我不开火");
            }

        }

    }

    public static void main(String[] args) throws InterruptedException {
        int numOfThreads = 5;

        CyclicBarrier barrier = new CyclicBarrier(numOfThreads, new Runnable() {
            public void run() {
                System.out.println("大家都到齐了"); //到齐时打印一条信息
            }
        });

        Thread[] threads = new Thread[numOfThreads];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new FireTogether(barrier));
            threads[i].setName("fire-thread-" + i);
        }

        for (Thread thread : threads) {
            thread.start();
            System.out.println(thread.getName() + " 已经动身了 ");
            Thread.sleep(1000l); //动身时间间隔一秒
        }
    }

}

输出:

引用

fire-thread-0 已经动身了

fire-thread-1 已经动身了

fire-thread-2 已经动身了

fire-thread-3 已经动身了

fire-thread-4 已经动身了

大家都到齐了

fire-thread-4: 哥开火了!

fire-thread-0: 哥开火了!

fire-thread-2: 哥开火了!

fire-thread-1: 哥开火了!

fire-thread-3: 哥开火了!

如果 Thread[] threads = new Thread[numOfThreads] 改成   Thread[] threads = new Thread[2 * numOfThreads] ,也就是说barrier仍然等5个线程就发,但现在共有10个线程,执行结果是:

引用

fire-thread-0 已经动身了

fire-thread-1 已经动身了

fire-thread-2 已经动身了

fire-thread-3 已经动身了

fire-thread-4 已经动身了

大家都到齐了

fire-thread-4: 哥开火了!

fire-thread-0: 哥开火了!

fire-thread-2: 哥开火了!

fire-thread-1: 哥开火了!

fire-thread-3: 哥开火了!

fire-thread-5 已经动身了

fire-thread-6 已经动身了

fire-thread-7 已经动身了

fire-thread-8 已经动身了

fire-thread-9 已经动身了

大家都到齐了

fire-thread-9: 哥开火了!

fire-thread-5: 哥开火了!

fire-thread-6: 哥开火了!

fire-thread-7: 哥开火了!

fire-thread-8: 哥开火了!

可见barrier一共发了两批,也就是说barrier得到了重用,这就是’CyclicBarrier’类名中有"cyclic"的原因。

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.