CountDownLatch的使用

简介

Latch即为门闩的意思,它所表达的意思是:当门没有打开时,所有的人都无法进入,即所有的线程也无所谓继续向下运行,这样可以控制线程执行任务的时机,使线程可以以组团的方式一起执行任务,CountDownLatch类所提供的功能是判断count计数不为0时,当前线程是wait状态,即都处于阻塞等待。同时CountDownLatch类也是一个具有同步功能的辅助类,使用效果是 给定一个计数,当时用这个CountDownLatch类的线程判断计数不为0时,则呈wait状态,如果为0时则继续运行。实现等待和继续运行的效果分别需要使用await()和countDown()方法来进行。调用await()方法时判断计数是否为0,如果不为0则呈等待状态。其他线程可以调用countDown()方法将计数减1,当计数减到0时,呈等待状态的线程继续运行。而方法getCount()就是获得当前的计数个数,需要注意的是,计数值无法被重置,计数是减法操作

API使用

在这里插入图片描述

1.1、await()以及countDwon()方法的使用

案例

public class MyService {
    //创建1个计数的实例
	private CountDownLatch downLatch = new CountDownLatch(1);
	public void testMethod() {
		try {
			System.out.println("A");
			//执行await方法时,程序被阻塞
			downLatch.await();
			System.out.println("B");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void downMethod() {
		System.out.println("X");
		//当计数为0时,程序继续运行
		downLatch.countDown();
	}
}

public class MyThread extends Thread {

	private MyService service;
	
	public MyThread(MyService service) {
		super();
		this.service = service;
	}
	
	@Override
	public void run() {
		service.testMethod();
	}
}

public class Run {
	
	public static void main(String[] args) throws InterruptedException {
		MyService service  = new MyService();
		MyThread thread = new MyThread(service);
		thread.start();
		Thread.sleep(2000);
		service.downMethod();
	}

}

运行结果如下:
在这里插入图片描述

1.2、await(long timeout, TimeUnit unit)的使用
此方法的作用是使线程在指定的最大时间单位内进入WAITING状态,如果超过这个时间则自动唤醒,程序继续向下运行。

案例如下:

public class MyService {
    //创建1个计数的实例
	private CountDownLatch downLatch = new CountDownLatch(1);
	public void testMethod() {
		try {
			System.out.println(Thread.currentThread().getName()+"准备"+System.currentTimeMillis());
			//执行await方法时,程序被阻塞3秒
			downLatch.await(3,TimeUnit.SECONDS);
			System.out.println(Thread.currentThread().getName()+"结束"+System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
}

public class MyThread extends Thread {

	private MyService service;
	
	public MyThread(MyService service) {
		super();
		this.service = service;
	}
	
	@Override
	public void run() {
		service.testMethod();
	}
}

public class Run {
	
	public static void main(String[] args) throws InterruptedException {
		MyService service  = new MyService();
		
		
		MyThread[] threadArray = new MyThread[3];
		for (int i = 0; i < threadArray.length; i++) {
			threadArray[i] = new MyThread(service);
		}
		
		for (int i = 0; i < threadArray.length; i++) {
			threadArray[i].start();
		}
		
	}

}

运行结果如下:
在这里插入图片描述

1.3、getCount()的使用
getCount()获取当前计数的值

案例如下:

public class Run {
	
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch count = new CountDownLatch(3);
		System.out.println("main getCount1="+count.getCount());
		count.countDown();
		System.out.println("main getCount2="+count.getCount());
		count.countDown();
		System.out.println("main getCount3="+count.getCount());
		count.countDown();
		System.out.println("main getCount4="+count.getCount());
		count.countDown();
		System.out.println("main getCount5="+count.getCount());
		count.countDown();
		System.out.println("main getCount6="+count.getCount());
	}

}

结果如下:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

升仔聊编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值