cyclicBarrier源码分析

141 阅读1分钟

重要属性

    //锁
    private final ReentrantLock lock = new ReentrantLock();
    //condition
    private final Condition trip = lock.newCondition();
    
     /** The number of parties */
    private final int parties;
    /* The command to run when tripped */
    private final Runnable barrierCommand;
    /** The current generation */
    private Generation generation = new Generation();
    
    //需要等待的数量
    private int count;

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)
                throw new BrokenBarrierException();

            //响应中断
            if (Thread.interrupted()) {
                //
                breakBarrier();
                throw new InterruptedException();
            }
        
            int index = --count;
            if (index == 0) {  // tripped
                boolean ranAction = false;
                try {
                    //执行传入的方法
                    final Runnable command = barrierCommand;
                    if (command != null)
                        command.run();
                    ranAction = true;
                    nextGeneration();
                    return 0;
                } 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) {
                    //如果等待时有线程中断了的话并且,generation没有重置
                    if (g == generation && ! g.broken) {
                        //唤醒其他线程,并更新g.broken为true
                        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();
                    }
                }

                //如果有一个线程被中断了或者超时,其他线程会抛出BrokenBarrierException
                if (g.broken)
                    throw new BrokenBarrierException();

                if (g != generation)
                    return index;

                //如果超时,执行breakBarrier,本线程抛出TimeoutException,其他线程抛出BrokenBarrierException
                if (timed && nanos <= 0L) {
                    breakBarrier();
                    throw new TimeoutException();
                }
            }
        } finally {
            lock.unlock();
        }
    }