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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值