java 异步编程( 三 )


public class CompletableFutureBlog2 {public static void main(String[] args) throws ExecutionException, InterruptedException {final Stopwatch started = Stopwatch.createStarted();final CompletableFuture<Integer> ret = CompletableFuture.supplyAsync(() -> {System.out.println("开始进行耗时的异步计算,消耗 3 秒");try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return 10;});final Integer integer = ret.get();System.out.println(StrUtil.format("异步执行的结果: {}", integer));System.out.println("执行时间: " + started.elapsed(TimeUnit.SECONDS));}}thenApplyAsync 、thenAcceptAsync 和 thenRunAsyncthenXX 都是为了在上一个异步计算的结束之后执行 。
我们对异步计算的结果分为以下几个情况:

  • 需要依赖异步计算的结果,并且依赖异步计算的结果计算返回另个一个结果 thenApplyAsync
  • 依赖异步计算的结果,但是不会产生新的结果,thenAcceptAsync
  • 不依赖计算计算的结果,并且没有返回值 thenRunAsync
public class CompletableFutureBlog3 {public static void main(String[] args) throws ExecutionException, InterruptedException {final Stopwatch started = Stopwatch.createStarted();final CompletableFuture<Integer> ret = CompletableFuture.supplyAsync(() -> {System.out.println("开始进行耗时的异步计算,消耗 3 秒");try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return 10;});final Integer result = ret.thenApplyAsync(data -> {System.out.println("依赖上一个异步计算,消耗 5 秒");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}return data + 12;}).get();System.out.println(StrUtil.format("异步执行的结果: {}", result));System.out.println("执行时间: " + started.elapsed(TimeUnit.SECONDS));}}thenCombineAsync结合另一个 CompletableFuture 异步计算,当两个异步计算执行完了,执行回调 。
计算一个耗时的计算 。将这个耗时计算拆成两个耗时的异步计算,当两个异步计算结束,在合并最终的结果
public class CompletableFutureBlog4 {public static void main(String[] args) throws ExecutionException, InterruptedException {final Stopwatch started = Stopwatch.createStarted();final CompletableFuture<Integer> ret1 = CompletableFuture.supplyAsync(() -> {System.out.println("开始进行耗时的异步计算,消耗 3 秒");try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return 10;});final CompletableFuture<Integer> ret2 = CompletableFuture.supplyAsync(() -> {System.out.println("开始进行耗时的异步计算,消耗 5 秒");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}return 10;});final CompletableFuture<Integer> integerCompletableFuture = ret2.thenCombineAsync(ret1, (result1, result2) -> result1 + result2);final Integer result = integerCompletableFuture.get();System.out.println(StrUtil.format("异步执行的结果: {}", result));System.out.println("执行时间: " + started.elapsed(TimeUnit.SECONDS));}}allOf 和 anyOf可以组合多个 CompletableFuture ,当每个 CompletableFuture 都执行完,执行后续逻辑 。
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {return andTree(cfs, 0, cfs.length - 1);}可以组合多个 CompletableFuture ,当任何一个 CompletableFuture 都执行完,执行后续逻辑 。
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {return orTree(cfs, 0, cfs.length - 1);}future,future2,future3 执行完之后,再执行后续逻辑 。
public class CompletableFutureBlog5 {public static void main(String[] args) throws ExecutionException, InterruptedException {final Stopwatch started = Stopwatch.createStarted();final CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(1);});final CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(1);});final CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {try {TimeUnit.SECONDS.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(1);});final CompletableFuture<Void> future1 = CompletableFuture.allOf(future3, future2, future);future1.get();System.out.println("执行时间: " + started.elapsed(TimeUnit.SECONDS));}}


推荐阅读