CountDownLatch源码解析之await()
CountDownLatch源码解析之await() CountDownLatch是Java并发编程中常用的同步工具,通过await()方法可以让当前线程处于阻塞状态,直到锁存器计数为零(或者线程中断)。下面我们将详细解析CountDownLatch源码之await()方法的原理。 我们来看await()方法的源码: public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } 可以看到,await()方法调用了sync的acquireSharedInterruptibly()方法,其中sync是CountDownLatch的内部类,继承了AbstractQueuedSynchronizer。 AbstractQueuedSynchronizer是一个非常重要的类,在Java线程中提供了一个框架来实现阻塞锁,以及依赖FIFO等待队列的相关同步器(比如信号、事件等)。 继续看acquireSharedInterruptibly()方法的源码: public final void acquireSharedInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg); } 这个方法首先判断线程是否中断,然后再进行下一个判断。我们主要看看第二个判断,tryAcquireShared()方法。 tryAcquireShared()方法是在Sync类中实现的,返回值是-state的当前状态是否能够被允许获取锁。我们可以看到Sync类中实现的tryAcquireShared()方法是通过判断state是否为0来返回对应的int值的。 protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; } 那么state是什么?state是表示同步的状态,使用volatile关键字修饰,保证了state的可见性。 走到这里state是多少呢?我们需要看一看CountDownLatch的构造函数。CountDownLatch的构造函数中,传入的数字就是用来set state的。 CountDownLatch end = new CountDownLatch(2); public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } Sync(int count) { setState(count); } 所以,我们这里state == 2了。tryAcquireShared()方法返回-1,进入到下面的doAcquireSharedInterruptibly(arg)方法。 private void doAcquireSharedInterruptibly(int arg) throws InterruptedException { final Node node = addWaiter(Node.SHARED); boolean failed = true; try { for (;;) { final Node p = node.predecessor(); if (p == head) { int h = state; if (h <= 0) { setHead(node); p.next = null; // help GC failed = false; return; } if (compareAndSetHead(p, node)) { break; } } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) throw new InterruptedException(); } } catch (Throwable t) { cancelAcquire(node); throw t; } } 这个方法主要是实现线程的阻塞和唤醒机制,使用了Node类来实现线程的等待队列。 CountDownLatch的await()方法通过sync的acquireSharedInterruptibly()方法实现了线程的阻塞和唤醒机制,直到锁存器计数为零(或者线程中断)。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/12761659/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 2
- 资源: 936
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)