ReentrantLock 重入锁,重入体会有多深?

本文深入探讨了Java并发工具类CyclicBarrier的工作原理,详细解析了其核心方法dowait的具体实现过程,包括线程间的等待与唤醒机制,并通过源码分析揭示了锁与条件变量在其中的作用。

以前写过这个 https://my.oschina.net/u/146130/blog/843445

今天看CyclicBarrier源码,核心代码 这一串

/**
     * Main barrier code, covering the various policies.
     */
    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;//到达的线程数目 conut-- 
           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();//等待lock上的条件    定义:private final Condition trip = lock.newCondition();
                    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();
        }
    }

一直疑惑,当一个线程获取lock.lock()锁时,什么时候释放的,要不然其他线程进来时,根本进入不了方法,怎么让int index = --count;,进而触发激活线程呢。糊涂很久,最后网上看到trip.await();解释为,等待lock上的条件(newCondition()),同时释放lock锁。这样其他线程才能获得锁,再进入dowait方法。其实,如果不用锁上的条件 (newCondition()) ,也就不好体会重入。

转载于:https://my.oschina.net/u/146130/blog/900516

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值