Java骚操作之CountDownLatch代码详解
作者:Java Pro 时间:2023-01-17 11:14:41
简述
用来干嘛的?当你在方法中调用了多个线程,对数据库进行了一些不为人知的操作后,还有一个操作需要留到前者都执行完的重头戏,就需要用到 CountDownLatch
了
实践代码
package com.github.gleans;
import java.util.concurrent.CountDownLatch;
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(3);
new KeyPass(1000L, "thin jack", latch).start();
new KeyPass(2000L, "noral jack", latch).start();
new KeyPass(3000L, "fat jack", latch).start();
latch.await();
System.out.println("此处对数据库进行最后的插入操作~");
}
static class KeyPass extends Thread {
private long times;
private CountDownLatch countDownLatch;
public KeyPass(long times, String name, CountDownLatch countDownLatch) {
super(name);
this.times = times;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
System.out.println("操作人:" + Thread.currentThread().getName()
+ "对数据库进行插入,持续时间:" + this.times / 1000 + "秒");
Thread.sleep(times);
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
图解
使用await()提前结束操作
package com.github.gleans;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(3);
new KeyPass(2000L, "公司一", latch).start();
new KeyPass(3000L, "公司二", latch).start();
new KeyPass(5000L, "公司三", latch).start();
latch.await(2, TimeUnit.SECONDS);
System.out.println("~~~贾总PPT巡演~~~~");
System.out.println("~~~~融资完成,撒花~~~~");
}
static class KeyPass extends Thread {
private long times;
private CountDownLatch countDownLatch;
public KeyPass(long times, String name, CountDownLatch countDownLatch) {
super(name);
this.times = times;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(times);
System.out.println("负责人:" + Thread.currentThread().getName()
+ "开始工作,持续时间:" + this.times / 1000 + "秒");
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
假设公司一、公司二、公司三各需要2s、3s、5s来完成工作,贾总等不了,只能等2s,那么就设置await的超时时间
latch.await(2, TimeUnit.SECONDS);
执行结果
负责人:公司一开始工作,持续时间:2秒
~~~贾总PPT巡演~~~~
~~~~融资完成,撒花~~~~
负责人:公司二开始工作,持续时间:3秒
负责人:公司三开始工作,持续时间:5秒
方法描述
来源:https://blog.csdn.net/Fine_Cui/article/details/107144502
标签:Java,CountDownLatch
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven的pom.xml中profiles的作用详解
2022-07-03 20:40:54
![](https://img.aspxhome.com/file/2023/9/64509_0s.png)
Android 添加系统设置属性的实现及步骤
2021-12-31 01:11:02
SpringBoot@DeleteMapping(/xxx/{id})请求报405的解决
2022-06-13 14:00:48
![](https://img.aspxhome.com/file/2023/8/78338_0s.png)
Java中ArrayList与顺序表的定义与实现方法
2022-06-08 03:27:12
![](https://img.aspxhome.com/file/2023/7/67197_0s.png)
Mybatis-Plus自动填充更新操作相关字段的实现
2023-06-04 22:37:12
Java接口的作用_动力节点Java学院整理
2021-12-23 20:46:14
![](https://img.aspxhome.com/file/2023/0/61570_0s.png)
深入分析c# 继承
2023-02-18 21:36:31
Android在项目中接入腾讯TBS浏览器WebView的教程与注意的地方
2021-12-18 01:58:28
![](https://img.aspxhome.com/file/2023/5/139505_0s.png)
C#简单的加密类实例
2022-12-15 11:31:18
SpringMVC的执行过程浅析
2021-05-31 20:51:11
![](https://img.aspxhome.com/file/2023/4/62074_0s.png)
在Winform动态启动、控制台命令行的方法
2021-11-02 05:11:07
Java中MyBatis Plus知识点总结
2023-08-11 14:12:04
java.util.Collection源码分析与深度理解
2022-07-31 09:05:52
![](https://img.aspxhome.com/file/2023/5/80215_0s.png)
Java Swing GridBagLayout网格袋布局的实现
2023-07-11 19:34:26
![](https://img.aspxhome.com/file/2023/8/74908_0s.png)
Spring+Vue整合UEditor富文本实现图片附件上传的方法
2022-09-27 10:36:13
![](https://img.aspxhome.com/file/2023/3/78033_0s.png)
Java 数据结构与算法系列精讲之时间复杂度与空间复杂度
2022-03-19 20:19:50
![](https://img.aspxhome.com/file/2023/6/75656_0s.gif)
Android屏幕旋转之横屏竖屏切换的实现
2023-10-29 21:40:10
java实现双色球彩票游戏
2022-06-29 12:39:45
![](https://img.aspxhome.com/file/2023/2/66652_0s.jpg)
java反射之Method的invoke方法实现教程详解
2023-07-09 20:24:36
![](https://img.aspxhome.com/file/2023/2/66632_0s.png)
Android用 Mob 实现发送短信验证码实例
2021-10-17 03:53:16
![](https://img.aspxhome.com/file/2023/4/106814_0s.jpg)