CountDownLatch 사용 방법

4072 단어 다중 스레드
CountDownLatch는 하나 이상의 작업을 동기화하는 데 사용되며 다른 작업이 수행하는 일련의 작업이 완료될 때까지 기다리도록 합니다.
구조 방법CountDownLatch(int count)은 주어진 계수로 초기화된CountDownLatch을 구성한다.
이 대상에서 await () 를 호출하는 모든 방법은 이 기술 값이 0이 될 때까지 막힐 것입니다.다른 작업이 끝날 때, 이 대상에서countDown () 을 호출해서 이 계수 값을 줄일 수 있습니다.CountDownLatch는 한 번만 터치할 수 있도록 설계되었고 계수 값은 리셋할 수 없습니다.계수 값의 버전을 재설정해야 하는 경우 CyclicBarrier를 호출할 수 있습니다.countDown () 을 호출하는 작업은 이 호출이 발생할 때 막히지 않습니다. 계수치가 0에 도달할 때까지 await () 에 대한 호출만 막힙니다.CountDownLatch의 전형적인 사용법은 하나의 프로그램을 n개의 서로 독립된 해결 가능한 임무로 나누고 n의 값을 가진 CountDownLatch를 만드는 것이다.모든 작업이 완료되면 잠금 메모리에서countDown () 을 호출합니다.문제가 해결되기를 기다리는 작업은 이 레지스터에서 await () 를 호출해서 레지스터 계수가 끝날 때까지 스스로 막습니다.
예:
package thread;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo {
	static final int SIZE = 100;
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		CountDownLatch latch = new CountDownLatch(100);
		for(int i=0;i<10;i++)
			exec.execute(new WaitingTask(latch));
		for(int i=0;i
출력:
Launched all tasks
37  completed
45  completed
99  completed
95  completed
94  completed
11  completed
23  completed
77  completed
7   completed
9   completed
75  completed
10  completed
79  completed
39  completed
25  completed
36  completed
96  completed
63  completed
30  completed
40  completed
55  completed
90  completed
28  completed
88  completed
6   completed
50  completed
8   completed
12  completed
0   completed
27  completed
13  completed
98  completed
72  completed
71  completed
2   completed
43  completed
35  completed
15  completed
91  completed
18  completed
5   completed
33  completed
97  completed
21  completed
69  completed
34  completed
4   completed
68  completed
38  completed
48  completed
87  completed
70  completed
84  completed
86  completed
66  completed
54  completed
42  completed
29  completed
46  completed
74  completed
57  completed
1   completed
20  completed
65  completed
80  completed
16  completed
60  completed
89  completed
14  completed
51  completed
53  completed
62  completed
58  completed
76  completed
92  completed
22  completed
19  completed
56  completed
85  completed
61  completed
31  completed
26  completed
24  completed
59  completed
67  completed
47  completed
41  completed
32  completed
3   completed
52  completed
93  completed
81  completed
78  completed
73  completed
44  completed
49  completed
17  completed
82  completed
64  completed
83  completed
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]
Latch barrier passed for java.util.concurrent.CountDownLatch@189e7143[Count = 0]

WaitingTask의 마지막 실행, count=0을 볼 수 있습니다.

좋은 웹페이지 즐겨찾기