当前位置: 首页 > news >正文

java多线程CountDownLatch简单测试

学习java多线程,请同时参阅    Java多线程 信号量和屏障实现控制并发线程数量,主线程等待所有线程执行完毕1

CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后再继续执行。当所有的线程都已经完成任务,然后在CountDownLatch上等待的线程就可以恢复执行接下来的任务。

代码如下:


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo {


    public static void main(String[] args) throws InterruptedException {

        ExecutorService threadPool= Executors.newFixedThreadPool(10);
        final CountDownLatch latch = new CountDownLatch(10);


        for(int i=0;i<10;i++){
            threadPool.execute(new Runnable(){
                @Override
                public void run() {

                        try {
                            System.out.println("-----------开始-----j-----" );
                            System.out.println("------------threadName--j---" + "====" + Thread.currentThread().getName());
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }finally {
                            System.out.println("------------threadName--j---" + "==finally==" + Thread.currentThread().getName());
                            System.out.println("---------完成了------j-----" );
                            latch.countDown();
                        }
                        
                }
            });
        }
        latch.await();
        System.out.println("------------------全部结束------------------" );
    }




}

运行上面测试代码输出如下:

-----------开始-----j-----
-----------开始-----j-----
------------threadName--j---====pool-1-thread-1
-----------开始-----j-----
-----------开始-----j-----
-----------开始-----j-----
------------threadName--j---====pool-1-thread-3
-----------开始-----j-----
-----------开始-----j-----
------------threadName--j---====pool-1-thread-5
-----------开始-----j-----
------------threadName--j---====pool-1-thread-4
------------threadName--j---====pool-1-thread-6
------------threadName--j---====pool-1-thread-7
------------threadName--j---====pool-1-thread-8
-----------开始-----j-----
------------threadName--j---====pool-1-thread-2
-----------开始-----j-----
------------threadName--j---====pool-1-thread-9
------------threadName--j---====pool-1-thread-10
------------threadName--j---==finally==pool-1-thread-6
------------threadName--j---==finally==pool-1-thread-5
------------threadName--j---==finally==pool-1-thread-8
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-1
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-3
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-2
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-10
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-7
---------完成了------j-----
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-4
---------完成了------j-----
------------threadName--j---==finally==pool-1-thread-9
---------完成了------j-----
---------完成了------j-----
------------------全部结束------------------

相关文章:

  • lodash常见的方法
  • Centos7安装docker、java、python环境
  • ubuntu/vscode下的c/c++开发之-CMake语法与练习
  • 网狐类源码游戏配置数据库数据(一键配置网狐数据库)
  • Kubernetes v1.28.4 安装笔记
  • Linux常用命令----history命令
  • Find My键盘|苹果Find My技术与键盘结合,智能防丢,全球定位
  • Git常用命令#更改用户名
  • web:ics-05(本地文件包含漏洞、preg_replace函数/e漏洞、php伪协议读取文件)
  • C语言——猜凶手
  • Kotlin学习——kt中的类,数据类 枚举类 密封类,以及对象
  • Element-UI Upload 手动上传文件的实现与优化
  • Vue中mvvm的作用
  • 【上海大学数字逻辑实验报告】一、基本门电路
  • Node——Node.js简介
  • Flutter应用程序加固的问题及解决方案
  • 【心得】XXE漏洞利用个人笔记
  • Python与GPU编程快速入门(五)
  • 机器学习中的混淆矩阵
  • window10家庭版中文转专业版流程
  • 男子不满和睦家医院手术效果还遇到了“冒牌医生”?院方回应
  • 中哥两国元首共同见证签署《中华人民共和国政府与哥伦比亚共和国政府关于共同推进丝绸之路经济带和21世纪海上丝绸之路建设的合作规划》
  • 北京今日白天超30℃晚间下冰雹,市民称“没见过这么大颗的”
  • 云南威信麟凤镇通报“有人穿‘警察’字样雨衣参与丧事”:已立案查处
  • 睡觉总做梦是睡眠质量差?梦到这些事,才要小心
  • 工人日报评规范隐藏式车门把手:科技美学须将安全置顶