Executors, thread pool, CountDownLatch


Executors, thread pool, CountDownLatch

여러 쓰레드를 동시에 구동하고 모두 완료될때 까지 기다리는 작업을 해야 할 경우가 자주 있습니다. 즉, 5개 rest api 를 병렬로 호출하고, 응답이 모두 올때까지 기다린 후 모든 응답이 왔을 때 , 다음 로직을 수행하고 싶을때가 많습니다. 이 경우 java 의 #executors 를 이용한 thread pool 을 생성하고, 모든 작업이 완료되었는지를 확인하기 위한 용도로 #CountDownLatch 를 쓰면 좋습니다. 아래 코드를 보면 쉽게 이해가 가능합니다. import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @Slf4j public class ThreadTest { @Test void test() throws InterruptedException { int totalEvent = 15; int th...


#CountDownLatch #executors

원문링크 : Executors, thread pool, CountDownLatch