循環柵欄CyclicBarrier 原理

 用法我就不說了,百度一大堆,我在這裏帶大家看看源碼瞭解其原理:

CyclicBarrier barrier = new CyclicBarrier(3);

 public CyclicBarrier(int parties) {
        this(parties, null);
    }

 public CyclicBarrier(int parties, Runnable barrierAction) {
        if (parties <= 0) throw new IllegalArgumentException();
        this.parties = parties;
        this.count = parties;
        this.barrierCommand = barrierAction;
    }

這裏設置了一個變量 count  和 parties 一開始都是初始值。

接着瞭解其核心方法await()  

 public int await() throws InterruptedException, BrokenBarrierException {
        try {
            return dowait(false, 0L);
        } catch (TimeoutException toe) {
            throw new Error(toe); // cannot happen
        }
    }

 private int dowait(boolean timed, long nanos)
        throws InterruptedException, BrokenBarrierException,
               TimeoutException {
        final ReentrantLock lock = this.lock;   聲明瞭一個鎖,鎖裏面其實有個同步隊列。
        lock.lock();
        try {
            final Generation g = generation;

            if (g.broken)  //判斷線程是否被破壞,其實就是下面breakBarrier()方法會設置爲被中斷
                throw new BrokenBarrierException();

            if (Thread.interrupted()) {
                breakBarrier();//設置線程中斷
                throw new InterruptedException();
            }

            int index = --count;重頭戲在這裏,判斷之前聲明的count是否降到0,所有的線程都走到這裏的話就會減到0
            if (index == 0) {  // tripped
                boolean ranAction = false;  設置一個標識,
                try {
                    final Runnable command = barrierCommand;
                    if (command != null)
                        command.run();自己先跑
                    ranAction = true;
                    nextGeneration();喚醒其他線程跑,並且設置count 爲原來的值,就是又可以循環使用了
                    return 0;return 之前執行finally lock.unlock();方法釋放鎖。
        }
                } finally {
                    if (!ranAction)
                        breakBarrier();
                }
            }

            // loop until tripped, broken, interrupted, or timed out
            for (;;) {死循環自旋
                try {
                    if (!timed)
                        trip.await();超時就進入同步隊列等待
                    else if (nanos > 0L)
                        nanos = trip.awaitNanos(nanos);
                } catch (InterruptedException ie) {
                    if (g == generation && ! g.broken) {
                        breakBarrier();
                        throw ie;
                    } else {
                        // We're about to finish waiting even if we had not
                        // been interrupted, so this interrupt is deemed to
                        // "belong" to subsequent execution.
                        Thread.currentThread().interrupt();
                    }
                }

                if (g.broken)
                    throw new BrokenBarrierException();

                if (g != generation)
                    return index;

                if (timed && nanos <= 0L) {
                    breakBarrier();
                    throw new TimeoutException();
                }
            }
        } finally {
            lock.unlock();
        }
    }

大概這些邏輯,我們理解理清楚了,大概就是聲明瞭一個數字 count ,然後每次調用await()就會減1 並且判斷是否到0了,如果到了就自己先跑並且喚醒其他線程。並且重新設置count的值。從而實現可循環。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章