CountDownLatch源码解析之countDown()

CountDownLatch 源码解析—— countDown()

上一篇文章从源码层面说了一下CountDownLatch 中 await() 的原理。这篇文章说一下countDown() 。

public void countDown() { //CountDownLatch
 sync.releaseShared(1);
}
 ↓
public final boolean releaseShared(int arg) { //AQS
 if (tryReleaseShared(arg)) {
  doReleaseShared();
  return true;
 }
 return false;
}
 ↓
protected boolean tryReleaseShared(int releases) { //CountDownLatch.Sync
 // Decrement count; signal when transition to zero
 for (;;) {
  int c = getState();
  if (c == 0)
   return false;
  int nextc = c-1;
  if (compareAndSetState(c, nextc))
   return nextc == 0;
 }
}

通过构造器 CountDownLatch end = new CountDownLatch(2);  state 被设置为2,所以c == 2,nextc = 2-1,

然后通过下面这个CAS操作将state设置为1。

protected final boolean compareAndSetState(int expect, int update) {
  // See below for intrinsics setup to support this
  return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
 }

此时nextc还不为0,返回false。一直等到countDown()  方法被调用两次,state == 0,nextc ==0,此时返回true。

进入doReleaseShared()方法。

doReleaseShared();
 ↓
private void doReleaseShared() {
 /*
  * Ensure that a release propagates, even if there are other
  * in-progress acquires/releases. This proceeds in the usual
  * way of trying to unparkSuccessor of head if it needs
  * signal. But if it does not, status is set to PROPAGATE to
  * ensure that upon release, propagation continues.
  * Additionally, we must loop in case a new node is added
  * while we are doing this. Also, unlike other uses of
  * unparkSuccessor, we need to know if CAS to reset status
  * fails, if so rechecking.
  */
 for (;;) {
  Node h = head;
  if (h != null && h != tail) {
   int ws = h.waitStatus;
   if (ws == Node.SIGNAL) {
    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
     continue;   // loop to recheck cases
    unparkSuccessor(h);
   }
   else if (ws == 0 &&
      !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
    continue;    // loop on failed CAS
  }
  if (h == head)     // loop if head changed
   break;
 }
}

回顾一下此时的等待队列模型。

  +--------------------------+ prev   +------------------+
head | waitStatus = Node.SIGNAL | <---- node(tail) | currentThread |
  +--------------------------+     +------------------+

此时head 不为null,也不为tail,waitStatus == Node.SIGNAL,所以进入 if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) 这个判断。

if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
 ↓
 /**
 * CAS waitStatus field of a node.
 */
private static final boolean compareAndSetWaitStatus(Node node,
              int expect,
              int update) {
 return unsafe.compareAndSwapInt(node, waitStatusOffset,
         expect, update);
}

这个CAS 操作将 state 设置为 0 ,也就是说此时Head 中的 waitStatus 是0.此时队列模型如下所示

  +----------------+ prev   +------------------+
head | waitStatus = 0 | <---- node(tail) | currentThread |
  +----------------+     +------------------+

该方法返回true。进入unparkSuccessor(h);

unparkSuccessor(h);
 ↓
private void unparkSuccessor(Node node) {
 /*
 * If status is negative (i.e., possibly needing signal) try
 * to clear in anticipation of signalling. It is OK if this
 * fails or if status is changed by waiting thread.
 */
 int ws = node.waitStatus;
 if (ws < 0)
  compareAndSetWaitStatus(node, ws, 0);

 /*
 * Thread to unpark is held in successor, which is normally
 * just the next node. But if cancelled or apparently null,
 * traverse backwards from tail to find the actual
 * non-cancelled successor.
 */
 Node s = node.next;
 if (s == null || s.waitStatus > 0) {
  s = null;
  for (Node t = tail; t != null && t != node; t = t.prev)
   if (t.waitStatus <= 0)
    s = t;
 }
 if (s != null)
  LockSupport.unpark(s.thread);
}

s 就是head的后继结点,也就是装有当前线程的结点。s != null ,并且s.waitStatus ==0 ,所以进入 LockSupport.unpark(s.thread);

 public static void unpark(Thread thread) {
  if (thread != null)
   UNSAFE.unpark(thread);
 }

也就是unlock 被阻塞的线程。裁判被允许吹哨了!

