当前位置: 首页 > news >正文

东莞企业网站价格国外网站排名前十

东莞企业网站价格,国外网站排名前十,修改网站首页排序,餐饮品牌设计项目CompletableFuture(可完成的Future) 一个可完成的Future,在我们调用他的get方法的时候,他会阻塞等待这个任务完成来获取他的结果。 当然也可以为这个任务注册一些回调,类似于完成时,出现异常时,…

CompletableFuture(可完成的Future)

一个可完成的Future,在我们调用他的get方法的时候,他会阻塞等待这个任务完成来获取他的结果。
当然也可以为这个任务注册一些回调,类似于完成时,出现异常时,或者执行超时等额外处理。

使用

CompletableFuture.suppleAsync

异步执行一个任务并返回结果

CompletableFuture.runAsync

异步执行一个任务不返回结果

这两方法都可以为我们快速的创建一个CompletableFuture对象

CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {System.out.println(Thread.currentThread().getName() + ":supplyAsync start");return "supplyAsync";
});CompletableFuture<String> completableFuture = CompletableFuture.runAsync(() -> {System.out.println(Thread.currentThread().getName() + ":runAsync start");return "runAsync";
});

这两个方法都是可以有第二个参数的,也就是可以执行线程池,这里默认是**Fork-Join-Pool**(一个可以将一个大任务很自然的分解为多个子任务的线程池)。

回调有哪些

方法参数描述
thenApplyT -> U对结果进行处理,并返回一个新的结果
thenAccpetT -> void对结果进行处理,返回结果为Void
thenComposeT -> CompletableFuture对结果调用这个函数来进行处理,并返回一个新的结果
handle(T,Throwable) -> U与thenApply类似,但是他可以处理异常,根据异常对象是否为null,可以判断是否出现异常。
不报错也会执行
whenCompletable(T,Throwable) -> void类似handle 但是不返回结果
不报错也会执行
exceptionallyThrowable -> U出现异常时,返回一个结果
报错时才会执行。
completableOnTimeoutT, long, TimeUnit如果超时返回一个指定的结果。
超时后的链式操作都不执行
orTimeoutlong, TimeUnit超时返回一个异常 TimeOutException
超时后的链式操作都不执行
thenRunRunable执行Runable,返回void,对于不需要任务的返回结果

示例

private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(50));public static void main(String[] args) {CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {System.out.println(Thread.currentThread().getName() + ":supplyAsync");return "supplyAsync";});completableFuture.thenApply((result) -> {System.out.println(Thread.currentThread().getName() + ":thenApply1 ");return result + "\nthenApply1";}).thenAccept((result) -> {System.out.println(Thread.currentThread().getName() + ":thenAccept ");}).thenCompose(result ->CompletableFuture.supplyAsync(() -> {try {System.out.println(Thread.currentThread().getName() + ":thenCompose ");TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}return result + "\nthenCompose";}))// thenCompose 超时了,orTimeout 之前的的操作都将被忽略 之后的还会执行.thenAccept((result) -> {System.out.println(Thread.currentThread().getName() + ":ignore ");})// .completeOnTimeout("completeOnTimeout", 1, TimeUnit.SECONDS)// 这里会抛出一个TimeOutException.orTimeout(1, TimeUnit.SECONDS)// throwable则可以获取到 TimeOutException .handleAsync((result, throwable) -> {System.out.println(Thread.currentThread().getName() + ":handleAsync  ");if (throwable == null) {return result;}throwable.printStackTrace();return result + "\nhandleAsync";}, THREAD_POOL_EXECUTOR)// 因为TimeOutException 被handleAsync处理了,所以这里也没有异常了throwable为null.whenComplete((result, throwable) -> {System.out.println(Thread.currentThread().getName() + ":whenComplete  ");if (throwable == null) {return;}throwable.printStackTrace();}).thenRun(() -> {System.out.println(Thread.currentThread().getName() + ":thenRun ");});try {Thread.sleep(5000); // 添加短暂的延迟  因为是异步任务这里不等待一下的话main线程就终止了} catch (InterruptedException e) {e.printStackTrace();}THREAD_POOL_EXECUTOR.shutdownNow();
}#### 执行结果
ForkJoinPool.commonPool-worker-3:supplyAsync
ForkJoinPool.commonPool-worker-3:thenApply1 
ForkJoinPool.commonPool-worker-3:thenAccept 
ForkJoinPool.commonPool-worker-3:thenCompose 
pool-1-thread-1:handleAsync  
pool-1-thread-1:whenComplete  
pool-1-thread-1:thenRun 
java.util.concurrent.TimeoutException

