CountDownLatch 및 CyclicBarrier 둘 다 멀티스레딩 환경에서 사용되며 둘 다 일부입니다.
Java 문서에 따라 -
CountDownLatch - 하나 이상의 스레드가 다른 스레드에서 수행 중인 일련의 작업이 완료될 때까지 대기할 수 있도록 하는 동기화 지원입니다.
CyclicBarrier - 스레드 집합이 모두 서로가 공통 장벽 지점에 도달할 때까지 기다릴 수 있도록 하는 동기화 보조 장치입니다.
| Sr. 아니요. | 키 | CyclicBarrier | CountDownLatch |
|---|---|---|---|
| 1 | 기본 | 스레드 세트가 모두 서로가 공통 장벽 지점에 도달하기를 기다릴 수 있도록 하는 동기화 지원. | 하나 이상의 스레드가 다른 스레드에서 수행 중인 일련의 작업이 완료될 때까지 대기할 수 있도록 하는 동기화 지원입니다. |
| 2 | 실행 가능 | Runnable을 제공할 수 있는 생성자가 있습니다. | 이러한 생성자가 없습니다 |
| 3 | 스레드 /Task | 스레드 수를 유지합니다. | 작업 수를 유지합니다. |
| 4. | 좋은 | 그것은 유리하지 않다 | 그것은 유리하다. |
| 5 | 예외 | 대기 중 하나의 스레드가 중단되면 다른 모든 대기 스레드에서 B rokenBarrierException이 발생합니다. | 현재 스레드만 throw됩니다. 인터럽트된 예외. 다른 스레드에는 영향을 미치지 않습니다. |
CyclicBarrier의 예
public class Main {
public static void main(String args[]) throws InterruptedException {
ExecutorService executors = Executors.newFixedThreadPool(4);
CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
executors.submit(new Service1(cyclicBarrier));
executors.submit(new Service1(cyclicBarrier));
executors.submit(new Service2(cyclicBarrier));
executors.submit(new Service2(cyclicBarrier));
executors.submit(new Service2(cyclicBarrier));
Thread.sleep(3000);
System.out.println("Done");
}
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Service1 implements Runnable {
CyclicBarrier cyclicBarrier;
public Service1(CyclicBarrier cyclicBarrier) {
super();
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println("Services1" + cyclicBarrier.getNumberWaiting());
while (true) {
try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Service2 implements Runnable {
CyclicBarrier cyclicBarrier;
public Service2(CyclicBarrier cyclicBarrier) {
super();
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println("Services2" + cyclicBarrier.getNumberWaiting());
while (true) {
try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} CountDownLatch의 예
public class Main {
public static void main(String args[]) throws InterruptedException {
ExecutorService executors = Executors.newFixedThreadPool(4);
CountDownLatch latch= new CountDownLatch(2);
executors.submit(new Service1(latch));
executors.submit(new Service2(latch));
latch.await();
System.out.println("Done");
}
}
import java.util.concurrent.CountDownLatch;
public class Service1 implements Runnable {
CountDownLatch latch;
public Service1(CountDownLatch latch) {
super();
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
latch.countDown();
System.out.println("Services2"+latch.getCount());
}
}
import java.util.concurrent.CountDownLatch;
public class Service2 implements Runnable {
CountDownLatch latch;
public Service2(CountDownLatch latch) {
super();
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
latch.countDown();
System.out.println("Services2"+latch.getCount());
}
}