countDown() 的原理就此就非常清晰了。

每执行一次countDown() 方法,state 就是减1,直到state == 0,则开始释放被阻塞在队列中的线程,根据前驱结点中waitStatus的状态,释放后续结点中的线程。

OK,回到上一篇文章的问题,什么时候跳出下面这个循环(await方法中的循环)

for (;;) {
 final Node p = node.predecessor();
 if (p == head) {
  int r = tryAcquireShared(arg);
  if (r >= 0) {
   setHeadAndPropagate(node, r);
   p.next = null; // help GC
   failed = false;
   return;
  }
 }
 if (shouldParkAfterFailedAcquire(p, node) &&
  parkAndCheckInterrupt())
  throw new InterruptedException();
}

此时state == 0,所以进入 setHeadAndPropagate 方法。

setHeadAndPropagate(node, r);
 ↓
private void setHeadAndPropagate(Node node, int propagate) {
 Node h = head; // Record old head for check below
 setHead(node);
 /*
  * Try to signal next queued node if:
  * Propagation was indicated by caller,
  *  or was recorded (as h.waitStatus either before
  *  or after setHead) by a previous operation
  *  (note: this uses sign-check of waitStatus because
  *  PROPAGATE status may transition to SIGNAL.)
  * and
  * The next node is waiting in shared mode,
  *  or we don't know, because it appears null
  *
  * The conservatism in both of these checks may cause
  * unnecessary wake-ups, but only when there are multiple
  * racing acquires/releases, so most need signals now or soon
  * anyway.
  */
 if (propagate > 0 || h == null || h.waitStatus < 0 ||
  (h = head) == null || h.waitStatus < 0) {
  Node s = node.next;
  if (s == null || s.isShared())
   doReleaseShared();
 }
}
 ↓
private void setHead(Node node) {
 head = node;
 node.thread = null;
 node.prev = null;
}

这个方法将head 的后继结点变为head。该方法过后,又将node的next结点设置为null,模型变成下图

  prev    +---------+ next
null <---- node(tail/head) | null | ----> null
       +---------+

也就是node head tail 什么的都被置为null,等待GC回收了,这个时候return,跳出了for循环,队列被清空。

下面演示一下整个过程

setHeadAndPropagate(node, r);

   +----------------+
head(tail) | waitStatus=0 |
   | thread =null |
   +----------------+
     ↓
   +----------------+   +----------------+
   | waitStatus=0 | prev  | waitStatus=0 |
head(tail) | thread =null | <---- node | currentThread |
   +----------------+   +----------------+
     ↓
  +----------------+     +----------------+
  | waitStatus=0 | prev   | waitStatus=0 |
head | thread =null | <---- node(tail) | currentThread |
  +----------------+     +----------------+
     ↓
  +----------------+     +----------------+
  | Node.SIGNAL | prev   | waitStatus=0 |
head | thread =null | <---- node(tail) | currentThread |
  +----------------+     +----------------+
       ↓
  +----------------+     +----------------+
  | waitStatus=0 | prev   | waitStatus=0 |
head | thread =null | <---- node(tail) | currentThread |
  +----------------+     +----------------+
       ↓
       +----------------+
  prev    | waitStatus=0 | next
null <---- node(tail/head) | null   | ----> null
       +----------------+

CountDownLatch 的核心就是一个阻塞线程队列,这是由链表构造而成的队列,里面包含thread 和 waitStatus,其中waitStatus说明了后继结点线程状态。

state 是一个非常重要的标志,构造时,设置为对应的n值,如果n != 0,阻塞队列将一直阻塞,除非中断线程。

