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

虹口做网站培训机构不退钱最怕什么举报

虹口做网站,培训机构不退钱最怕什么举报,浏览器禁止网站怎么做,编程培训机构哪个好1. 引言 在现代 Java 开发中,异步编程是提升应用性能的重要手段。CompletableFuture 作为 Java 8 引入的异步编程工具,不仅提供了丰富的 API 用于任务管理,还能让代码更加优雅地处理并发逻辑。 本文将深入解析 CompletableFuture 的常见方法…

1. 引言

在现代 Java 开发中,异步编程是提升应用性能的重要手段。CompletableFuture 作为 Java 8 引入的异步编程工具,不仅提供了丰富的 API 用于任务管理,还能让代码更加优雅地处理并发逻辑。

本文将深入解析 CompletableFuture 的常见方法,结合实际代码示例,并统计任务执行耗时,帮助你更好地理解 CompletableFuture 在实际开发中的应用场景。

2. CompletableFuture 常见方法及应用场景

2.1 supplyAsync() & runAsync() —— 创建异步任务

  • supplyAsync(Supplier):有返回值
  • runAsync(Runnable):无返回值
  • 适用场景远程 API 调用、数据库查询、文件 IO
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {try { Thread.sleep(1000); } catch (InterruptedException e) { }return "Hello, CompletableFuture!";
});
System.out.println(future.get()); // 阻塞等待执行结果

2.2 thenApply() —— 任务结果转换

  • 适用场景:对异步任务的结果进行后续计算
Instant start = Instant.now();CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { }return 10;
}).thenApply(result -> result * 2); //将 result*2System.out.println("cost: " + Duration.between(start, Instant.now()).toSeconds() + "s , result is " + future.get());

2.3 thenCompose() —— 连接两个异步任务

  • **适用场景:**第一个任务的结果作为第二个异步任务的输入
static void testThenCompose() throws ExecutionException, InterruptedException {Instant start = Instant.now();CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { }return 10;}).thenCompose(result -> CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { }return result * 2;}));System.out.println("cost: " + Duration.between(start, Instant.now()).toSeconds() + "s , result is " + future.get());
}
  • 执行结果分析: f1(2s)+f2(3s) ≈ 5s

2.4 thenCombine() —— 合并两个异步任务的结果

  • 适用场景:合并数据库查询、合并 API 请求数据
static void testThenCombine() throws ExecutionException, InterruptedException {Instant start = Instant.now();CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(7); } catch (InterruptedException e) { }return 10;});CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(13); } catch (InterruptedException e) { }return 20;});CompletableFuture<Integer> combinedFuture = future1.thenCombine(future2, Integer::sum);System.out.println("cost: " + Duration.between(start, Instant.now()).toSeconds() + "s , result is " + combinedFuture.get());
}
  • 执行结果分析:
    • future1 运行 7s,future2 运行 13s
    • thenCombine() 需要等待两个任务都完成,所以总时间 ≈ 13s

2.5 allOf() —— 等待所有任务完成

  • 适用场景:多个 API 并行请求,全部完成后执行逻辑
static void testAllOf() throws ExecutionException, InterruptedException {Instant start = Instant.now();CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { }return 10;});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { }return 20;});CompletableFuture<Integer> f3 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { }return 30;});CompletableFuture<Void> allFutures = CompletableFuture.allOf(f1, f2, f3);allFutures.join(); // 等待所有任务完成int sum = f1.get() + f2.get() + f3.get();System.out.println("cost: " + Duration.between(start, Instant.now()).toSeconds() + "s , result is " + sum);
}
  • 执行结果分析: f1(3s),f2(5s),f3(2s),allOf() 取最大时间 ≈ 5s

2.6 anyOf() —— 等待最快的任务完成

  • 适用场景:多个数据源查询,获取最快的结果
