CyclicBarrier源码浅析

目录

简述

成员变量

等待解除阻塞

重置

获取当前阻塞的线程个数


简述

阻塞多个线程,直到阻塞的线程个数达到一定条件,则唤醒所有阻塞的线程,并由唤醒的线程执行任务。

 

成员变量

    // 用于线程安全的锁
    private final ReentrantLock lock = new ReentrantLock();
    // 等待条件
    private final Condition trip = lock.newCondition();
    // 需要触发解除阻塞的线程总数
    private final int parties;
    // 解除阻塞时运行的任务
    private final Runnable barrierCommand;
    /** The current generation */
    private Generation generation = new Generation();
    // 用于记录当前剩余触发解除线程阻塞的个数
    private int count;

 

等待解除阻塞

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

    public int await(long timeout, TimeUnit unit)
        throws InterruptedException,
               BrokenBarrierException,
               TimeoutException {
        return dowait(true, unit.toNanos(timeout));
    }

 

dowait方法

    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();
            }

	    // 更新count
            int index = --count;
			
	    // 如果当前调用await方法的线程个数满足count,执行barrierCommand任务,并唤醒所有等待的线程
            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) {
                    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(); // 解锁
        }
    }

    private void nextGeneration() {
        // signal completion of last generation
	// 唤醒所有等待的线程
        trip.signalAll();
        // set up next generation
	// 重置count
        count = parties;
        generation = new Generation();
    }

 

重置

    public void reset() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            breakBarrier();   // break the current generation
            nextGeneration(); // start a new generation
        } finally {
            lock.unlock();
        }
    }

 

获取当前阻塞的线程个数

    public int getNumberWaiting() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return parties - count;
        } finally {
            lock.unlock();
        }
    }

 

你可能感兴趣的:(Java,多线程)