CountDownLatch是一个同步工具,它主要用线程执行之间的协做。CountDownLatch 的做用和 Thread.join() 方法相似,让一些线程阻塞直到另外一些线程完成一系列操做后才被唤醒。在直接建立线程的年代(Java 5.0 以前),咱们能够使用 Thread.join()。在线程池出现后,由于线程池中的线程不能直接被引用,因此就必须使用 CountDownLatch 了。html
CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞。其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞),当计数器的值变为0时,因await方法阻塞的线程会被唤醒,继续执行。java
实现原理:计数器的值由构造函数传入,并用它初始化AQS的state值。当线程调用await方法时会检查state的值是否为0,若是是就直接返回(即不会阻塞);若是不是,将表示该节点的线程入列,而后将自身阻塞。当其它线程调用countDown方法会将计数器减1,而后判断计数器的值是否为0,当它为0时,会唤醒队列中的第一个节点,因为CountDownLatch使用了AQS的共享模式,因此第一个节点被唤醒后又会唤醒第二个节点,以此类推,使得全部因await方法阻塞的线程都能被唤醒而继续执行。dom
从源代码和实现原理中能够看出一个CountDownLatch对象,只能使用一次,不能重复使用。ide
await方法源码函数
public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } public final void acquireSharedInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg); } protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; }
doAcquireSharedInterruptibly 主要实现线程的入列与阻塞。工具
countDown方法ui
public void countDown() { sync.releaseShared(1); } public final boolean releaseShared(int arg) { if (tryReleaseShared(arg)) { doReleaseShared(); return true; } return false; } protected boolean tryReleaseShared(int releases) { // 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; } }
doReleaseShared主要实现唤醒第一个节点,第一个节点有会唤醒第二个节点,……。this
package demo; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchDemo { private CountDownLatch cdl = new CountDownLatch(2); private Random rnd = new Random(); class FirstTask implements Runnable{ private String id; public FirstTask(String id){ this.id = id; } @Override public void run(){ System.out.println("Thread "+ id + " is start"); try { Thread.sleep(rnd.nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread "+ id + " is over"); cdl.countDown(); } } class SecondTask implements Runnable{ private String id; public SecondTask(String id){ this.id = id; } @Override public void run(){ try { cdl.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("----------Thread "+ id + " is start"); try { Thread.sleep(rnd.nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("----------Thread "+ id + " is over"); } } public static void main(String[] args){ ExecutorService es = Executors.newCachedThreadPool(); CountDownLatchDemo cdld = new CountDownLatchDemo(); es.submit(cdld.new SecondTask("c")); es.submit(cdld.new SecondTask("d")); es.submit(cdld.new FirstTask("a")); es.submit(cdld.new FirstTask("b")); es.shutdown(); } }
运行结果spa
Thread a is start线程
Thread b is start
Thread b is over
Thread a is over
----------Thread c is start
----------Thread d is start
----------Thread d is over
----------Thread c is over
[1] http://developer.51cto.com/art/201403/432095.htm