용어
Client
: 메서드, 함수 혹은 요청을 하는 측Server
: 메서드, 함수를 수행하고 요청에 따른 응답을 내려주는 측Blocking, Non-Blocking
Blocking
Server
가 자신이 할 일을 모두 마칠 때 까지 제어권을 계속 가지고Client
에게 바로 돌려주지 않음
Non-Blocking
Server
는 자신이 할 일을 모두 마치지 않더라도 바로 제어권을 건네주어Client
가 다른 일을 진행할 수 있도록 해줌
Synchronous, Asynchronous
Synchronous
Server
의 수행 결과 및 종료를 (Server
와 더불어)Client
가 신경씀
Asynchronous
Server
의 수행 결과 및 종료를Server
혼자 직접 신경 쓰고 처리한다면 (~ callback 함수)

참고)
Code Example
import lombok.extern.slf4j.Slf4j; import java.util.concurrent.CompletableFuture; @Slf4j public class Boss { public String sync_hire() { log.info("오케이, 잠깐만 거기 계세요!"); sleep(2000); log.info("채용 공고 등록 .."); sleep(1000); log.info("지원자 연락 .."); sleep(1000); log.info("면접 진행 .."); sleep(1000); log.info("연봉 협상 .."); sleep(1000); log.info("끝!"); return "득윤"; } public CompletableFuture<String> async_hire() { return CompletableFuture.supplyAsync(() -> { log.info("오케이, 잠깐만 거기 계세요!"); sleep(2000); log.info("채용 공고 등록 .."); sleep(1000); log.info("지원자 연락 .."); sleep(1000); log.info("면접 진행 .."); sleep(1000); log.info("연봉 협상 .."); sleep(1000); log.info("끝!"); return "득윤"; }); } private static void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Slf4j class Me { ExecutorService es = Executors.newFixedThreadPool(2); Boss boss = new Boss(); @Test void 동기_블록킹() { String result = boss.sync_hire(); log.info("result = {}", result); } @Test void 비동기_블록킹() { CompletableFuture<String> future = boss.async_hire(); log.info("아직 최종 데이터를 전달 받지는 않았지만, 다른 작업 수행 가능"); String result = future.join(); //블록킹 log.info("result 전달 받음 : {}", result); } @Test void 비동기_콜백_반환없음() { CompletableFuture<Void> future = boss.async_hire() .thenAccept(result -> log.info("콜백, result : " + result)); log.info("아직 최종 데이터를 전달 받지는 않았지만, 다른 작업 수행 가능, 논블록킹"); assertThat(future.join()).isNull(); } @Test void 비동기_콜백_반환있음() { CompletableFuture<Void> future = boss.async_hire() .thenApply(result -> { log.info("bossThread"); return "득윤득윤"; }) .thenAccept(result -> log.info("콜백, result : {}", result)); log.info("아직 최종 데이터를 전달 받지는 않았지만, 다른 작업 수행 가능"); assertThat(future.join()).isNull(); } @Test void 비동기_콜백_반환있음_쓰레드_지정() { CompletableFuture<Void> future = boss.async_hire() .thenApplyAsync(result -> { log.info("es thread"); return "득윤득윤"; },es) .thenAcceptAsync(result -> log.info("콜백, result : {}", result),es); log.info("아직 최종 데이터를 전달 받지는 않았지만, 다른 작업 수행 가능"); assertThat(future.join()).isNull(); } }