每次调用countDown()  方法,就是将state-1,而调用await() 方法就是将调用该方法的线程加入到阻塞队列,直到state==0,才能释放线程。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java线程并发countdownlatch类使用示例

    复制代码 代码如下: package com.yao; import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; /** * CountDownLatch是个计数器,它有一个初始数, * 等待这个计数器的线程必须等到计数器倒数到零时才可继续. */public class CountDownLatchTe

  • Android 列表倒计时的实现的示例代码(CountDownTimer)

    实习一段时间了,一直想写点技术总结,但一直没找到合适的主题.刚好,最近版本中我负责的模块遇到了个线程相关问题(之前一直画界面,做点基础功能,有点乏味),列表项倒计时的实现. 于是乎,我的第一篇android技术文章就诞生了. [醒目]该demo用Kotlin语言实现. 背景介绍 需要在ListView的item里实现倒计时,初看还挺简单的,但是真正做的时候也遇到了不少坑. 网上有不少类似文章,有用对TextView扩展实现的,也有用自带的CountDownTimer实现的,本文就是用CountD

  • 使用CountDownTimer类轻松实现倒计时功能

    CountDownTimer由系统提供 查资料的时候 发现了CountDownTimer这个类之后 果断抛弃了以前的倒计时做法 功能: 30秒倒计时 每次间隔1秒 参数: mc.start();方法开始 mc.cancel();方法结束 new MyCountDownTimer(30000, 1000); 第一个参数表示 总的时间为30000毫秒,间隔1000毫秒 直接上代码: package com.example.daojishi; import android.app.Activity;

  • Android基于CountDownTimer实现倒计时功能

    本文实例讲述了Android编程基于CountDownTimer实现倒计时功能的方法.分享给大家供大家参考,具体如下: 在逛论坛的时候,看到一个网友提问,说到了CountDownTimer这个类,从名字上面大家就可以看出来,记录下载时间.将后台线程的创建和Handler队列封装成一个方便的类调用. 查看了一下官方文档,这个类及其简单,只有四个方法,上面都涉及到了onTick,onFinsh.cancel和start.其中前面两个是抽象方法,所以要重写一下. 下面是官方给的一个小例子: new C

  • 详解Java多线程编程中CountDownLatch阻塞线程的方法

    直译过来就是倒计数(CountDown)门闩(Latch).倒计数不用说,门闩的意思顾名思义就是阻止前进.在这里就是指 CountDownLatch.await() 方法在倒计数为0之前会阻塞当前线程. CountDownLatch是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. CountDownLatch 的作用和 Thread.join() 方法类似,可用于一组线程和另外一组线程的协作.例如,主线程在做一项工作之前需要一系列的准备工作,只有这些准备工

  • Java多线程编程之CountDownLatch同步工具使用实例

    好像倒计时计数器,调用CountDownLatch对象的countDown方法就将计数器减1,当到达0时,所有等待者就开始执行. java.util.concurrent.CountDownLatch 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数初始化CountDownLatch.由于调用了countDown()方法,所以在当前计数到达零之前,await方法会一直受阻塞.之后,会释放所有等待的线程,await的所有后续调用都将立即返回.这种现

  • Java CountDownLatch完成异步回调实例详解

    Java CountDownLatch完成异步回调实例详解 实例代码: public class AsyncDemo { private static void doSomeTask() { System.out.println("Hello World"); } private static void onCompletion() { System.out.println("All tasks finished"); } public static void ma

  • Android使用CountDownTimer实现倒数定时器效果

    实现倒计时的效果 例子:发送验证码按钮 效果图: /** * 倒计时 * * @author admin * */ public class MainActivity extends ActionBarActivity { private Button tvTime;// 显示时间 private MyCountDownTimer myCountDownTimer;// 倒计时对象 @Override protected void onCreate(Bundle savedInstanceSta

  • Android基于CountDownView的时间控件扩展

    首先,新年的一年里祝大家,心想事成,鸡年大吉.去年的时候,我们做时间控件的时候一直遗留一个问题那就是正计时控件一直没有好的解决方案,我们很想把CountDownView既支持正计时又能支持倒计时.基于这个想法,便有了今天这篇文章,原理不在介绍,其实很简单,主要是我们知道怎么用,此控件的优点有: * 实现了正计时倒计时的统一 * 优化了Adapter,不再绑定控件Id * 一个属性实现正倒计时 * 不在为具体的时间属性设置别名 具体用法 1.xml文件 属性 app:isCountUp="fals

  • Android中CountDownTimer倒计时器用法实例

    本文实例讲述了Android中CountDownTimer倒计时器用法.分享给大家供大家参考,具体如下: 在平时我们编程的时候,经常会用到倒计时这个功能,很多人不知道Android已经帮封装好了一个类,往往都自己写.现在发现了这个类,大家共享一下: 在一个TextView不断显示剩下的时间,代码如下: private TextView vertifyView; private CountDownTimer timer = new CountDownTimer(10000, 1000) { @Ov

随机推荐