Java之CountDownLatch使用

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

时间: 2024-11-12 21:48:26

Java之CountDownLatch使用的相关文章

java多线程 -- CountDownLatch 闭锁

CountDownLatch 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.闭锁可以延迟线程的进度直到其到达终止状态,闭锁可以用来确保某些活动直到其他活动都完成才继续执行: 确保某个计算在其需要的所有资源都被初始化之后才继续执行; 确保某个服务在其依赖的所有其他服务都已经启动之后才启动; 等待直到某个操作所有参与者都准备就绪再继续执行. package com.company; import java.util.concurrent.CountDownLa

java计数器CountDownLatch

CountDownLatch里面有个属性为state,当为零时触发代码往下执行,代码如下: package threadLock; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchTest

浅析Java中CountDownLatch用法

/** CountDownLatch类是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞程序继续执行 CountDownLatch如其所写,是一个倒计数的锁存器,当计数减至0时触发特定的事件.利用这种特性,可以让主线程等待子线程的结束.下面以一个模拟运动员比赛的例子加以说明. */ import java.util.concurrent.CountDownLatch; import jav

java中CountDownLatch线程同步的入门使用

一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 下面使用了两个倒计数锁存器:即一个等待另一个的完成 import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchTest { /** * @param args

java 多线程 CountDownLatch用法

ountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); public void countDown(); public void await() throws InterruptedException 构造方法参数指定了计数的次数 countDown方法,当前线程调用此方法,则计数减一 awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0 例子

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 main(String[] args) { ExecutorService

使用Java辅助类(CountDownLatch、CyclicBarrier、Semaphore)并发编程

在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法 一.CountDownLatch用法 CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能.比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了. CountDownLatch类只提

【JAVA】java中CountDownLatch的用法,实例讲解

CountDownLatch主要用于多线程环境中,当所有的线程都countDown了,就会释放所有的等待的线程,await在到0之前一直等待. 直接看一段代码: package thread.thread; import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; impor

Java中CountDownLatch使用初步

package gj.lang.util.concurrent.countdonwlatch; import java.util.Random; import java.util.concurrent.CountDownLatch; /** * Author: areful * Date: 2019/4/24 */ public class CountDownLatchSample { private static class Player implements Runnable { priva