/**
* 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();
}
}

最新文章

  1. jquery css3问卷答题卡翻页动画效果
  2. mvc添加多条数据到数据库
  3. 读取.properties配置文件
  4. Java网络编程——IP
  5. aws在线技术峰会笔记-基于AWS的Devops最佳实践
  6. CustomerConfigHelper
  7. css cursor属性详解
  8. three.js 简介
  9. platform_driver与file_operations两种方法开发led驱动
  10. OC-手动内存管理
  11. ASP.NET多线程下使用HttpContext.Current为null解决方案
  12. String类的实现,内部采用字符数组实现
  13. openNebula libvirt-virsh attach disk device for kvm
  14. JavaScript高级程序设计:第六章
  15. 使用nexus3.x搭建maven私服
  16. 得到一个文件夹中所有文件的名称的几个方法(命令指示符, C++, python)
  17. sqlserver 多行转一行
  18. Andorid 刷新样式一
  19. eclipse输入中文为繁体字
  20. GOROOT、GOPATH、GOBIN

热门文章

  1. Scoop及使用
  2. php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)
  3. 方差variance, 协方差covariance, 协方差矩阵covariance matrix
  4. .Net Core(完) 创建Docker镜像
  5. Linux 装机必备工具
  6. CentOS7安装搭建.Net Core 2.0环境-详细步骤
  7. .NET MVC 后台接受base64的上传图片
  8. 【PAT】B1075 链表元素分类(25 分)
  9. MySQL基本简单操作03
  10. 记录:一个SQL SERVER奇怪的问题。