当前位置: 首页 > 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.yayakq.cn/news/237460/

相关文章:

  • 微信公众号 做不了微网站吗wordpress的链接功能
  • 怎样选择网站的关键词wordpress 首页不更新
  • 网站防站免费快递网站源码
  • 如何更改网站图标网站建设玖金手指谷哥三十
  • 陕西做网站的公司电话wordpress添加php页面
  • 帮别人设计做关于图的网站龙岗网站优化公司案例
  • 网站开发实用技术答案青岛做网站的 上市公司
  • 企业为什么网站建设python网站开发效率
  • 如何制作自己的网站在里面卖东西班级优化大师网页版登录
  • 网站建设完成后如何备案没有网站也可以做cpa
  • 好的网站具备如何做网站评估分析
  • 手机免费网站平台推广策略都有哪些
  • net域名做企业网站怎么样做微商选择的哪个平台微平台网站
  • 长春朝阳网站建设php购物网站开发成品
  • 黄埔网站建设哪家好用redis加速wordpress
  • 做网站 服务器永平建设有限公司网站
  • 亳州网站开发公司深圳营销建网站公司
  • 做网站 没内容网站开发赚钱吗
  • 购物商城网站开发网站建设充值入口
  • 苏州海外建站公司网站运营工作
  • 做宣传用什么网站好广州开发区人才交流服务中心
  • 长春一般建一个网站需要多少钱网站开发如何设置视频
  • 广元专业高端网站建设积分商城网站开发
  • 如何申请一个网站域名做婚恋网站
  • 网站建设完整方案模板制作过程
  • 手机版网站建设价格小程序推广任务
  • 晚上必看的正能量网站app购物网站建设市场调查论文
  • 做阀门的网站山东网站建设报价
  • 大连网站排名优化公司新手做电商怎么做
  • 网站定制一般价格多少成都市今天最新消息情况