当前位置: 首页 > 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/843945/

相关文章:

  • 旅游类网站建设教案wordpress mysql5.7
  • 个人网站对主机有什么要求为什么企业建设银行网站打不开
  • 网站做优化和推广哪个好阿里云域名查询官网
  • 网站优化标题不超过多少个字符汽车网络营销方式
  • 济南网站搜索引擎优化公司企业网站建设多少钱
  • 河北网站推广公司广州建设工程质量安全网站
  • 青铜峡建设局网站电商手机网站开发
  • 广州网站建设好评公司wordpress社
  • 如何建设一免费的网站广州网站推广工具
  • 创手机网站新网站建设服务公司
  • 网站建设基础申请建设工作网站的函
  • python做的大型网站12345浏览器
  • 做网站详细教程做网站用的什么编程语言
  • 关于医疗保障局门户网站建设做cpa用什么类型的网站好
  • 怎样做电子商务网站网站导航固定
  • 好的策划方案网站网站策划选题
  • 网站建设没业务管理咨询行业的理解
  • 网站内怎么做链接wordpress 图片问题
  • 揭阳手机网站建设餐饮最有效的营销方案
  • 视频网站咋么做高端网站的特点
  • 建设一个网站成本多少公司部门名称大全
  • 装修公司网站建设方案企业年报网上申报系统网址
  • 网站服务器多少钱一年公众号文章链接wordpress
  • 什么平台做网站宝安网站制作需要多少钱
  • wordpress建站配置推广营销网络
  • 石岩做网站公司国家职业资格证书网站
  • 西安自助建站魔方 网站
  • 海口招商建设有限公司网站百度搜到自己网站
  • 机关单位网站管理部门应建立做企业网站用哪个软件
  • 保定网站建设求职简历邹平网站建设公司报价