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

网站开发 混合式 数据库网络推广渠道排名

网站开发 混合式 数据库,网络推广渠道排名,做钢材的做什么网站效果好,下载网页制作设计编辑器软件背景 CompletableFuture是Java 8中引入的一个类,它实现了Future和CompletionStage接口,用于表示异步计算的结果。使用CompletableFuture可以方便地编写异步编程的代码,并且可以链式地组合多个异步操作。 接口 CompletableFuture实现了Future…

背景

CompletableFuture是Java 8中引入的一个类,它实现了Future和CompletionStage接口,用于表示异步计算的结果。使用CompletableFuture可以方便地编写异步编程的代码,并且可以链式地组合多个异步操作。

接口

CompletableFuture实现了Future接口和CompletionStage接口

public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {//...
}

常用API

CompletableFuture 提供了很多实用的 API 来处理异步计算的结果。以下是 CompletableFuture 的一些常用 API:

supplyAsync

含义:传入一个Supplier,返回一个新的 CompletableFuture

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {return asyncSupplyStage(ASYNC_POOL, supplier);
}public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor) {return asyncSupplyStage(screenExecutor(executor), supplier);
}
runAsync

含义:传入一个Runnable,返回一个新的CompletableFuture

public static CompletableFuture<Void> runAsync(Runnable runnable) {return asyncRunStage(ASYNC_POOL, runnable);
}public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor) {return asyncRunStage(screenExecutor(executor), runnable);
}
thenApply

含义:对于一个已完成的CompletableFuture的返回值进行同步操作

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) {return uniApplyStage(null, fn);
}
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "result_1");
CompletableFuture<String> future2 = future.thenApply(result -> "result->" + result);
System.out.println(future2.get());
result->result_1
thenApplyAsync

含义:对于一个已完成的CompletableFuture的返回值进行异步操作

public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) {return uniApplyStage(defaultExecutor(), fn);
}public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) {return uniApplyStage(screenExecutor(executor), fn);
}
thenAccept

含义:

public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {return uniAcceptStage(null, action);
}
thenAcceptAsync

含义:

public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {return uniAcceptStage(defaultExecutor(), action);
}public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor) {return uniAcceptStage(screenExecutor(executor), action);
}
thenRun

含义:

public CompletableFuture<Void> thenRun(Runnable action) {return uniRunStage(null, action);
}
thenRunAsync

含义:

public CompletableFuture<Void> thenRunAsync(Runnable action) {return uniRunStage(defaultExecutor(), action);
}public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor) {return uniRunStage(screenExecutor(executor), action);
}
thenCombine

含义:

public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn) {return biApplyStage(null, other, fn);
}
thenCombineAsync

含义:

public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn) {return biApplyStage(defaultExecutor(), other, fn);
}public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn, Executor executor) {return biApplyStage(screenExecutor(executor), other, fn);
}

示例

1. 创建CompletableFuture实例:可以通过无参数的构造函数来创建一个表示异步计算结果的CompletableFuture实例。

CompletableFuture<String> future = new CompletableFuture<>();

2. 提交异步任务:可以使用CompletableFuture的静态方法supplyAsync或runAsync来提交异步任务。supplyAsync接受一个Supplier函数式接口,表示异步计算的任务;runAsync接受一个Runnable接口,表示异步执行的任务。这两个方法都可以指定执行异步任务的线程池。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {  // 异步计算任务  return "Hello";  
});  CompletableFuture<Void> voidFuture = CompletableFuture.runAsync(() -> {  // 异步执行任务  
});

完成Future:当异步计算任务完成后,可以使用CompletableFuture的complete方法来完成Future,并设置结果值。如果异步计算任务抛出异常,可以使用completeExceptionally方法来完成Future,并设置异常信息。

future.complete("Hello");  
future.completeExceptionally(new Exception("Error"));

获取结果:可以使用CompletableFuture的get方法来获取异步计算的结果。get方法会阻塞当前线程,直到异步计算完成并返回结果。如果异步计算抛出异常,get方法会抛出ExecutionException异常。如果需要添加超时时间,可以使用get方法的重载版本。

String result = future.get(); // 阻塞当前线程,直到异步计算完成并返回结果

链式组合异步操作:可以使用CompletableFuture的thenApply、thenAccept、thenRun等链式方法来组合多个异步操作。这些方法接受一个函数式接口作为参数,用于处理前一个CompletableFuture的结果。例如,可以使用thenApply方法对前一个CompletableFuture的结果进行转换,并返回一个新的CompletableFuture。

CompletableFuture<String> newFuture = future.thenApply(s -> s.toUpperCase());

http://www.ds6.com.cn/news/41322.html

相关文章:

  • 大学生毕业设计网站南京seo域名
  • 新疆建设兵团第五师网站推广普通话手抄报
  • 网站建设推销拜访客户怎么开头seo链接优化建议
  • wordpress触屏主题网站怎样优化文章关键词
  • wordpress输网址采集太原百度seo排名
  • 怎样做网站图清晰百度云网盘下载
  • 学校网站模板 dedecms推销产品的万能句子
  • 怎么用dw做地图网站惠州百度seo地址
  • wordpress怎么远程保存图片大小网站seo标题优化技巧
  • 网站服务器 要求百度软件商店
  • 西宁网站建设排名长沙专业竞价优化公司
  • 东莞网站建设提供商谷歌 google
  • django 电商网站开发引擎优化搜索
  • 北京喷绘写真广告公司seo教程免费分享
  • 越南网站怎么做有道搜索引擎入口
  • 安卓软件开发需要学什么软件seo基础课程
  • 做电商网站用什么技术菏泽地网站seo
  • 网站备案照相怎么照百度站长平台官网登录入口
  • 做网站还 淘宝关键词优化平台有哪些
  • 网站域名费用有没有免费的seo网站
  • 怎么做动态网站asp长沙谷歌优化
  • 杭州房产网签流程吉林seo刷关键词排名优化
  • 怎么添加字体在wordpressseo岗位工作内容
  • 照明做外贸的有那些网站百度搜索收录
  • 网站关键词优化是什么郑州网站网页设计
  • 网站设计 网站建设网站关键词优化的步骤和过程
  • 怎么给网站做跳转seo优化网站推广专员招聘
  • 珠海网站建设有限公司网页设计大作业
  • 网页设计代码网站东莞做网站推广的公司
  • 织梦网站转跳手机站优秀企业网站模板