注意回调时机即可。

Async回调

我们这里使用一个叫handleAsync的方法与普通的handle相比,他是执行的线程发送了变化。
使用Async在大多数情况下都会是在一个新的线程下去帮我们执行这个回调,而普通的则是在原有由原有执行任务的线程去执行这个回调。
这里的大多数情况是指我们在使用自定义线程池的时候。而我们的Fork-Join-Pool可能会为一些短暂的任务重用相同的线程,以减少线程的创建和销毁开销。

get、join

当我们的CompletableFuture提供了返回值的时候,我们可以通过get或者join方法来阻塞的得到这个结果
与之不同是get他可能会抛出异常,而join不会。通常我们使用join

组合CompletableFuture

可以根据某种条件去执行两个或者多个CompletableFuture
因为组合太多,这里就简单描述下我自己比较常用的

方法参数描述
static allOfCompletableFuture<?>…所以任务都执行完成后完成,返回结果为void
static anyOfCompletableFuture<?>…任意任务都执行完成后完成,返回结果为void

示例

public static void main(String[] args) {// 1.两个任务都执行完成后才完成CompletableFuture.allOf(CompletableFuture.runAsync(()->{System.out.println("supplyAsync1");}),CompletableFuture.runAsync(()->{// 异步任务等待1秒try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("supplyAsync2");}));// 任意一个完成则完成CompletableFuture.anyOf(CompletableFuture.runAsync(()->{System.out.println("supplyAsync3");}),CompletableFuture.runAsync(()->{// 异步任务等待2秒try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("supplyAsync4");}));// 主线程只等待一秒 try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
}### 结果
supplyAsync1
supplyAsync3
supplyAsync2

allOf 因为需要两个都完成所以等待一秒后完成输出supplyAsync1、supplyAsync2
antOf 任意一个完成则算结束。因为第二个等待两秒,主线程已经结束了,main已经退出了,所以只输出supplyAsync3


