便利店网站建设拓扑图,策划书格式模板范文,现在外贸做那个网站好,wordpress一行太宽了CompletableFuture被设计在Java中进行异步编程。异步编程意味着在主线程之外创建一个独立的线程#xff0c;与主线程分隔开#xff0c;并在上面运行一个非阻塞的任务#xff0c;然后通知主线程进展#xff0c;成功或者失败。 一、概述 
1.CompletableFuture和Future的区别与主线程分隔开并在上面运行一个非阻塞的任务然后通知主线程进展成功或者失败。 一、概述 
1.CompletableFuture和Future的区别 
CompletableFuture和Future出现的原因是继承Thread或者实现Runnable接口的异步线程没有返回值需要返回值的异步线程可以通过CompletableFuture和Future来创建。 
CompletableFuture和Future都可以获取到异步线程的返回值但是Future只能通过get()方法阻塞式获取CompletableFuture由于实现了CompletionStage接口可以通过丰富的异步回调方式来执行后续的操作同时还能对多个任务按照先后顺序进行任务编排。 
2.特性 
CompletableFuture的作用主要体现在1异步回调2任务编排 
3.使用场景 
执行比较耗时的操作时尤其是那些依赖一个或多个远程服务的操作使用异步任务可以改善程序的性能加快程序的响应速度使用CompletableFuture类它提供了异常管理的机制让你有机会抛出、管理异步任务执行种发生的异常如果这些异步任务之间相互独立或者他们之间的的某一些的结果是另一些的输入你可以讲这些异步任务构造或合并成一个 
二、原理 
CompletableFuture实现了CompletionStage接口和Future接口前者是对后者的一个扩展增加了异步回调、流式处理、多个Future组合处理的能力使Java在处理多任务的协同工作时更加顺畅便利。 Future接口主要提供get()和join()方法可以获取任务的返回值同时也会阻塞调用线程。 
CompletionStage接口提供了丰富的执行异步调用和任务处理的接口任务之间可以分为存在时序关系包括串行关系、并行关系和汇聚关系等。 
三、实践 
1.创建任务和获取结果 
(1)runAsync创建任务 
通过runAsync()方法创建的异步任务没有返回值其中有有runAsync(Runnable runnable)和runAsync(Runnable runnable, Executor executor)两个重载方法后者比前者多一个Executor executor即可以传入自定义的线程池如果没传即用默认线程池ForkJoinPool.commonPool()。 
ExecutorService executor  Executors.newFixedThreadPool(3);CompletableFutureVoid f1  CompletableFuture.runAsync(new Runnable() {Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(f1 start);}
}, executor); 
(2)supplyAsync创建任务 
通过supplyAsync()方法创建的异步任务有返回值其中有有supplyAsync(Runnable runnable)和supplyAsync(Runnable runnable, Executor executor)两个重载方法后者比前者多一个Executor executor即可以传入自定义的线程池如果没传即用默认线程池ForkJoinPool.commonPool()。 
CompletableFutureString f2  CompletableFuture.supplyAsync(new SupplierString() {Overridepublic String get() {System.out.println(f2 start);try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return f2 return;}
}, executor); 
总结 
runAsync创建的异步任务没有返回值supplyAsync创建的异步任务有返回值。 
(3)获取结果和结束任务 
// 如果完成则返回结果否则就抛出具体的异常
public T    get()
// 最大时间等待返回结果否则就抛出具体异常
public T    get(long timeout, TimeUnit unit)
// 如果完成则返回结果值或抛出任何遇到的异常否则返回默认值。
public T    getNow(T valueIfAbsent)
// 完成时返回结果值否则抛出unchecked异常。为了更好地符合通用函数形式的使用如果完成此 CompletableFuture所涉及的计算引发异常则此方法将引发unchecked异常并将底层异常作为其原因
public T    join()
//如果任务还未完成直接给他返回值置为value
public boolean complete(T value)  
2.异步回调 
(4)whenComplete和whenCompleteAsync 
whenComplete()方法是当某个任务执行完成后执行的回调方法。该方法会将该任务的执行结果或者执行期间抛出的异常传递给回调方法如果是正常执行则异常为null回调方法对应的CompletableFuture的result和是该任务的返回值如果该任务正常执行则get方法返回执行结果如果是执行异常则get方法抛出异常。该任务抛出的异常一般用exceptionally()处理其入参是异常Throwable throwable可以有返回值。 
whenComplete和whenCompleteAsync和区别在于whenComplete是在当前线程中执行该回调任务whenCompleteAsync是会另启动一个线程来执行该回调任务默认情况下是使用ForkJoinPool.commonPool()线程池中的线程也可以设置自定义线程池。后面以-Async为后缀的方法也都是这样的区别。 
CompletableFutureString f7  f2.whenCompleteAsync((result, e) - {System.out.println(f7 start);if (1  1) {throw new IllegalStateException(f7 IllegalStateException);}}
).exceptionally(new FunctionThrowable, String() {Overridepublic String apply(Throwable throwable) {System.out.println(f8 exception:   throwable);return f8 return;}
}); 
(5)handle和handleAsync 
handle方法也是当某个任务执行完成后执行的回调方法整体功能和whenComplete方法差不多但是handleAsync方法会有返回值handleAsync()可以传入自定义线程池。 
CompletableFutureString f5  f2.handleAsync(new BiFunctionString, Throwable, String() {Overridepublic String apply(String s, Throwable throwable) {System.out.println(f5 start);if (1  1) {throw new IllegalStateException(f5 IllegalStateException);}return f5 return;}}, executor).exceptionally(new FunctionThrowable, String() {Overridepublic String apply(Throwable throwable) {System.out.println(f6 exception:   throwable);return f6 return;}
}); 
总结 
whenComplete和handle的区别 
whenComplete的回调方法没有返回值handle方法有返回值 
(6)thenApply和thenApplyAsync 
thenApply 表示某个任务执行完成后执行回调方法会将该任务的执行结果即方法返回值作为入参传递到回调方法中带有返回值。其中 
CompletableFutureString f3  f2.thenApplyAsync(new FunctionString, String() {Overridepublic String apply(String s) {System.out.println(s  ,f3 start);return f3 return;}
}, executor); 
(7)thenAccept和thenAcceptAsync 
thenAccep表示某个任务执行完成后执行的回调方法会将该任务的执行结果即方法返回值作为入参传递到回调方法中无返回值。 
CompletableFutureVoid f3  f2.thenAcceptAsync(new ConsumerString() {Overridepublic void accept(String s) {System.out.println(f3 start);}
}, executor); 
(8)thenRun和thenRunAsync 
thenRun表示某个任务执行完成后执行的动作即回调方法无入参无返回值。 
CompletableFutureVoid f4  f2.thenRunAsync(new Runnable() {Overridepublic void run() {System.out.println(f4 start);}
}, executor); 
异步回调方法总结 
方法入参返回值异常处理whenComplete有入参无返回值能抛出异常handle有入参有返回值能抛出异常thenApply有入参有返回值不能抛出异常thenAccept有入参无返回值不能抛出异常thenRun无入参无返回值不能抛出异常 
3.任务编排 
(9)thenCombine、thenAcceptBoth 和runAfterBoth 
这三个方法都是将两个CompletableFuture组合起来处理只有两个任务都正常完成时才进行下阶段任务。可以理解为为两个任务AND汇聚后才能执行。 
区别thenCombine会将两个任务的执行结果作为所提供函数的参数且该方法有返回值thenAcceptBoth同样将两个任务的执行结果作为方法入参但是无返回值runAfterBoth没有入参也没有返回值。注意两个任务中只要有一个执行异常则将该异常信息作为指定任务的执行结果。 ExecutorService executor  Executors.newFixedThreadPool(3);CompletableFutureString f1  CompletableFuture.supplyAsync(new SupplierString() {Overridepublic String get() {System.out.println(f1 running);try {Thread.sleep(6000);} catch (InterruptedException e) {e.printStackTrace();}return f1 return;}}, executor);CompletableFutureString f2  CompletableFuture.supplyAsync(new SupplierString() {Overridepublic String get() {System.out.println(f2 running);try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return f2 return;}}, executor);CompletableFutureString f30  CompletableFuture.supplyAsync(new SupplierString() {Overridepublic String get() {System.out.println(f30 running);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return f30 return;}}, executor);CompletableFutureString f3  f1.thenCombine(f2, new BiFunctionString, String, String() {Overridepublic String apply(String s, String s2) {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(s  ,  s2  ,  f3 running);return f3 ruturn;}});CompletableFutureVoid f4  f1.thenAcceptBoth(f2, new BiConsumerString, String() {Overridepublic void accept(String s, String s2) {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(s  ,  s2  ,  f4 running);}}); 
(10)applyToEither、acceptEither和runAfterEither 
这三个方法和上面一样也是将两个CompletableFuture组合起来处理当有一个任务正常完成时就会进行下阶段任务。可以理解为两个任务OR汇聚后才能执行。 
区别applyToEither会将已经完成任务的执行结果作为所提供函数的参数且该方法有返回值acceptEither同样将已经完成任务的执行结果作为方法入参但是无返回值runAfterEither没有入参也没有返回值。 
CompletableFutureString f6  f1.applyToEither(f2, new FunctionString, String() {Overridepublic String apply(String s) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(s  ,  f6 running);return return f6;}});CompletableFutureVoid f7  f1.acceptEither(f2, new ConsumerString() {Overridepublic void accept(String s) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(s  ,  f7 running);}
});CompletableFutureVoid f8  f1.runAfterEither(f2, new Runnable() {Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(f7 running);}
}); 
(11)allOf / anyOf 
allOf和anyOf都是CompletableFuture的方法他们针对的都是多任务汇聚后才能执行的逻辑可以理解为多任务AND/OR汇聚后模式。 
allOfCompletableFuture是多个任务都执行完成后才会执行只有有一个任务执行异常则返回的CompletableFuture执行get方法时会抛出异常如果都是正常执行则get返回null。 
anyOf CompletableFuture是多个任务只要有一个任务执行完成则返回的CompletableFuture执行get方法时会抛出异常如果都是正常执行则get返回执行完成任务的结果。 
CompletableFutureVoid f9  CompletableFuture.allOf(f1, f2, f30);
CompletableFutureObject f10  CompletableFuture.anyOf(f1, f2, f30); 
任务编排方法总结 
类型方法入参返回值描述两个任务AND汇聚thenCombine有入参有返回值两个任务AND汇聚thenAcceptBoth有入数无返回值两个任务AND汇聚runAfterBoth无入参无返回值两个任务OR汇聚applyToEither有入参有返回值两个任务OR汇聚acceptEither有入参无返回值两个任务OR汇聚runAfterEither无入参无返回值多任务AND汇聚后模式allOf全部执行多任务OR汇聚后模式anyOf至少一个执行 
总结以上方法 
以Async结尾的方法都是异步方法对应的没有Async则是同步方法一般都是一个异步方法对应一个同步方法以Async后缀结尾的方法都有两个重载的方法一个是使用内容的forkjoin线程池一种是使用自定义线程池以Apply开头或者结尾的方法入口有参数有返回值以supply开头的方法入口也是没有参数的但是有返回值以Accept开头或者结尾的方法入口参数是有参数但是没有返回值以run开头的方法其入口参数一定是无参的并且没有返回值类似于执行Runnable方法。和异步方法相比任务编排方法多数带有-Both或-Either的后缀-Both表示需要执行全部任务-Either表示至少执行一个任务。 
4.代码实现 
参考资料 
CompletableFuture使用详解全网看这一篇就行:https://blog.csdn.net/zsx_xiaoxin/article/details/123898171 主要参考 CompletableFuture使用大全简单易懂https://juejin.cn/post/6844904195162636295 CompletableFuture用法详解https://zhuanlan.zhihu.com/p/344431341 并发编程系列-CompletableFuturehttps://zhuanlan.zhihu.com/p/650700731 Java CompletableFuture实现多线程异步编排https://juejin.cn/post/7140244126679138312 使用CompletableFuturehttps://www.liaoxuefeng.com/wiki/1252599548343744/1306581182447650 并发编程 - CompletableFuture 解析 | 京东物流技术团队:https://zhuanlan.zhihu.com/p/646472720  本文由博客一文多发平台 OpenWrite 发布 
 文章转载自: http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn