java computablefuture 代码示例
completablefuture
是 java 8 引入的一个强大的异步编程工具,它实现了 future
接口并提供了更丰富的功能。
以下是一些常见的 completablefuture
使用示例:
1. 基本用法
import java.util.concurrent.completablefuture; import java.util.concurrent.executionexception; public class completablefuturebasicexample { public static void main(string[] args) { // 创建一个completablefuture并异步执行任务 completablefuture<string> future = completablefuture.supplyasync(() -> { try { thread.sleep(1000); // 模拟耗时操作 } catch (interruptedexception e) { e.printstacktrace(); } return "hello, completablefuture!"; }); // 注册完成时的回调 future.thenaccept(result -> system.out.println("result: " + result)); // 阻塞等待结果 try { string result = future.get(); system.out.println("main thread got: " + result); } catch (interruptedexception | executionexception e) { e.printstacktrace(); } } }
2. 链式调用
import java.util.concurrent.completablefuture; public class completablefuturechainexample { public static void main(string[] args) { completablefuture.supplyasync(() -> { // 第一个异步任务 system.out.println("task 1 running in thread: " + thread.currentthread().getname()); return "hello"; }).thenapplyasync(result -> { // 第二个异步任务,接收上一个任务的结果 system.out.println("task 2 running in thread: " + thread.currentthread().getname()); return result + " world"; }).thenacceptasync(result -> { // 第三个异步任务,消费最终结果 system.out.println("task 3 running in thread: " + thread.currentthread().getname()); system.out.println("final result: " + result); }).join(); // 等待所有任务完成 } }
3. 组合多个 future
import java.util.concurrent.completablefuture; public class completablefuturecombineexample { public static void main(string[] args) { completablefuture<string> future1 = completablefuture.supplyasync(() -> "hello"); completablefuture<string> future2 = completablefuture.supplyasync(() -> "world"); // 合并两个future的结果 completablefuture<string> combinedfuture = future1.thencombine(future2, (s1, s2) -> s1 + " " + s2); combinedfuture.thenaccept(system.out::println).join(); } }
4. 异常处理
import java.util.concurrent.completablefuture; public class completablefutureexceptionhandling { public static void main(string[] args) { completablefuture.supplyasync(() -> { if (math.random() > 0.5) { throw new runtimeexception("something went wrong!"); } return "success"; }).exceptionally(ex -> { system.out.println("exception occurred: " + ex.getmessage()); return "recovered from exception"; }).thenaccept(system.out::println).join(); } }
5. 并行执行多个任务
import java.util.concurrent.completablefuture; import java.util.concurrent.timeunit; public class completablefutureallofexample { public static void main(string[] args) { completablefuture<string> task1 = completablefuture.supplyasync(() -> { sleep(1); return "task 1"; }); completablefuture<string> task2 = completablefuture.supplyasync(() -> { sleep(2); return "task 2"; }); completablefuture<string> task3 = completablefuture.supplyasync(() -> { sleep(3); return "task 3"; }); // 等待所有任务完成 completablefuture<void> allfutures = completablefuture.allof(task1, task2, task3); // 获取所有任务的结果 completablefuture<string> combinedfuture = allfutures.thenapply(v -> { string result1 = task1.join(); string result2 = task2.join(); string result3 = task3.join(); return result1 + ", " + result2 + ", " + result3; }); system.out.println("combined result: " + combinedfuture.join()); } private static void sleep(int seconds) { try { timeunit.seconds.sleep(seconds); } catch (interruptedexception e) { e.printstacktrace(); } } }
6. 超时处理(java 9+)
import java.util.concurrent.completablefuture; import java.util.concurrent.timeunit; import java.util.concurrent.timeoutexception; public class completablefuturetimeoutexample { public static void main(string[] args) { completablefuture<string> future = completablefuture.supplyasync(() -> { try { thread.sleep(2000); // 模拟耗时操作 } catch (interruptedexception e) { e.printstacktrace(); } return "result"; }); // 设置超时时间为1秒 completablefuture<string> timeoutfuture = future.ortimeout(1, timeunit.seconds); timeoutfuture.whencomplete((result, ex) -> { if (ex != null) { system.out.println("exception: " + ex.getclass().getname()); if (ex.getcause() instanceof timeoutexception) { system.out.println("task timed out"); } } else { system.out.println("result: " + result); } }).join(); } }
这些示例展示了 completablefuture
的主要功能,包括异步执行、链式调用、组合多个 future、异常处理和超时控制等。completablefuture
是 java 并发编程中非常强大的工具,可以大大简化异步编程的复杂性。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论