当前位置: 首页 > 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.zhongyajixie.com/news/25251.html

相关文章:

  • 客户关系管理系统简称怎样优化网络
  • 如何更改网站的关键词百度app下载安装普通下载
  • 做搜狗网站优化点seow
  • 怎么做一个动态网站吗各种资源都有的搜索引擎
  • 做网站还要维护吗宽带营销策略
  • 做电脑租赁网站360网站排名优化
  • php完整网站开发源码自媒体营销
  • 东莞哪里有做企业网站的网站建设优化400报价
  • 网站开发参考文献2015年后广州最新发布最新
  • 买域名和服务器做自己的网站网络优化工程师需要学什么
  • 一个人建设小型网站长沙seo网站排名
  • 南充哪里做网站搜索引擎的网站
  • 杭州酒店团购网站建设晋城网站seo
  • 怎么把自己做的网站挂到外网上智能识别图片
  • 淘宝的网站架构关键词排名顾问
  • 知名设计公司网站会员制营销
  • html网站开发基础每日关键词搜索排行
  • 网站综合开发怎么做无锡网站制作优化
  • 做网站菜单背景图片奉化网站关键词优化费用
  • 个人网站设计作品展示目前最牛的二级分销模式
  • 上海近期大型招聘会seo优化推广教程
  • 真人做爰直播视频网站企业网络营销系统分析报告
  • 亚马逊 网站建设站长之家关键词挖掘工具
  • 杭州网站建站平台网络营销策划的概念
  • 常州哪些网站公司做的好处免费seo工具大全
  • 什么平台可以做网站南京seo网络推广
  • 主机宝怎么设置网站主页企业网站的搜索引擎推广与优化
  • 赚钱网站入口谷歌seo搜索
  • 专门做鞋子的网站吗企业网站建设价格
  • 怎样做自己的微商网站百度优化seo