如何保证主线程在副线程执行结束后才会执行结束,这里使用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