首页 > 基础资料 博客日记
Future和CompletableFuture区别
2023-08-21 22:22:07基础资料围观634次
本篇文章分享Future和CompletableFuture区别,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识
-
Future :获取异步返回的结果需要使用轮询的方式,消耗cup
ExecutorService executorService = Executors.newFixedThreadPool(10); Future<String> future = executorService.submit(()->{ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "future"; }); while(true){ if(future.isDone()){ System.out.println(future.get()); break; } } -
CompletableFuture:采用观察者模式,阻塞获取异步返回的结果,性能得到优化
System.out.println("=============CompletableFuture==================="); CompletableFuture testFuture1 = CompletableFuture.supplyAsync(()->{ return "丽丽1"; }).thenApply((element)->{ System.out.println("testFuture1后续操作:"+element); return "丽丽2"; }); System.out.println(testFuture1.get()); System.out.println("=============CompletableFuture==================="); CompletableFuture testFuture2 = CompletableFuture.supplyAsync(()->{ return "丽丽1"; }).thenAccept((element)->{ System.out.println("testFuture2后续操作:"+element); }); System.out.println(testFuture2.get()); -
CompletableFuture的使用明细
- 官方文档:https://nightlies.apache.org/flink/flink-docs-release-1.14/docs/dev/datastream/operators/asyncio
* runAsync 无返回值 * supplyAsync 有返回值 * * thenAccept 无返回值 * thenApply 有返回值 * thenRun 不关心上一步执行结果,执行下一个操作 * get() 为阻塞获取 可设置超时时间 避免长时间阻塞实现接口 AsyncFunction 用于请求分发 定义一个callback回调函数,该函数用于取出异步请求的返回结果,并将返回的结果传递给ResultFuture 对DataStream的数据使用Async操作-
例子
/** * An implementation of the 'AsyncFunction' that sends requests and sets the callback. * 通过向数据库发送异步请求并设置回调方法 */ class AsyncDatabaseRequest extends RichAsyncFunction<String, Tuple2<String, String>> { /** The database specific client that can issue concurrent requests with callbacks 可以异步请求的特定数据库的客户端 */ private transient DatabaseClient client; @Override public void open(Configuration parameters) throws Exception { client = new DatabaseClient(host, post, credentials); } @Override public void close() throws Exception { client.close(); } @Override public void asyncInvoke(String key, final ResultFuture<Tuple2<String, String>> resultFuture) throws Exception { // issue the asynchronous request, receive a future for result // 发起一个异步请求,返回结果的 future final Future<String> result = client.query(key); // set the callback to be executed once the request by the client is complete // the callback simply forwards the result to the result future // 设置请求完成时的回调.将结果传递给 result future CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { try { return result.get(); } catch (InterruptedException | ExecutionException e) { // Normally handled explicitly. return null; } } }).thenAccept( (String dbResult) -> { resultFuture.complete(Collections.singleton(new Tuple2<>(key, dbResult))); }); } } // create the original stream // 创建一个原始的流 DataStream<String> stream = ...; // apply the async I/O transformation // 添加一个 async I/O ,指定超时时间,和进行中的异步请求的最大数量 DataStream<Tuple2<String, String>> resultStream = AsyncDataStream.unorderedWait(stream, new AsyncDatabaseRequest(), 1000, TimeUnit.MILLISECONDS, 100); -
注意事项
- Timeout:定义请求超时时间,异步请求多久没完成会被认为是超时了
- Capacity:定义了同时进行的异步请求的数量,可以限制并发请求数量,不会积压过多的请求
- 超时处理:默认当一个异步 I/O 请求超时时,会引发异常并重新启动作业。 如果要处理超时,可以覆盖该
AsyncFunction的timeout方法来自定义超时之后的处理方式 - 响应结果的顺序:AsyncDataStream包含两种输出模式,
- unorderedWait无序:响应结果的顺序与异步请求的顺序不同
- orderedWait有序:响应结果的顺序与异步请求的顺序相同
文章来源:https://www.cnblogs.com/xietingwei/p/17647249.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- SHP文件与PostGIS数据库Geom字段:WKT和EWKB相互转换SQL
- Spring AI 2.0 GA 倒计时:先别急,来看看 Java AI 框架的另一条路
- HEIC 批量转 JPG - 一键转换苹果 HEIC 格式为 JPG/PNG,保留 EXIF 信息,支持批量处理
- 【php】老旧PHP项目(PHP 5.6)本地环境搭建与踩坑记录
- Vue3 轻量安全滑动拼图验证码:vue-sliding-puzzle 上手全攻略
- 多市场行情时间戳对齐:UTC 存储的夏令时陷阱与数据库设计方案
- 15. 别再硬写提示词了!LangChain ChatPromptTemplate核心实战
- 一次 OOM 线上排查实录
- 从控制论看 Harness Engineering:当反馈回路终于能在"重要的地方"闭合
- AI 测试全场景提效:功能 / 性能 / 安全 / 自动化,用 AI 重塑测试工作流

