循環柵欄:CyclicBarrier(司令要求任務) 讀書筆記


可以理解為循環柵欄,柵欄就是一種障礙物.假如我們將計數器設置為10,那么湊齊第一批10個線程后,計數器就會歸零,然后接着湊齊下一批10個線程,這就是循環柵欄的含義.
構造器:
public CyclicBarrier(int parties, Runnable barrierAction)
parties:計數總數,也就是參與的線程總數. barrierAction 當計數器一次完成計數后,系統會執行的動作
 
下面代碼展示了 司令要求10個士兵去完成任務,先集合10個然后去一起完成任務,等全部完成后 司令才會宣布任務完成!
 
public class CyclicBarrierDemo {
    public static class Soldier implements Runnable {
        private String soldier;
        private final CyclicBarrier cyclic;

        public Soldier(CyclicBarrier cyclic, String soldier) {
            this.soldier = soldier;
            this.cyclic = cyclic;
        }

        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        @Override
        public void run() {
            try {
                //等待所有士兵到齊
                cyclic.await();
                doWork();
                //等待所有士兵完成工作
                cyclic.await();
            } catch (InterruptedException e) {//在等待過程中,線程被中斷
                e.printStackTrace();
            } catch (BrokenBarrierException e) {//表示當前CyclicBarrier已經損壞.系統無法等到所有線程到齊了.
                e.printStackTrace();
            }
        }

        void doWork() {
            try {
                Thread.sleep(Math.abs(new Random().nextInt() % 10000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(soldier + ":任務完成");
        }

    }

    public static class BarrierRun implements Runnable {
        boolean flag;
        int N;

        public BarrierRun(boolean flag, int N) {
            this.flag = flag;
            this.N = N;
        }

        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        @Override
        public void run() {
            if (flag) {
                System.out.println("司令:[士兵" + N + "個,任務完成!]");
            } else {
                System.out.println("司令:[士兵" + N + "個,集合完畢!]");
                flag = true;
            }
        }
    }

    public static void main(String[] args) {
        final int N = 10;
        Thread[] allSoldier = new Thread[N];
        boolean flag = false;
        CyclicBarrier cyclic = new CyclicBarrier(N, new BarrierRun(flag, N));
        //設置屏障點,主要為了執行這個方法
        System.out.println("集合隊伍! ");
        for (int i = 0; i < N; i++) {
            System.out.println("士兵" + i + "報道! ");
            allSoldier[i] = new Thread(new Soldier(cyclic, "士兵" + i));
            allSoldier[i].start();
        }
    }
}
 
結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM