java并发编程CountDownLatch
2024-10-11 17:34:12
/**
* CountDownLatch用法
* CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,
* 它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了
*/
@Test
public void getThread(){
final CountDownLatch latch = new CountDownLatch(2);
new Thread (){
@Override
public void run() {
try {
System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
Thread.sleep(3000);
System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
latch.countDown();
System.out.println(latch);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}.start();
new Thread (){
@Override
public void run() {
try {
System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
Thread.sleep(3000);
System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
latch.countDown();
System.out.println(latch);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}.start(); try {
System.out.println("等待2个子线程执行完毕...");
latch.await();
System.out.println("2个子线程已经执行完毕");
System.out.println("继续执行主线程");
}catch(InterruptedException e){
e.printStackTrace();
}
}
最新文章
- jquery css3问卷答题卡翻页动画效果
- mvc添加多条数据到数据库
- 读取.properties配置文件
- Java网络编程——IP
- aws在线技术峰会笔记-基于AWS的Devops最佳实践
- CustomerConfigHelper
- css cursor属性详解
- three.js 简介
- platform_driver与file_operations两种方法开发led驱动
- OC-手动内存管理
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- String类的实现,内部采用字符数组实现
- openNebula libvirt-virsh attach disk device for kvm
- JavaScript高级程序设计:第六章
- 使用nexus3.x搭建maven私服
- 得到一个文件夹中所有文件的名称的几个方法(命令指示符, C++, python)
- sqlserver 多行转一行
- Andorid 刷新样式一
- eclipse输入中文为繁体字
- GOROOT、GOPATH、GOBIN
热门文章
- Scoop及使用
- php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix
- .Net Core(完) 创建Docker镜像
- Linux 装机必备工具
- CentOS7安装搭建.Net Core 2.0环境-详细步骤
- .NET MVC 后台接受base64的上传图片
- 【PAT】B1075 链表元素分类(25 分)
- MySQL基本简单操作03
- 记录:一个SQL SERVER奇怪的问题。