CountDownLatch使用场景

阅读更多
Java 理论与实践: 正确使用 Volatile 变量: http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
聊聊并发(一)——深入分析Volatile的实现原理: http://www.infoq.com/cn/articles/ftf-java-volatile
深入理解Java内存模型(四)——volatile: http://www.infoq.com/cn/articles/java-memory-model-4/
Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before):
http://www.cnblogs.com/mengheng/p/3495379.html

CountDownLatch同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
团队成员跑完,方可计算总成绩。使用情况两种:第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完;第二种,所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务方可执行主线程任务。

测试有统一开始信号的情况:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class RunnerGames {
	public static void main(String[] args) {
		   CountDownLatch startSignal = new CountDownLatch(1);
		   CountDownLatch doneSignal = new CountDownLatch(3);
		   ExecutorService  exec = Executors.newCachedThreadPool();
		   RunnableMan rm1 = new RunnableMan(startSignal,doneSignal, 1000);
		   RunnableMan rm2 = new RunnableMan(startSignal,doneSignal, 2000);
		   RunnableMan rm3 = new RunnableMan(startSignal,doneSignal, 3000);
		   Future score1 = exec.submit(rm1);
		   Future score2 = exec.submit(rm2);
		   Future score3 = exec.submit(rm3);
		   System.out.println("开始赛跑......");
		   startSignal.countDown();
		   try {
			   doneSignal.await();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}  
		   int sumScores =0;
		   try {
			   try {
				sumScores  = score1.get()+score2.get()+score3.get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		   System.out.println("团队赛跑结束,最后成绩为:"+sumScores);
		   exec.shutdown();
		 }
}


package juc;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

class RunnableMan implements Callable {  
	  private final CountDownLatch startSignal;
	  private final CountDownLatch doneSignal;             
	  private final int i;                                 
	  RunnableMan(CountDownLatch startSignal,CountDownLatch doneSignal, int i) {   
		 this.startSignal = startSignal;
	     this.doneSignal = doneSignal;                     
	     this.i = i;                                       
	  }                                                    
	  public Integer call() {        
	     try {
	    	startSignal.await();
			doRun(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}                                      
	     doneSignal.countDown();
	     return new Integer(i);
	  }                                                    
	  void doRun(int i) throws InterruptedException { 
		  System.out.println("选手"+i/1000+"正在赛跑中........");
		  Thread.sleep(i*2);
	  }                             
}
                                                     

测试结果:
开始赛跑......
选手3正在赛跑中........
选手2正在赛跑中........
选手1正在赛跑中........
团队赛跑结束,最后成绩为:6000


测试第二种情况,没有开始信号
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**AtomicInteger,一个提供原子操作的Integer的类。
在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。*/
public class ShopTickets {
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	public static void main(String[] args) throws InterruptedException {
		  CountDownLatch doneSignal = new CountDownLatch(3);
		   Executor exec = Executors.newCachedThreadPool();
		   exec.execute(new TicketSales(doneSignal,"售票员1",tickets));
		   exec.execute(new TicketSales(doneSignal,"售票员2",tickets));
		   exec.execute(new TicketSales(doneSignal,"售票员3",tickets));
		   doneSignal.await();  
		   System.out.println("票已售完,所有售票员,停止售票");
		 }
}


package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales implements Runnable {  
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private String saleName;
	  
	  TicketSales(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		   try {
				   while(tickets.get()>0){
					System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
					Thread.sleep(1000);
			   }
				doneSignal.countDown();
		  }catch (InterruptedException e) {
			e.printStackTrace();
		}
	  }                              
}                             
 
                     
测试结果:                                                                                                                                      
售票员1卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:9张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
售票员2卖完一张票,还有:0张
票已售完,所有售票员,停止售票
有时测试结果为:

售票员1卖完一张票,还有:9张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员3卖完一张票,还有:4张
售票员3卖完一张票,还有:2张
售票员2卖完一张票,还有:1张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:0张
售票员2卖完一张票,还有:-1张
票已售完,所有售票员,停止售票

把Thread.sleep(1000)这句去掉,票数为负数的概率较小,但还是会出现,
我测试了3分钟,出现2次。
关注这一句:
while(tickets.get()>0){
问题可能在tickets.get()的时候,票数不为零,在卖票的时候,其他线程已经在其基础上,修改
tickets,而当前线程tickets.get()已经做过判断,不为零,导致的错误。
再次修改:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class ShopTickets2 {
	/**
	 * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
	 * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。
	 */
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch doneSignal = new CountDownLatch(3);
		Executor exec = Executors.newCachedThreadPool();
		exec.execute(new TicketSales2(doneSignal, "售票员1", tickets));
		exec.execute(new TicketSales2(doneSignal, "售票员2", tickets));
		exec.execute(new TicketSales2(doneSignal, "售票员3", tickets));
		doneSignal.await();
		System.out.println("票已售完,所有售票员,停止售票");
	}
}


package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales2 implements Runnable { 
	  private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private String saleName;
	  
	  TicketSales2(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		 while(isDoned.get()){
				System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
				if(tickets.get()==0){
					isDoned.compareAndSet(true, false);
				}
            }
		doneSignal.countDown();
	  }                              
}     
                                              
                                                    
测试:
售票员1卖完一张票,还有:9张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员2卖完一张票,还有:0张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
票已售完,所有售票员,停止售票
不在出现上述票数为负的情况,关键是添加了所有线程都可见,且线程安全的余票状态
private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);


下面来看使用同步锁的场景:
package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class ShopTickets3 {
	/**
	 * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,
	 * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。
	 */
	private static volatile AtomicInteger tickets = new AtomicInteger(10);
	private static volatile Object saleLock = new Object();
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch doneSignal = new CountDownLatch(3);
		Executor exec = Executors.newCachedThreadPool();
		exec.execute(new TicketSales3(doneSignal, "售票员1", tickets,saleLock));
		exec.execute(new TicketSales3(doneSignal, "售票员2", tickets,saleLock));
		exec.execute(new TicketSales3(doneSignal, "售票员3", tickets,saleLock));
		doneSignal.await();
		System.out.println("票已售完,所有售票员,停止售票");
	}
}

package juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

class TicketSales3 implements Runnable {  
	  private final CountDownLatch doneSignal;   
	  private final AtomicInteger tickets;
	  private final Object saleLock;
	  private String saleName;
	  
	  TicketSales3(CountDownLatch doneSignal, String saleName,AtomicInteger tickets, Object saleLock) { 
	     this.doneSignal = doneSignal;                   
	     this.saleName = saleName; 
	     this.tickets = tickets;
	     this.saleLock = saleLock;
	  }         
	  public  void run() {                                
		  doSales(tickets);                                    
	  }                                                  
	  public  void  doSales(AtomicInteger tickets) { 
		 boolean flag = true;
		 while(flag){
			 synchronized(saleLock){
				   if(tickets.get()>0){
					   System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张");
					   saleLock.notifyAll();
				   }
				   else{
					   flag= false;
				   }
			   }
            }
		doneSignal.countDown();
	  }                              
}                                                    
                                  
控制台输出:
售票员1卖完一张票,还有:9张
售票员1卖完一张票,还有:8张
售票员1卖完一张票,还有:7张
售票员1卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员1卖完一张票,还有:4张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:2张
售票员3卖完一张票,还有:1张
售票员3卖完一张票,还有:0张
票已售完,所有售票员,停止售票
虽然也可以控制卖票,但只有两个售票员在售票,不能有效的利用资源,达到最大性能。
从上面可以看出,最有效方式为第二种。
/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

package java.util.concurrent;
import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.*;

/**
 * A synchronization aid that allows one or more threads to wait until
 * a set of operations being performed in other threads completes.
 * 同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
 *比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
 *主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
 *再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
 *团队成员跑完,方可计算总成绩。
 * 

A {@code CountDownLatch} is initialized with a given [i]count[/i]. * The {@link #await await} methods block until the current count reaches * zero due to invocations of the {@link #countDown} method, after which * all waiting threads are released and any subsequent invocations of * {@link #await await} return immediately. This is a one-shot phenomenon * -- the count cannot be reset. If you need a version that resets the * count, consider using a {@link CyclicBarrier}. *CountDownLatch有一个初始值,调用await的线程,要等待CountDownLatch值为0时,方可 *往下执行 *

A {@code CountDownLatch} is a versatile synchronization tool * and can be used for a number of purposes. A * {@code CountDownLatch} initialized with a count of one serves as a * simple on/off latch, or gate: all threads invoking {@link #await await} * wait at the gate until it is opened by a thread invoking {@link * #countDown}. A {@code CountDownLatch} initialized to [i]N[/i] * can be used to make one thread wait until [i]N[/i] threads have * completed some action, or some action has been completed N times. * *

A useful property of a {@code CountDownLatch} is that it * doesn't require that threads calling {@code countDown} wait for * the count to reach zero before proceeding, it simply prevents any * thread from proceeding past an {@link #await await} until all * threads could pass. * *

Sample usage: Here is a pair of classes in which a group * of worker threads use two countdown latches: * [list] *

  • The first is a start signal that prevents any worker from proceeding * until the driver is ready for them to proceed; *
  • The second is a completion signal that allows the driver to wait * until all workers have completed. * [/list] *第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完 *方可执行主线程任务 *
     * class Driver { // ...
     *   void main() throws InterruptedException {
     *     CountDownLatch startSignal = new CountDownLatch(1);
     *     CountDownLatch doneSignal = new CountDownLatch(N);
     *
     *     for (int i = 0; i < N; ++i) // create and start threads
     *       new Thread(new Worker(startSignal, doneSignal)).start();
     *
     *     doSomethingElse();  // don't let run yet
           //给所有线程一个开始信号,比如上面的团队赛发令枪
     *     startSignal.countDown();      // let all threads proceed
     *     doSomethingElse();
           //等待所有团队赛跑选手,释放跑完信号
     *     doneSignal.await();           // wait for all to finish
     *   }
     * }
     *
     * class Worker implements Runnable {
     *   private final CountDownLatch startSignal;
     *   private final CountDownLatch doneSignal;
     *   Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
     *      this.startSignal = startSignal;
     *      this.doneSignal = doneSignal;
     *   }
     *   public void run() {
     *      try {
             //等待开始信息号
     *        startSignal.await();
             //赛跑
     *        doWork();
              //跑完,计时员收到跑完信号通知
     *        doneSignal.countDown();
     *      } catch (InterruptedException ex) {} // return;
     *   }
     *
     *   void doWork() { ... }
     * }
     *
     * 
    * *

    Another typical usage would be to divide a problem into N parts, * describe each part with a Runnable that executes that portion and * counts down on the latch, and queue all the Runnables to an * Executor. When all sub-parts are complete, the coordinating thread * will be able to pass through await. (When threads must repeatedly * count down in this way, instead use a {@link CyclicBarrier}.) *所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务 *

     * class Driver2 { // ...
     *   void main() throws InterruptedException {
     *     CountDownLatch doneSignal = new CountDownLatch(N);
     *     Executor e = ...
     *
     *     for (int i = 0; i < N; ++i) // create and start threads
     *       e.execute(new WorkerRunnable(doneSignal, i));
     *
     *     doneSignal.await();           // wait for all to finish
     *   }
     * }
     *
     * class WorkerRunnable implements Runnable {
     *   private final CountDownLatch doneSignal;
     *   private final int i;
     *   WorkerRunnable(CountDownLatch doneSignal, int i) {
     *      this.doneSignal = doneSignal;
     *      this.i = i;
     *   }
     *   public void run() {
     *      try {
     *        doWork(i);
     *        doneSignal.countDown();
     *      } catch (InterruptedException ex) {} // return;
     *   }
     *
     *   void doWork() { ... }
     * }
     *
     * 
    * *

    Memory consistency effects: Until the count reaches * zero, actions in a thread prior to calling * {@code countDown()} * [url=package-summary.html#MemoryVisibility]happen-before[/url] * actions following a successful return from a corresponding * {@code await()} in another thread. * * @since 1.5 * @author Doug Lea */ public class CountDownLatch { /** * Synchronization control For CountDownLatch. * Uses AQS state to represent count. *用一个 AQS状态代表信号量数 */ private static final class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 4982264981922014374L; Sync(int count) { setState(count); } int getCount() { return getState(); } //获取信号量 protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; } //释放信号量 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; } } } private final Sync sync; /** * Constructs a {@code CountDownLatch} initialized with the given count. * * @param count the number of times {@link #countDown} must be invoked * before threads can pass through {@link #await} * @throws IllegalArgumentException if {@code count} is negative */ public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } /** * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@linkplain Thread#interrupt interrupted}. * *

    If the current count is zero then this method returns immediately. * *

    If the current count is greater than zero then the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of two things happen: * [list] *

  • The count reaches zero due to invocations of the * {@link #countDown} method; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. * [/list] * *

    If the current thread: * [list] *

  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting, * [/list] * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @throws InterruptedException if the current thread is interrupted * while waiting */ public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } /** * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@linkplain Thread#interrupt interrupted}, * or the specified waiting time elapses. * *

    If the current count is zero then this method returns immediately * with the value {@code true}. * *

    If the current count is greater than zero then the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of three things happen: * [list] *

  • The count reaches zero due to invocations of the * {@link #countDown} method; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or *
  • The specified waiting time elapses. * [/list] * *

    If the count reaches zero then the method returns with the * value {@code true}. * *

    If the current thread: * [list] *

  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting, * [/list] * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * *

    If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param timeout the maximum time to wait * @param unit the time unit of the {@code timeout} argument * @return {@code true} if the count reached zero and {@code false} * if the waiting time elapsed before the count reached zero * @throws InterruptedException if the current thread is interrupted * while waiting */ public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } /** * Decrements the count of the latch, releasing all waiting threads if * the count reaches zero. * *

    If the current count is greater than zero then it is decremented. * If the new count is zero then all waiting threads are re-enabled for * thread scheduling purposes. * *

    If the current count equals zero then nothing happens. */ public void countDown() { sync.releaseShared(1); } /** * Returns the current count. * *

    This method is typically used for debugging and testing purposes. * * @return the current count */ public long getCount() { return sync.getCount(); } /** * Returns a string identifying this latch, as well as its state. * The state, in brackets, includes the String {@code "Count ="} * followed by the current count. * * @return a string identifying this latch, as well as its state */ public String toString() { return super.toString() + "[Count = " + sync.getCount() + "]"; } }


  • 你可能感兴趣的:(java,CountDownLatch)