Future<T>是一种任务,可以使用CompleteFuture<T>代替Future<T>。
CompleteFuture<T>的介绍:
https://blog.csdn.net/qq_34562093/article/details/90209520
https://www.cnblogs.com/TS86/p/18205735
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | package com.example.demo.basicSE; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; // Future<T> f = executorService.submit(new Callable<T>),但f.get()是阻塞的(CompletableFuture.get()也是阻塞的) // 一般使用CompletableFuture替代Future:https://blog.csdn.net/qq_34562093/article/details/90209520 // CompletionService和CompletableFuture(按照任务完成的先后顺序获取任务的结果) public class threads { public static void _future() throws InterruptedException, ExecutionException{ List<Integer> list = List.of( 1 , 2 , 3 , 4 , 5 ); ExecutorService executorService = Executors.newFixedThreadPool( 5 ); List<Future<Integer>> futures = new ArrayList<>(); for ( int i : list) { Future<Integer> future = executorService.submit( new createSqre(i)); futures.add(future); } List<Integer> res = new ArrayList<>(); for (Future<Integer> f:futures){ int t = f.get(); res.add(t); } for ( int i:res){ System.out.println(i); } // shutdown()只是关闭了外部的submit(),线程池内部未执行完的还会继续跑 executorService.shutdown(); try { // shutdownNow()立即停止线程池,正在跑的和等待的都停止了,但风险较大 // awaitTermination()后外部可以继续submit()进线程池。当前线程阻塞直到所有已提交任务跑完/超时/线程Interrupt()。 // 使用awaitTermination()来等待线程池中所有任务都执行完毕 if (!executorService.awaitTermination( 60 , TimeUnit.SECONDS)){ executorService.shutdownNow(); } } catch (Exception e) { // TODO: handle exception executorService.shutdownNow(); Thread.currentThread().interrupt(); } } public static int math_square( int x){ return x*x; } public static void _completeFuture(){ List<Integer> l = List.of( 1 , 2 , 3 , 4 , 5 ); // supplyAsync()异步处理 List<CompletableFuture<Integer>> futures = l.stream().map(i->CompletableFuture.supplyAsync(()->math_square(i))).collect(Collectors.toList()); // allof()等待所有future执行完成 CompletableFuture<Void> aCompletableFuture = CompletableFuture.allOf(futures.toArray( new CompletableFuture[ 0 ])); // 等待所有任务完成 aCompletableFuture.join(); List<Integer> ans = futures.stream().map(f->{ try { return f.get(); } catch (Exception e) { throw new IllegalStateException(e); // TODO: handle exception } }).collect(Collectors.toList()); ans.forEach(System.out::println); } public static void main(String[] args) throws InterruptedException, ExecutionException { // _future(); _completeFuture(); } } class createSqre implements Callable<Integer>{ private int n; createSqre( int a){ this .n = a;} @Override public Integer call() throws Exception { return n*n; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类