1. 程式人生 > >Java之CountDownLatch使用

Java之CountDownLatch使用

操作 java代碼 catch sdf none 同步輔助類 date() latch 調用

CountDownLatch,一個同步輔助類,在完成一組正在其他線程中執行的操作之前,它允許一個或多個線程一直等待。

主要方法

public CountDownLatch(int count);

public void countDown();

public void await() throws InterruptedException

構造方法參數指定了計數的次數

countDown方法,當前線程調用此方法,則計數減一

awaint方法,調用此方法會一直阻塞當前線程,直到計時器的值為0

例子

Java代碼 技術分享 技術分享
  1. public class CountDownLatchDemo {
  2. final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3. public static void main(String[] args) throws InterruptedException {
  4. CountDownLatch latch=new CountDownLatch(2);//兩個工人的協作
  5. Worker worker1=new Worker("zhang san", 5000, latch);
  6. Worker worker2=new Worker("li si", 8000, latch);
  7. worker1.start();//
  8. worker2.start();//
  9. latch.await();//等待所有工人完成工作
  10. System.out.println("all work done at "+sdf.format(new Date()));
  11. }
  12. static class Worker extends Thread{
  13. String workerName;
  14. int workTime;
  15. CountDownLatch latch;
  16. public Worker(String workerName ,int workTime ,CountDownLatch latch){
  17. this.workerName=workerName;
  18. this.workTime=workTime;
  19. this.latch=latch;
  20. }
  21. public void run(){
  22. System.out.println("Worker "+workerName+" do work begin at "+sdf.format(new Date()));
  23. doWork();//工作了
  24. System.out.println("Worker "+workerName+" do work complete at "+sdf.format(new Date()));
  25. latch.countDown();//工人完成工作,計數器減一
  26. }
  27. private void doWork(){
  28. try {
  29. Thread.sleep(workTime);
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }
public class CountDownLatchDemo {
	final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) throws InterruptedException {
    	CountDownLatch latch=new CountDownLatch(2);//兩個工人的協作
    	Worker worker1=new Worker("zhang san", 5000, latch);
    	Worker worker2=new Worker("li si", 8000, latch);
    	worker1.start();//
    	worker2.start();//
    	latch.await();//等待所有工人完成工作
        System.out.println("all work done at "+sdf.format(new Date()));
	}
    
    
    static class Worker extends Thread{
    	String workerName; 
    	int workTime;
    	CountDownLatch latch;
    	public Worker(String workerName ,int workTime ,CountDownLatch latch){
    		 this.workerName=workerName;
    		 this.workTime=workTime;
    		 this.latch=latch;
    	}
    	public void run(){
    		System.out.println("Worker "+workerName+" do work begin at "+sdf.format(new Date()));
    		doWork();//工作了
    		System.out.println("Worker "+workerName+" do work complete at "+sdf.format(new Date()));
    		latch.countDown();//工人完成工作,計數器減一

    	}
    	
    	private void doWork(){
    		try {
				Thread.sleep(workTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    	}
    }
    
     
}

輸出:

Worker zhang san do work begin at 2011-04-14 11:05:11 Worker li si do work begin at 2011-04-14 11:05:11 Worker zhang san do work complete at 2011-04-14 11:05:16 Worker li si do work complete at 2011-04-14 11:05:19 all work done at 2011-04-14 11:05:19

Java之CountDownLatch使用