概述
本文详细介绍CountDownLatch的两种使用场景,分别是倒数(等待所有的线程处理完成)和唤醒所有线程同时运行(适用于性能测试中触发所有并发同时运行)。
下文从这个两个方面进行介绍
CountDownLatch倒数实例
实例代码利用CountDownLatch的downLatch方法让子线程在运行结束了减2;利用await方法,让主线程阻塞,见下实例代码。
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
|
package com.yang.concurrent; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchDemo01 { public static void main(String[] args) { final CountDownLatch countDownLatch= new CountDownLatch( 5 ); ExecutorService executorService = Executors.newFixedThreadPool( 5 ); for ( int i = 0 ; i < 5 ; i++) { Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + "等待运行中" ); Thread.sleep( 1000 ); System.out.println(Thread.currentThread().getName() + "运行结束" ); countDownLatch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }; executorService.submit(runnable); } System.out.println( "主线程等待子线程运行结束" ); try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "主线程运行结束" ); } } |
运行结果如下图所示:
CountDownLatch同时运行实例
本实例利用CountDownLatch的await方法让所以的子线程都等待,待相关资源准备好后,主线程通知线程运行。如下实例代码所示:
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
|
package com.yang.concurrent; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 本实例演示子线程都等待 */ public class CountDownLatchDemo02 { public static void main(String[] args) { final CountDownLatch countDownLatch= new CountDownLatch( 1 ); ExecutorService executorService = Executors.newFixedThreadPool( 5 ); for ( int i = 0 ; i < 5 ; i++) { Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + "等待运行中" ); countDownLatch.await(); System.out.println(Thread.currentThread().getName() + "运行结束" ); } catch (InterruptedException e) { e.printStackTrace(); } } }; executorService.submit(runnable); } System.out.println( "主线程准备相关资源" ); try { Thread.sleep( 1000 ); System.out.println( "主线程资源准备就绪,子线程可以运行了" ); countDownLatch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } } |
运行结果如下图所示:
CountDownLatch赛跑实例
下实例代码我们模拟百米赛跑的故事,赛跑先要进行相关的准备工作,待准备工作完成后,明枪起跑,待所有人都到终点后,起跑结束,相关实例代码如下图所示。
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
|
package com.yang.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 本实例演示子线程都等待 */ public class CountDownLatchDemo03 { public static void main(String[] args) { final CountDownLatch beginCountDownLatch= new CountDownLatch( 1 ); final CountDownLatch endCountDownLatch= new CountDownLatch( 5 ); ExecutorService executorService = Executors.newFixedThreadPool( 5 ); System.out.println( "百米赛跑准备中" ); for ( int i = 0 ; i < 5 ; i++) { Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + "准备起跑!" ); beginCountDownLatch.await(); Thread.sleep(( long ) new Random( 1000 ).nextLong()); System.out.println(Thread.currentThread().getName() + "到达终点" ); endCountDownLatch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }; executorService.submit(runnable); } try { Thread.sleep( 1000 ); System.out.println( "名枪起跑" ); beginCountDownLatch.countDown(); endCountDownLatch.await(); System.out.println( "所有人到达终点,赛跑结束" ); } catch (InterruptedException e) { e.printStackTrace(); } } } |
运行结果如下: