当前位置: 首页 > 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-----
------------------全部结束------------------

http://www.dtcms.com/a/4924.html

相关文章:

  • 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家庭版中文转专业版流程
  • 【数据集】中国1米分辨率土地覆盖图SinoLC-1
  • git的使用
  • 【自主探索】基于 frontier_exploration 的单个机器人自主探索建图
  • NextJS开发:封装shadcn/ui中的AlertDialog确认对话框
  • web静态网页设计与制作-基于HTML+CSS+JS实现旅游摄影网站
  • 强芯铸魂,生态共赢!麒麟信安出席2023龙芯产品发布暨用户大会
  • 【经验分享】开发问题记录总结(持续更新)
  • 机器学习-线性模型·
  • 蓝桥杯每日一题2023.11.28
  • 【微服务】java 规则引擎使用详解