CompletableFuture 测试类
package com.example.apidemo.completableFutrue; import java.util.Random; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; /** * @Author Tim * @Date 2021/10/25 14:18 */ public class TestComplete { //runAsync方法不支持返回值。 //supplyAsync可以支持返回值。 public static void main1(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { System.out.println("run end1 ..." + Thread.currentThread().getName()); } catch (Exception e) { e.printStackTrace(); } System.out.println("run end2 ..." + Thread.currentThread().getName()); return "返回值:" + System.currentTimeMillis(); }); System.out.println("run end3 ...:" + future.get() + "/" + Thread.currentThread().getName()); } //当CompletableFuture的计算结果完成的回调方法 public static void main2(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { System.out.println("run ..." + Thread.currentThread().getName()); } catch (Exception e) { } System.out.println("run end ..." + Thread.currentThread().getName()); return "1234567"; }).whenComplete((String v, Throwable t) -> { System.out.println("run end2 ..." + v + "/" + Thread.currentThread().getName() + "/" + t); }); // v是有返回值的回调的结果,t 是线程... 可省略写法:whenComplete((v, t) -> {}); } //thenApply 方法 : 当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。 //.thenApply(Function<? super T, ? extends U> fn) T是上一个任务返回结果的类型。U:当前任务的返回值类型 public static void main3(String[] args) throws Exception { CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() { @Override public Long get() { long result = new Random().nextInt(100); System.out.println("result1 ="+result); return result; } }).thenApply(new Function<Long, Long>() { @Override public Long apply(Long t) { long result = t*5; System.out.println("result2 ="+result); return result; } }); long result = future.get(); System.out.println("result--------:" + result); } //handle 是执行任务(包括出现异常)完成时对结果的处理。 而thenApply 方法,如果上个任务出现错误,则不会执行 thenApply 方法。 public static void main4(String[] args) throws Exception { CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { return new Random().nextInt(10); } }).handle(new BiFunction<Integer, Throwable, Integer>() { @Override public Integer apply(Integer param, Throwable throwable) { int result = 1; if (throwable == null){ result = param * 2; System.out.println("apply: ==== " + param); } else { System.out.println("error: ==== " + throwable.getMessage()); } return result; } }); System.out.println("result--------:" + future.get()); } //thenAccept 接收任务的处理结果,并消费处理,无返回结果。没有后续的输出操作, 所以future.get() 是null public static void main5(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { return new Random().nextInt(10); } }).thenAccept(integer -> { System.out.println("integer====" + integer); }); System.out.println("result--------:" + future.get()); } // thenRun: 该方法同 thenAccept 方法类似。不同的是上个任务处理完成后,并不会把计算的结果传给 thenRun 方法。 // 只是处理玩任务后,执行 thenAccept 的后续操作 public static void main6(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { return new Random().nextInt(10); } }).thenRun(() -> { System.out.println("thenRun ..." + 1); }); System.out.println("result--------:" + future.get()); } //thenCombine 合并任务 public static void main(String[] args) throws Exception { CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { return "zhangshan"; } }); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { return "lisi"; } }); CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() { @Override public String apply(String t, String u) { return t + "===" + u; } }); System.out.println("result--------:" + result.get()); } }
学海无涯 代码作伴
分类:
异步线程,回调类
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南