设为首页 加入收藏

TOP

Springboot中使用线程池的三种方式(四)
2023-09-23 15:44:13 】 浏览:150
Tags:Springboot 程池的 方式
yList<>(); while (j < size) { List<String> batchList = nameList.stream().skip(j).limit(Math.min(j + batchSize, size) - j).collect(Collectors.toList()); list.add(batchList); j += batchSize; } // 先把 list 切分成小份数据,在使用 @Async(),异步处理数据 list.stream().parallel().forEach(this::asynchronousAuthorization1); } /** * 异步注解处理业务逻辑,实际业务开发,需要提取到 Service 层,否则会报错。 * * @param paramList 入参 */ @Async("asyncServiceExecutor") public void asynchronousAuthorization1(List<String> paramList) { paramList.forEach(System.out::println); System.out.println("异步执行 paramList 业务逻辑"); } //================================分隔符====================== @Autowired private ExecutorService workerPool; /** * demo2: workerPool.execute() 实现异步逻辑。适用于没有返回值的情况下 */ public void asyncDemo2() { // 假设这是从数据库查询出来的数据 List<String> nameList = new ArrayList<>(Arrays.asList("张三", "李四", "王五")); // 把 nameList 进行切分 int j = 0, size = nameList.size(), batchSize = 10; List<List<String>> list = new ArrayList<>(); while (j < size) { List<String> batchList = nameList.stream().skip(j).limit(Math.min(j + batchSize, size) - j).collect(Collectors.toList()); list.add(batchList); j += batchSize; } // 将 list 切分成小份数据,workerPool.execute(),异步处理数据 list.stream().parallel().forEach(paramList->{ workerPool.execute(()->asynchronousAuthorization2(paramList)); }); } public void asynchronousAuthorization2(List<String> paramList) { paramList.forEach(System.out::println); System.out.println("异步执行 paramList 业务逻辑"); } //================================分隔符====================== /** * demo3: futures.add() 实现异步逻辑。适用于有返回值的情况下 */ public void asyncDemo3() { // 假设这是从数据库查询出来的数据 List<String> nameList = new ArrayList<>(Arrays.asList("张三", "李四", "王五")); // 把 nameList 进行切分 int j = 0, size = nameList.size(), batchSize = 10; List<List<String>> list = new ArrayList<>(); while (j < size) { List<String> batchList = nameList.stream().skip(j).limit(Math.min(j + batchSize, size) - j).collect(Collectors.toList()); list.add(batchList); j += batchSize; } List<CompletableFuture<String>> futures = new ArrayList<>(); // 将 list 切分成小份数据,futures.add(),异步处理数据,有返回值的情况下 list.forEach(paramList->{ // CompletableFuture.supplyAsync() 该任务会在一个新的线程中执行,并返回一个结果 // 通过futures.add(...)将这个异步任务添加到futures列表中。这样可以方便后续对多个异步任务进行管理和处理 futures.add(CompletableFuture.supplyAsync(() -> { asynchronousAuthorization3(paramList); return "默认值"; }, workerPool)); // 防止太快,让它休眠一下 try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } }); //new CompletableFuture[0] 创建了一个初始长度为 0 的 CompletableFuture 数组,作为目标数组。然后,futures.toArray(new CompletableFuture[0]) 将 futures 列表中的元素复制到目标数组中,并返回结果数组。 CompletableFuture<String>[] futuresArray = futures.toArray(new CompletableFuture[0]); // 通过将多个异步任务添加到futures列表中,我们可以使用CompletableFuture提供的方法来对这些异步任务进行组合、等待和处理。 // 例如使用CompletableFuture.allOf(...)等待所有任务完成,或者使用CompletableFuture.join()获取单个任务的结果等。 CompletableFuture.allOf(futures.toArray(futuresArray)).joi
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇组合模式 下一篇支持JDK19虚拟线程的web框架,之..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目