Page History
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
import java.util.concurrent.CompletableFuture; //선언부 @FunctionalInterface interface FuncB { public int calc(int a, int b); } @FunctionalInterface interface FuncA { public int calc(int a); } //사용부 FuncA F1 = (int a) -> a+1; FuncA F2 = (int a) -> a+1; FuncA F3 = (int a) -> a+1; FuncB F4 = (int a,int b) -> a+b; Integer input=1; CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> F1.calc(1) ); CompletableFuture<Integer> futureD = CompletableFuture.supplyAsync(() -> F3.calc(F2.calc(1)) ); CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(futureB, futureD) .thenAccept( r-> { // 최종 연산로직을 비동기로 처리한 케이스 Integer value1 = futureB.join(); Integer value2 = futureD.join(); log( String.format("async result:%d", value1+value2 )); }); // 최종 연산로직을 동기처리로 변환한 예 combinedFuture.get(); Integer sresult = futureB.get() + futureD.get(); log( String.format("sync result:%d",sresult) ); |
여러연산을 결합하기위해서는, 람다식 사용을 강요받습니다.
람다식에 익숙해지면 , 복잡한 비동기 연산처리에대해 간결한 코드로 작성이 가능해집니다.
Graph
일반적인 비동기 조합처리를 위해서 JAVA의 CompletetableFuture 로도 충분하지만
...