循環柵欄: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();
        }
    }
}
 
結果:
相關文章
相關標籤/搜索