JAVA/Java Stream

Java 1.8 CompletableFuture 모든 동작 완료 확인

lovineff 2020. 6. 4. 17:41
public static void main(String[] args) throws ExecutionException, InterruptedException {
        final int num = 100;

        CompletableFuture cf1 = CompletableFuture.completedFuture(num)
                .thenApply(sNum -> {
                    System.out.println("first start");
                    return sNum + 100;
                }).thenAcceptAsync(aNum ->{
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("first end");
                });

        CompletableFuture cf2 = CompletableFuture.completedFuture(num)
                .thenApply(sNum -> {
                    System.out.println("second start");
                    return sNum;
                }).thenAcceptAsync(aNum ->{
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("second end");
                });

        CompletableFuture.allOf(cf1, cf2).join();
        System.out.println("all end");
    }

'JAVA > Java Stream' 카테고리의 다른 글

Map 반복문  (0) 2021.04.12
Map Parallel (Map 병렬처리) 방법  (0) 2021.03.30
Stream 객체 값 변경 예제  (0) 2020.11.30
Stream 함수  (0) 2020.06.04
JAVA Stream 기본  (0) 2020.06.04