1、CyclicBarrier:
一個(gè)同步輔助類,用于協(xié)調(diào)多個(gè)子線程,讓多個(gè)子線程在這個(gè)屏障前等待,直到所有子線程都到達(dá)了這個(gè)屏障時(shí),再一起繼續(xù)執(zhí)行后面的動(dòng)作。
2、使用場(chǎng)景舉例:
年末公司組織團(tuán)建,要求每一位員工周六上午8點(diǎn)【自駕車】到公司門口集合,然后【自駕車】前往目的地。
在這個(gè)案例中,公司作為主線程,員工作為子線程。
3、代碼示例:
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
|
package com.test.spring.support; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author javaloveiphone * @date 創(chuàng)建時(shí)間:2017年1月25日 上午10:59:11 * @Description: */ public class Company { public static void main(String[] args) throws InterruptedException { //員工數(shù)量 int count = 5 ; //創(chuàng)建計(jì)數(shù)器 CyclicBarrier barrier = new CyclicBarrier(count+ 1 ); //創(chuàng)建線程池,可以通過(guò)以下方式創(chuàng)建 //ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1,1,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(count)); ExecutorService threadPool = Executors.newFixedThreadPool(count); System.out.println( "公司發(fā)送通知,每一位員工在周六早上8點(diǎn)【自駕車】到公司大門口集合" ); for ( int i = 0 ;i<count ;i++){ //將子線程添加進(jìn)線程池執(zhí)行 threadPool.execute( new Employee(barrier,i+ 1 )); Thread.sleep( 10 ); } try { //阻塞當(dāng)前線程,直到所有員工到達(dá)公司大門口之后才執(zhí)行 barrier.await(); Thread.sleep( 10 ); // 使當(dāng)前線程在鎖存器倒計(jì)數(shù)至零之前一直等待,除非線程被中斷或超出了指定的等待時(shí)間。 //latch.await(long timeout, TimeUnit unit) System.out.println( "所有員工已經(jīng)到達(dá)公司大門口,公司領(lǐng)導(dǎo)一并【自駕車】同員工前往活動(dòng)目的地。" ); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } finally { //最后關(guān)閉線程池,但執(zhí)行以前提交的任務(wù),不接受新任務(wù) threadPool.shutdown(); //關(guān)閉線程池,停止所有正在執(zhí)行的活動(dòng)任務(wù),暫停處理正在等待的任務(wù),并返回等待執(zhí)行的任務(wù)列表。 //threadPool.shutdownNow(); } } } //分布式工作線程 class Employee implements Runnable{ private CyclicBarrier barrier; private int employeeIndex; public Employee(CyclicBarrier barrier, int employeeIndex){ this .barrier = barrier; this .employeeIndex = employeeIndex; } @Override public void run() { try { System.out.println( "員工:" +employeeIndex+ ",正在前往公司大門口集合..." ); Thread.sleep( 10 *employeeIndex); System.out.println( "員工:" +employeeIndex+ ",已到達(dá)。" ); barrier.await(); Thread.sleep( 10 ); System.out.println( "員工:" +employeeIndex+ ",【自駕車】前往目的地" ); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } |
打印輸出可能的結(jié)果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
公司發(fā)送通知,每一位員工在周六早上 8 點(diǎn)【自駕車】到公司大門口集合 員工: 1 ,正在前往公司大門口集合... 員工: 1 ,已到達(dá)。 員工: 2 ,正在前往公司大門口集合... 員工: 3 ,正在前往公司大門口集合... 員工: 2 ,已到達(dá)。 員工: 4 ,正在前往公司大門口集合... 員工: 5 ,正在前往公司大門口集合... 員工: 3 ,已到達(dá)。 員工: 4 ,已到達(dá)。 員工: 5 ,已到達(dá)。 員工: 3 ,【自駕車】前往目的地 員工: 5 ,【自駕車】前往目的地 所有員工已經(jīng)到達(dá)公司大門口,公司領(lǐng)導(dǎo)一并【自駕車】同員工前往活動(dòng)目的地。 員工: 4 ,【自駕車】前往目的地 員工: 1 ,【自駕車】前往目的地 員工: 2 ,【自駕車】前往目的地 |
注意:
子線程執(zhí)行了await()方法,必須等待其它所有子線程執(zhí)行await()方法之后,才能一起繼續(xù)后續(xù)的(await后main的)工作,就像上面的例子,所有自駕車必須都到達(dá)公司大門口之后,才能一起繼續(xù)各自自駕車前往目的地。
但,主線程await()之后的工作與子線程await()之后的工作是不受影響的,只要所有的子線程執(zhí)行了await()方法,主線程此時(shí)就可以后續(xù)的工作了,不必管子線程await()方法后續(xù)工作的情況。
4、CyclicBarrier與CountDownLatch的區(qū)別:
1)、構(gòu)造兩者對(duì)象傳入的參數(shù)不一樣:構(gòu)造CyclicBarrier比構(gòu)造CountDownLatch的參數(shù)大了1,原因是構(gòu)造CyclicBarrier的數(shù)量表示的是調(diào)用await()的次數(shù),構(gòu)造CountDownLatch的數(shù)量表示的是調(diào)用countDown()的次數(shù);
2)、子線程調(diào)用了barrier.await()之后,必須等待所有子線程都完成barrier.await()調(diào)用后才能一起繼續(xù)后續(xù)自己的工作,而子線程調(diào)用latch.countDown()之后,會(huì)繼續(xù)子線程自己的工作,不用等待其它子線程latch.countDown()調(diào)用情況。
3)、CyclicBarrier可以循環(huán)使用,而CountDownLatch不是循環(huán)使用的。
4)、程序?qū)Ρ瓤梢钥矗?a href="http://www.zmynmublwnt.cn/article/182376.html" target="_blank">java多線程CountDownLatch及線程池ThreadPoolExecutor/ExecutorService使用案例
補(bǔ)充:CyclicBarrier的使用,多線程'同時(shí)'啟動(dòng)
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
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.noway.test.concurrent.cyclicBarrier; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * 多線程中如何'同時(shí)'啟動(dòng)多個(gè)線程 * @author Noway * */ public class TestCyclicBarrier { public static void main(String[] args) { CyclicBarrier cb = new CyclicBarrier( 3 ); new Thread( new cbThread(cb, "張三" )).start(); new Thread( new cbThread(cb, "李四" )).start(); new Thread( new cbThread(cb, "王五" )).start(); new Thread( new cbThread(cb, "馬六" )).start(); new Thread( new cbThread(cb, "小七" )).start(); } } class cbThread implements Runnable{ private CyclicBarrier cb; private String name; public cbThread(CyclicBarrier cb, String name) { super (); this .cb = cb; this .name = name; } @Override public void run() { System.out.println( this .name+ "準(zhǔn)備好了..." ); try { this .cb.await(); //形成一個(gè)屏障 } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println( this .name+ "出發(fā)了..." ); } } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/javaloveiphone/article/details/54730384