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.join(); //연산을 비동기적으로 시작하려면 join명령을 사용합니다.
// 최종 연산로직을 동기처리로 변환한 예
combinedFuture.get(); //동기처리로 진행하려면 get을 사용합니다.
Integer sresult = futureB.get() + futureD.get();
log( String.format("sync result:%d",sresult) ); |
...