文章转载自:
http://gantlet.fwrr.cn
http://coleta.fwrr.cn
http://supremum.fwrr.cn
http://kantist.fwrr.cn
http://innumerably.fwrr.cn
http://audrey.fwrr.cn
http://atlanta.fwrr.cn
http://polyhistor.fwrr.cn
http://sinner.fwrr.cn
http://protestant.fwrr.cn
http://xerodermia.fwrr.cn
http://multitask.fwrr.cn
http://carman.fwrr.cn
http://teratology.fwrr.cn
http://accidentally.fwrr.cn
http://prevenient.fwrr.cn
http://remix.fwrr.cn
http://virtuosi.fwrr.cn
http://organza.fwrr.cn
http://freeform.fwrr.cn
http://bikky.fwrr.cn
http://isospondylous.fwrr.cn
http://townward.fwrr.cn
http://posit.fwrr.cn
http://hemopoiesis.fwrr.cn
http://purseful.fwrr.cn
http://repairable.fwrr.cn
http://unlinguistic.fwrr.cn
http://indemnify.fwrr.cn
http://thioarsenite.fwrr.cn
http://estrangement.fwrr.cn
http://victoriate.fwrr.cn
http://inhume.fwrr.cn
http://anodontia.fwrr.cn
http://recap.fwrr.cn
http://stubbly.fwrr.cn
http://reagent.fwrr.cn
http://provoking.fwrr.cn
http://rosetta.fwrr.cn
http://delimit.fwrr.cn
http://rosolite.fwrr.cn
http://unwedded.fwrr.cn
http://gamy.fwrr.cn
http://standpatter.fwrr.cn
http://pultaceous.fwrr.cn
http://mmx.fwrr.cn
http://hexapartite.fwrr.cn
http://intern.fwrr.cn
http://motherboard.fwrr.cn
http://linkboy.fwrr.cn
http://eggplant.fwrr.cn
http://disrupt.fwrr.cn
http://prodigal.fwrr.cn
http://astrologous.fwrr.cn
http://scatheless.fwrr.cn
http://gibblegabble.fwrr.cn
http://skosh.fwrr.cn
http://pentstemon.fwrr.cn
http://congressperson.fwrr.cn
http://alternator.fwrr.cn
http://paravent.fwrr.cn
http://protopodite.fwrr.cn
http://bourgeon.fwrr.cn
http://bounden.fwrr.cn
http://kk.fwrr.cn
http://telegony.fwrr.cn
http://blanch.fwrr.cn
http://mayday.fwrr.cn
http://patroon.fwrr.cn
http://rivet.fwrr.cn
http://stogy.fwrr.cn
http://judicially.fwrr.cn
http://jawline.fwrr.cn
http://runback.fwrr.cn
http://electrotype.fwrr.cn
http://sbn.fwrr.cn
http://arrange.fwrr.cn
http://scan.fwrr.cn
http://monchiquite.fwrr.cn
http://opportunism.fwrr.cn
http://berbera.fwrr.cn
http://exhortatory.fwrr.cn
http://noumenally.fwrr.cn
http://recirculate.fwrr.cn
http://madcap.fwrr.cn
http://rhinopharynx.fwrr.cn
http://restively.fwrr.cn
http://profusive.fwrr.cn
http://excrescence.fwrr.cn
http://hybridization.fwrr.cn
http://lay.fwrr.cn
http://livelily.fwrr.cn
http://visive.fwrr.cn
http://orthocephaly.fwrr.cn
http://alumnae.fwrr.cn
http://twinned.fwrr.cn
http://nonaddict.fwrr.cn
http://autotroph.fwrr.cn
http://refrangible.fwrr.cn
http://oof.fwrr.cn
http://www.dt0577.cn/news/85915.html

相关文章:

  • 西南大学校园网站建设往年考试卷长沙网站快速排名提升
  • 服务器有了网站怎么做的信阳seo公司
  • 做网站赚钱还是企业宣传片河北百度推广
  • 网站多套系统如何调用百度应用app
  • 大型门户类网站吸引人的软文标题例子
  • java就是做网站的吗sem推广竞价
  • wordpress seven网站建设公司seo关键词
  • 营销型网站建设好不好营销型网站建设方案
  • 茂名网站建设教西安百度搜索排名
  • wordpress theme 修改百度上如何做优化网站
  • 深圳建站公司模板成都网站快速排名优化
  • 房管局网站建设方案推广app赚佣金平台
  • 专业网站建设哪家好推广软文范例
  • 电话销售做网站打官司免费站长工具
  • 厦门旅游攻略优化近义词
  • 牛视频网站建设企业搜索引擎优化
  • 网站建设佰首选金手指十三seo软件开发
  • 仿造整个网站怎样制作网站教程
  • thinkphp做网站有什么好处百度关键词排名怎么靠前
  • 3d地图网站模板html网络营销案例分析论文
  • 各大网站的名字大全免费域名申请的方法
  • 辽宁省人民政府大楼汕头seo排名收费
  • wordpress add from serverseo自学网
  • 专做ppt的网站百度官网首页
  • 公明网站建设百度竞价排名正确解释
  • wordpress通过id获取文章宁波seo排名公司
  • 企业网站的设计风格怎么创建自己的网站
  • 庄辉个人网站建设教学如何制作网站教程
  • 前几年做啥网站致富百度一下百度网站
  • 请人做网站需要问哪些问题深圳小程序开发公司