首页 > 其他分享 >CountDownLatch简单使用

CountDownLatch简单使用

时间:2022-12-22 20:11:35浏览次数:49  
标签:import 使用 boot springframework 简单 org latch CountDownLatch

如何保证主线程在副线程执行结束后才会执行结束,这里使用CountDownLatch

 

 

 

 

 //设置三个线程需要执行
CountDownLatch latch = new CountDownLatch(3);

  

//每调用一次数值减1,当count为0,代表全部线程执行结束
 latch.countDown();

  

 //当设置的线程未执行结束,保持等待状态
 latch.await();

  

 

 

 

 因为我设置的数量是3,但是只使用了两个,所以一直处于等待的状态

 

 

 

 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.java</groupId>
    <artifactId>test-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--tomcat容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <!--引入junit单元测试依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--判断空的用法  -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!--ThreadFactoryBuilder的依赖包,多线程使用-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>

        <!--Lists.partition要用的依赖-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
       <!--ListUtils.partition使用的依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
        <!--操作redis的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>study</finalName>
    </build>


</project>

  

server.port=2001

logging.level.com.java.test=debug
logging.level.web=debug
spring.devtools.add-properties=false

  

package com.java.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/10/20 15:32
 */
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

  

package com.java.test.countdownlatch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.CountDownLatch;

/**
 * @author Yourheart
 * @Create: 2022/12/22 11:41
 */
@Slf4j
/**
 * 主要用于标记配置类,兼备Component的效果。
 */
@Configuration
/**
 * 开启定时任务
 */
@EnableScheduling
public class CountDownLatchDemo {

    /**
     * 设置三个线程需要执行
     */
    public static CountDownLatch latch = new CountDownLatch(3);

    /**
     * 每隔15秒执行一次
     */
    //@Scheduled(cron = "0/15 * * * * *")
    //每天的17点45分0秒执行
    @Scheduled(cron = "0 45 17 * * *")
    @Async
    public void endTest() throws InterruptedException {
        long count = latch.getCount();
        log.info("count:{}",count);
        latch.countDown();
    }
}

  

package com.java.test.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/22 16:29
 */
@Slf4j
public class CountDownLatchThread implements Runnable {

    private final CountDownLatch latch;

    public CountDownLatchThread(String name, CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + "线程结束");
        //每调用一次数值减1,当count为0,代表全部线程执行结束
        latch.countDown();

    }
}

  

package com.java.test.countdownlatch;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/22 17:36
 */
@RestController
@Slf4j
public class CountDownLatchController {

    @GetMapping("/countDownLatchTest")
    public String countDownLatchTest() throws InterruptedException {

        StopWatch started = StopWatch.createStarted();

        long latchCount = CountDownLatchDemo.latch.getCount();
        log.info("latchCount:{}",latchCount);

        for(int i = 0; i<latchCount-1; i++){
            CountDownLatchThread latchThread = new CountDownLatchThread("线程"+i,CountDownLatchDemo.latch);
            new Thread(latchThread).start();
        }
        //当设置的线程未执行结束,保持等待状态
        CountDownLatchDemo.latch.await();
        log.info("耗时:{}",started.toString());

        return started.toString();

    }
}

  

标签:import,使用,boot,springframework,简单,org,latch,CountDownLatch
From: https://www.cnblogs.com/q202105271618/p/16999300.html

相关文章

  • Springboot+Mybatis+MySql下,mysql使用json类型字段存取的处理
    转载:Springboot+Mybatis+MySql下,mysql使用json类型字段存取的处理背景:1、mysql5.7开始支持json类型字段;2、mybatis暂不支持json类型字段的处理,需要自己做处理项目......
  • jmeter的函数使用
    JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便。JMeter函数是一种特殊值,可用于除测试计划外的任何组件。函数调用的格式如下所示:${__functionName(var1,var2......
  • 开源库glog使用
    windows下:1,下载地址: https://github.com/google/glog点击DownloadZIP下载即可。2.解压,打开google-glog.sln编译,生成debug下的lib和dll文件;3.将新建一个基于console......
  • Linux 初级班(A) – 2. Linux简单目录结构
    Linux 标准目录结构如下:1. 目录架构根目录:  /· root --- 启动​​Linux​​​时使用的一些核心文件。如操作系统​​内核​​​、引导程序​​Grub​​等。· hom......
  • Linux 初级班(A) – 1. Linux图形界面使用
    Linux图形界面和Windows差不了多少,大同小异。由于习惯性的问题,我们可能觉得Linux的界面不是特别友好,其实Linux是很友好的,他高效且可以定制化。安装后参见帮助文档半天就可以......
  • Maven -- 属性的定义和使用(可以理解为Maven的变量)
    属性的定义和使用版本统一的重要性   如果定义了多个资源不同版本的依赖,会以后面配置的为准,那么我们怎么实现资源版本的统一呢?可以用属性解决,相当于Java语言中......
  • [leetcode]第 6 天 搜索与回溯算法(简单)
    32-I.从上到下打印二叉树思路没有思路。。看题解要求二叉树从上至下打印,叫做二叉树的广度优先搜索(BFS)。BFS通常借助队列的先入先出特性实现。算法流程:1.特例处理......
  • 一款功能强大的浏览器网页视频下载插件-猫爪视频下载插件使用与安装
    猫爪视频下载插件官方版是一款功能强大的浏览器插件工具。通过猫爪视频下载插件用户可以轻松的抓取任意网页的视频文件,并将其保存到本地,节省了缓存加载的时间。猫爪视频......
  • vue3使用composables来取代mixins 来状态复用
    前言最简单理解composables的方式就是将其视为vue版的自定义hooks来个demo比如页面初始化的时候我需要请求接口获取数据并回填至表单以前的做法src/pages/index.......
  • Logoist - 适用于设计师以及初次使用者的快速制作精美 logo 工具
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/24c0f566dcf14be2aa72afaa78c87c40.png)>从简单的标识到设计开发。它只需要一点时间来创建令人印象深刻的图像和矢量......