前言
CompletableFuture
是Java8新增的一个功能十分强大的工具类,它一方面实现了Future
接口,另一方面也实现了CompletionStage
接口,CompletionStage
接口多达40中方法,为我们函数式编程
流式调用提供支持。相较于FutureTask
来做多任务更简洁了。
使用
完成了就通知我
public String completeNotify() {
CompletableFuture<Integer> future = new CompletableFuture<>();
threadPoolTaskExecutor.execute(new AskThread(future));
try {
Integer result = future.get();
System.out.println("result " + result);
return result.toString();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
class AskThread implements Runnable {
CompletableFuture<Integer> future;
public AskThread(CompletableFuture<Integer> future) {
this.future = future;
}
@Override
public void run() {
int res = 0;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
res = 100;
future.complete(res);
}
}
异步执行任务
public String asyncTask() {
StopWatch stopWatch = new StopWatch("asyncTask");
stopWatch.start("task");
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> calc(50), threadPoolTaskExecutor);
CompletableFuture<Integer> futureTwo = CompletableFuture.supplyAsync(() -> calc(60), threadPoolTaskExecutor);
int result = 0;
int res = 0;
try {
result = future.get();
res = futureTwo.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println(result + " " + res);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
System.out.println(stopWatch.getLastTaskTimeMillis());
return result + " " + res;
}
public int calc(int param) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (EXCEPTION_PARAM == param){
throw new RuntimeException("传了异常参数 "+param);
}
return param * 2;
}
流式调用
public String stream() {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> calc(50), threadPoolTaskExecutor).
thenApply((i) -> Integer.toString(i)).
thenApply((str) -> "res " + str).
thenAccept(System.out::println);
try {
future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return "done";
}
异常处理
public String exception() {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> calc(10))
.exceptionally(ex -> {
System.out.println("异常信息 " + ex.toString());
return 0;
})
.thenApply((i) -> Integer.toString(i)).
thenApply((str) -> "res " + str).
thenAccept(System.out::println);
try {
future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return "done";
}
组合多个CompletableFuture
public String compose(){
CompletableFuture future = CompletableFuture.supplyAsync(()->calc(50),threadPoolTaskExecutor)
.thenCompose((i)->CompletableFuture.supplyAsync(()->calc(i),threadPoolTaskExecutor))
.thenApply((str)->"res " + str)
.thenAccept(System.out::println);
try {
future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return "done";
}
小结
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话