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 resultsresult = futureB.get() + futureD.get(); System.out.println(result log( String.format("sync result:%d",sresult) ); |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
//Labda Func<int, int> F1 = x => x + 1; Func<int, int> F2 = x => x + 1; Func<int, int> F3 = x => x + 1; Func<int, int, int> F4 = delegate ( int x, int y ){ return x + y; }; //Block with task Task<int> futureA = Task.Factory.StartNew<int>(() => F1(1)); int c = F2(1); int d = F3(c); int f = F4(futureA.Result, d); Console.WriteLine("ResultA:" + f); //Continuation Tasks var futureB = Task.Factory.StartNew<int>(() => F1(1)); var futureD = Task.Factory.StartNew<int>(() => F3(F2(1))); var futureF = Task.Factory.ContinueWhenAll<int, int>( new[] { futureB, futureD }, ( tasks ) => F4(futureB.Result, futureD.Result)); futureF.ContinueWith(( t ) => Console.WriteLine("ResultF:" + t.Result) ); Console.WriteLine("Code...End"); |
...