CountDownLatch两种用法
1.当前线程等待其他线程执行完毕后在执行。
2.模拟高并发场景。
在多线程编程里,CountDownLatch是一个很好的计数器工具。
常用的两个方法:
1、计数器减一
public void countDown() {
sync.releaseShared(1);
}
2、线程等待,在计算器未到达0之前会一直等待
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
好,下面来演示两个demo.
1、等待所有子线程执行完成后再执行主线程的情况
2、模拟高并发的情况
package com.figo.study.test;
import java.util.concurrent.CountDownLatch;
public class TestSomething {
public static void main(String[] args) {
//testWaitThread();
testConcurrent();
}
/**
* 1、模拟所有子线程都执行完成后再执行主线程
* countdownLatch计数,模拟子线程执行完成之后再执行主线程
* 这个也可以用future来实现
*
*/
public static void testWaitThread()
{
final CountDownLatch latch = new CountDownLatch(2);
new Thread(){
public void run() {
try {
System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
Thread.sleep(3000);
System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread(){
public void run() {
try {
System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
Thread.sleep(3000);
System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
latch.countDown();
} 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();
}
}
/**
* 线程数量
*/
public static final int THREAD_NUM = 100;
/**
* 开始时间
*/
private static long startTime = 0L;
/**
* 2、模拟高并发
*/
public static void testConcurrent()
{
try {
startTime = System.currentTimeMillis();
System.out.println("CountDownLatch started at: " + startTime);
// 初始化计数器为1
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i = 0; i < THREAD_NUM; i ++) {
new Thread(new Run(countDownLatch)).start();
}
// 启动多个线程
countDownLatch.countDown();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
/**
* 线程类
*/
private static class Run implements Runnable {
private final CountDownLatch startLatch;
public Run(CountDownLatch startLatch) {
this.startLatch = startLatch;
}
@Override
public void run() {
try {
// 线程等待
startLatch.await();
// 模拟耗时操作
Thread.sleep(3000);
long endTime = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName() + " ended at: " + endTime + ", cost: " + (endTime - startTime) + " ms.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
CountDownLatch两种用法的更多相关文章
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- Service的两种用法及其生命周期
先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...
- JSP中的include的两种用法
1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用法区别 (1)执行时间上区别 < ...
- 子查询。ANY三种用法。ALL两种用法。HAVING中使用子查询。SELECT中使用子查询。
子查询存在的意义是解决多表查询带来的性能问题. 子查询返回单行多列: ANY三种用法: ALL两种用法: HAVING中的子查询返回单行单列: SELECT中使用子查询:(了解就好,避免使用这种方法! ...
- Comparable和Comparator的区别&Collections.sort的两种用法
在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...
- in有两种用法:
# in有两种用法: 1. 在for中. 是把每一个元素获取到赋值给前⾯的变量. 2. 不在for中. 判断xxx是否出现在str中. #len() 为内置函数,输出为1,2,3,4....., 长度 ...
- [VC]C++ operator 两种用法
C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍: 1.operato ...
- jsp中include的两种用法
JSP中的include的两种用法 1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用 ...
随机推荐
- 内存泄漏 tensorflow
http://blog.csdn.net/qq_25737169/article/details/78125550
- java中的线程问题(三)——继承Thread VS 实现Runnable的区别
从java的设计来看,通过继承Thread或者实现Runnable接口来创建线程本质上没有区别,从jdk帮助文档我们可以看到Thread类本身就实现了Runnable接口,如果一定要说它们有什么区别, ...
- Kafka0.8.2删除topic逻辑(转)
原文链接:Kafka0.8.2.1删除topic逻辑 前提条件: 在启动broker时候开启删除topic的开关,即在server.properties中添加: delete.topic.enabl ...
- oracle高级分组
基本group by用法 create table test_table(a varchar(20),b varchar(20),c varchar(20)) insert into test_tab ...
- opcode cache与JIT的区别
要说明opcode cache与JIT的区别,得先明白,字节码,又叫中间码与机器码的区别. 操作码(opcode) 一条机器指令.比如我们汇编语言写的一条操作语句. 机器码(machine code) ...
- 关于maven环境变量的配置问题
开始使用“MAVEN_HOME”配置完环境变量后,在cmd中输入mvn -v提示不是内部命令,后直接在PATH 路径里面添加maven所在的位置+\bin,比如,maven的路径为E:\maven\a ...
- 学习HashMap随笔(更新中)
1.先来一个HashMap和HashTable的区别: HashMap线程不安全,键值可以为空 HashTable线程安全,键值不可以为空 2.hashmap我理解的是把数组存储和链表存储相结合了 具 ...
- day 12 名称空间和闭包函数
函数嵌套 按照函数的两个阶段分为: 嵌套调用:在一个函数内部调用另一个函数 嵌套定义:在一个函数内部定义另一个函数 名称空间(namespace):存储名字的内存区域 名称空间的分类: 内置名称空间: ...
- 使用mybatis调用存储过程(注解形式和配置文件形式)
最近在看资料中涉及到mybatis,突然想到mysql中的视图.存储过程.函数.现将在使用mybatis调用mysql的存储过程使用总结下: 使用的环境:mybatis3.4.6,mysql 5.6, ...
- 【软件安装】nvidia驱动安装事宜
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html https://docs.nvidia.com/cuda/arch ...