广州个人网站搭建天津招标信息网
news/
2025/9/30 16:12:14/
文章来源:
广州个人网站搭建,天津招标信息网,站长工具高清,无极在线招工招聘信息前面两篇文章已经整理了CompletableFuture大部分的特性#xff0c;本文会整理完CompletableFuture余下的特性#xff0c;以及将它跟RxJava进行比较。 3.6 Either Either 表示的是两个CompletableFuture#xff0c;当其中任意一个CompletableFuture计算完成的时候就会执行。 …前面两篇文章已经整理了CompletableFuture大部分的特性本文会整理完CompletableFuture余下的特性以及将它跟RxJava进行比较。 3.6 Either Either 表示的是两个CompletableFuture当其中任意一个CompletableFuture计算完成的时候就会执行。 方法名描述acceptEither(CompletionStage? extends T other, Consumer? super T action)当任意一个CompletableFuture完成的时候action这个消费者就会被执行。acceptEitherAsync(CompletionStage? extends T other, Consumer? super T action)当任意一个CompletableFuture完成的时候action这个消费者就会被执行。使用ForkJoinPoolacceptEitherAsync(CompletionStage? extends T other, Consumer? super T action, Executor executor)当任意一个CompletableFuture完成的时候action这个消费者就会被执行。使用指定的线程池 Random random new Random();CompletableFutureString future1 CompletableFuture.supplyAsync(()-{try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future1;});CompletableFutureString future2 CompletableFuture.supplyAsync(()-{try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future2;});CompletableFutureVoid future future1.acceptEither(future2,str-System.out.println(The future is str));try {future.get();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}复制代码执行结果The future is from future1 或者 The future is from future2。因为future1和future2执行的顺序是随机的。 applyToEither 跟 acceptEither 类似。 方法名描述applyToEither(CompletionStage? extends T other, Function? super T,U fn)当任意一个CompletableFuture完成的时候fn会被执行它的返回值会当作新的CompletableFutureU的计算结果。applyToEitherAsync(CompletionStage? extends T other, Function? super T,U fn)当任意一个CompletableFuture完成的时候fn会被执行它的返回值会当作新的CompletableFutureU的计算结果。使用ForkJoinPoolapplyToEitherAsync(CompletionStage? extends T other, Function? super T,U fn, Executor executor)当任意一个CompletableFuture完成的时候fn会被执行它的返回值会当作新的CompletableFutureU的计算结果。使用指定的线程池 Random random new Random();CompletableFutureString future1 CompletableFuture.supplyAsync(()-{try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future1;});CompletableFutureString future2 CompletableFuture.supplyAsync(()-{try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future2;});CompletableFutureString future future1.applyToEither(future2,str-The future is str);try {System.out.println(future.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}复制代码执行结果也跟上面的程序类似。 3.7 其他方法 allOf、anyOf是CompletableFuture的静态方法。 3.7.1 allOf 方法名描述allOf(CompletableFuture?... cfs)在所有Future对象完成后结束并返回一个future。allOf()方法所返回的CompletableFuture并不能组合前面多个CompletableFuture的计算结果。于是我们借助Java 8的Stream来组合多个future的结果。 CompletableFutureString future1 CompletableFuture.supplyAsync(() - tony);CompletableFutureString future2 CompletableFuture.supplyAsync(() - cafei);CompletableFutureString future3 CompletableFuture.supplyAsync(() - aaron);CompletableFuture.allOf(future1, future2, future3).thenApply(v -Stream.of(future1, future2, future3).map(CompletableFuture::join).collect(Collectors.joining( ))).thenAccept(System.out::print);复制代码执行结果 tony cafei aaron复制代码3.7.2 anyOf 方法名描述anyOf(CompletableFuture?... cfs)在任何一个Future对象结束后结束并返回一个future。 Random rand new Random();CompletableFutureString future1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(rand.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future1;});CompletableFutureString future2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(rand.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future2;});CompletableFutureString future3 CompletableFuture.supplyAsync(() - {try {Thread.sleep(rand.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return from future3;});CompletableFutureObject future CompletableFuture.anyOf(future1,future2,future3);try {System.out.println(future.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}复制代码使用anyOf()时只要某一个future完成就结束了。所以执行结果可能是from future1、from future2、from future3中的任意一个。 anyOf 和 acceptEither、applyToEither的区别在于后两者只能使用在两个future中而anyOf可以使用在多个future中。 3.8 CompletableFuture异常处理 CompletableFuture在运行时如果遇到异常可以使用get()并抛出异常进行处理但这并不是一个最好的方法。CompletableFuture本身也提供了几种方式来处理异常。 3.8.1 exceptionally 方法名描述exceptionally(Function fn)只有当CompletableFuture抛出异常的时候才会触发这个exceptionally的计算调用function计算值。 CompletableFuture.supplyAsync(() - hello world).thenApply(s - {s null;int length s.length();return length;}).thenAccept(i - System.out.println(i)).exceptionally(t - {System.out.println(Unexpected error: t);return null;});复制代码执行结果 Unexpected error:java.util.concurrent.CompletionException: java.lang.NullPointerException复制代码对上面的代码稍微做了一下修改修复了空指针的异常。 CompletableFuture.supplyAsync(() - hello world).thenApply(s - {
// s null;int length s.length();return length;}).thenAccept(i - System.out.println(i)).exceptionally(t - {System.out.println(Unexpected error: t);return null;});复制代码执行结果 11复制代码3.8.2 whenComplete whenComplete 在上一篇文章其实已经介绍过了在这里跟exceptionally的作用差不多可以捕获任意阶段的异常。如果没有异常的话就执行action。 CompletableFuture.supplyAsync(() - hello world).thenApply(s - {s null;int length s.length();return length;}).thenAccept(i - System.out.println(i)).whenComplete((result, throwable) - {if (throwable ! null) {System.out.println(Unexpected error:throwable);} else {System.out.println(result);}});复制代码执行结果 Unexpected error:java.util.concurrent.CompletionException: java.lang.NullPointerException复制代码跟whenComplete相似的方法是handlehandle的用法在上一篇文章中也已经介绍过。 四. CompletableFuture VS Java8 Stream VS RxJava1 RxJava2 CompletableFuture 有很多特性跟RxJava很像所以将CompletableFuture、Java 8 Stream和RxJava做一个相互的比较。 composablelazyresuableasynccachedpushback pressureCompletableFuture支持不支持支持支持支持支持不支持Stream支持支持不支持不支持不支持不支持不支持Observable(RxJava1)支持支持支持支持支持支持支持Observable(RxJava2)支持支持支持支持支持支持不支持Flowable(RxJava2)支持支持支持支持支持支持支持五. 总结 Java 8提供了一种函数风格的异步和事件驱动编程模型CompletableFuture它不会造成堵塞。CompletableFuture背后依靠的是fork/join框架来启动新的线程实现异步与并发。当然我们也能通过指定线程池来做这些事情。 CompletableFuture特别是对微服务架构而言会有很大的作为。举一个具体的场景电商的商品页面可能会涉及到商品详情服务、商品评论服务、相关商品推荐服务等等。获取商品的信息时/productdetails?productidxxx需要调用多个服务来处理这一个请求并返回结果。这里可能会涉及到并发编程我们完全可以使用Java 8的CompletableFuture或者RxJava来实现。 先前的文章Java8新的异步编程方式 CompletableFuture(一)Java8新的异步编程方式 CompletableFuture(二)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/923049.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!