CompletableFuture
package com.example.demo.threadDemo; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; public class CompetableFutuerDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { /* CompletableFuture<Void> stringCompletableFuture = CompletableFuture.runAsync(()-> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("11111"); }).thenRun( () -> {System.out.println("22222");} ); Void aVoid = stringCompletableFuture.get(); System.out.println(aVoid); Void v= stringCompletableFuture.get(); System.out.println(); */ /* CompletableFuture<Void> stringCompletableFuture = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { try { Thread.sleep(3000); System.out.println("23232"); } catch (InterruptedException e) { e.printStackTrace(); } return "11111ddd"; } }).thenAccept(new Consumer<String>() { @Override public void accept(String s) { System.out.println("2222ddd" +"---" +s); } }); Void v= stringCompletableFuture.get(); System.out.println(); */ CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { try { Thread.sleep(3000); System.out.println("23232"); } catch (InterruptedException e) { e.printStackTrace(); } return "11111ddd"; } }).thenApply(new Function<String, String>() { @Override public String apply(String s) { System.out.println("3333"); return s +"---33333333"; } }); String s = stringCompletableFuture.get(); System.out.println(s); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架