static void testAnyOf() throws ExecutionException, InterruptedException {Instant start = Instant.now();CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { }return "Task 1";});CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { }return "Task 2";});CompletableFuture<Object> any = CompletableFuture.anyOf(f1, f2);System.out.println("cost: " + Duration.between(start, Instant.now()).toSeconds() + "s , result is " + any.get());
}

-执行结果分析: f1(5s),f2(3s),返回最快的 ≈ 3s

2.7 exceptionally() —— 处理异常

  • 适用场景:网络请求失败时提供默认值
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {if (true) throw new RuntimeException("任务失败!");return 10;
}).exceptionally(ex -> {System.out.println("发生异常:" + ex.getMessage());return -1;
});
System.out.println(future.get());

3. 总结

方法作用适用场景
supplyAsync()创建异步任务(有返回值)API 调用、数据库查询
thenApply()转换任务结果计算返回值
thenCompose()连接两个异步任务任务依赖执行
thenCombine()合并两个任务的结果订单计算、价格合并
exceptionally()处理异常任务失败回退
allOf()等待所有任务完成并行任务
anyOf()只要有一个完成即返回竞态任务

4.其它

4.1指定线程池

在 CompletableFuture 中,supplyAsync() 方法默认使用ForkJoinPool.commonPool() 线程池执行任务。但如果需要使用自定义线程池,可以传递 Executor 作为参数,例如 Executors.newFixedThreadPool()
示例:使用自定义线程池

import java.util.concurrent.*;public class CompletableFutureWithCustomThreadPool {public static void main(String[] args) throws ExecutionException, InterruptedException {ExecutorService customThreadPool = Executors.newFixedThreadPool(5);CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {System.out.println("Running in thread: " + Thread.currentThread().getName());return 42;}, customThreadPool);System.out.println("Result: " + future.get());customThreadPool.shutdown(); // 记得关闭线程池}
}

执行结果

Running in thread: pool-1-thread-1
Result: 42

此示例使用了 固定大小的线程池,确保 CompletableFuture 任务在特定的线程池中执行,而不会与默认的 ForkJoinPool 共享资源。

适用场景
• 限制并发线程数,避免 ForkJoinPool 线程数失控
• 需要隔离 CompletableFuture 任务与其他任务(如 Web 服务器主线程)
• 适用于 数据库查询、API 调用、CPU 密集型计算

http://www.cadmedia.cn/news/11199.html

相关文章:

  • 网站如何建设与安全电商怎么注册开店
  • 廊坊企业做网站seo优化平台
  • cms建站流程网络营销策划的概念
  • 网站设计公司 广州志鸿优化网下载
  • 公司企业网站建设方案书seo提高关键词
  • 长春网站建设工作室百度官方电话人工服务电话
  • 商务网站建设论文sem竞价推广托管
  • 昆明app开发公司网站建设seo优化培训
  • 网站免费建站性盈盈影院windows优化大师下载
  • 网站建设的域名北京seo薪资
  • 电力建设科学技术进步申报网站百度竞价排名广告定价
  • 武汉网站建设哪家强每日国际新闻最新消息
  • 网站建设放什么会计科目国外搜索引擎
  • 海口网站建设呢网络热词有哪些
  • 九江 网站建设公司2022最近比较火的热点话题
  • 网站群建设意见征集郑州网络推广
  • 哔哩哔哩推广网站优化教程网下载
  • 网站建设平台赚钱线上营销手段
  • 商场设计与商品陈列苏州seo排名公司
  • 北京企业网站建设方案成品网站1688入口的功能介绍
  • 网站建设上机考试题目谷歌seo顾问
  • 企业网站的基本形式不包括优化软件seo排名
  • 大连网站seo顾问百度收录站长工具
  • 佛山市住房建设局网站恢复2345网址导航
  • 做网站哪个公司最专业的google推广公司
  • 网站建设 9a微信管理工具
  • 兰溪优秀高端网站设计地址如何推广一款app
  • 锋创科技园网站建设网络营销顾问是做什么的
  • 怎样防止网站被黑潍坊快速网站排名
  • 淘宝加盟网站建设b站推广入口2023破解版