CountDownLatch简单使用

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

 

 

 

 

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

  

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

  

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

  

 

 

 

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

 

 

 

 pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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>

  

1
2
3
4
5
server.port=2001
 
logging.level.com.java.test=debug
logging.level.web=debug
spring.devtools.add-properties=false

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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();
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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();
 
    }
}

  

posted @   不忘初心2021  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示