首页

java8新新特性之CompletableFuture.allOf及thenApply、whenComplete用法代码示例(多线程并发任务控制四)

标签:多线程     发布时间:2024-09-11   

对于JDK8及以上版本通过CompletableFuture.allOf实现多任务并发进行,必须按照阶段1-2-3顺序整体并行进行复杂场景,代码如下:

911-4_2024-09-11_22-46-48.jpg

package com.xwood.java8.threads;@b@@b@import java.util.ArrayList;@b@import java.util.List;@b@import java.util.concurrent.CompletableFuture;@b@import java.util.concurrent.CopyOnWriteArrayList;@b@import java.util.concurrent.ExecutorService;@b@import java.util.concurrent.Executors;@b@@b@public class CompletableFutureComplexTest {@b@    public static void main(String[] args) {@b@        //原始数据集@b@        CopyOnWriteArrayList<Integer> taskList = new CopyOnWriteArrayList();@b@        taskList.add(1);@b@        taskList.add(2);@b@        taskList.add(3);@b@        taskList.add(4);@b@@b@        // 结果集@b@        List<Character> resultList = new ArrayList<>();@b@        //线程池,可容纳四个线程@b@        ExecutorService executorService = Executors.newFixedThreadPool(4);@b@        //执行四次任务:按照1-2-3顺序执行:1-转ascii码->2-转字符->3-加入结果集@b@        CompletableFuture[] cfs = taskList.stream()@b@                //第一阶段@b@                .map(integer -> CompletableFuture.supplyAsync(@b@                        () -> calcASCII(integer), executorService)@b@                        //第二阶段@b@                        .thenApply(i -> {@b@                            char c = (char) (i.intValue());@b@                            System.out.println("【阶段2】线程"@b@                                    + Thread.currentThread().getName() + "执行完毕,"@b@                                    + "已将int"@b@                                    + i + "转为了字符" + c);@b@                            return c;@b@                        })@b@                        //第三阶段@b@                        .whenComplete((ch, e) -> {@b@                            resultList.add(ch);@b@                            System.out.println("【阶段3】线程" +@b@                                    Thread.currentThread().getName() + "执行完毕," + "已将"@b@                                    + ch + "增加到了结果集" + resultList + "中");@b@                            executorService.shutdown();@b@                        })@b@                ).toArray(CompletableFuture[]::new);@b@@b@        // 封装后无返回值,必须自己whenComplete()获取@b@        CompletableFuture.allOf(cfs).join();//future.get()@b@@b@@b@        System.out.println(Thread.currentThread().getName()+"完成!result=" + resultList);@b@    }@b@@b@    //计算i的ASCII值@b@    public static Integer calcASCII(Integer i) {@b@        try {@b@            if (i == 1) {@b@                Thread.sleep(5000);@b@            } else {@b@                Thread.sleep(1000);@b@            }@b@            //数字 -> A-D对应的ascii@b@            i = i + 64;@b@            System.out.println("【阶段1】线程" + Thread.currentThread().getName()@b@                    + "执行完毕," + "已将" + i@b@                    + "转为了A(或B或C或D)对应的ASCII" + i);@b@        } catch (InterruptedException e) {@b@            e.printStackTrace();@b@        }@b@        return i;@b@    }@b@}

控制台结果

【阶段1】线程pool-1-thread-3执行完毕,已将67转为了A(或B或C或D)对应的ASCII67@b@【阶段1】线程pool-1-thread-2执行完毕,已将66转为了A(或B或C或D)对应的ASCII66@b@【阶段1】线程pool-1-thread-4执行完毕,已将68转为了A(或B或C或D)对应的ASCII68@b@【阶段2】线程pool-1-thread-2执行完毕,已将int66转为了字符B@b@【阶段2】线程pool-1-thread-3执行完毕,已将int67转为了字符C@b@【阶段2】线程pool-1-thread-4执行完毕,已将int68转为了字符D@b@【阶段3】线程pool-1-thread-3执行完毕,已将C增加到了结果集[null, C]中@b@【阶段3】线程pool-1-thread-2执行完毕,已将B增加到了结果集[null, C]中@b@【阶段3】线程pool-1-thread-4执行完毕,已将D增加到了结果集[null, C, D]中@b@【阶段1】线程pool-1-thread-1执行完毕,已将65转为了A(或B或C或D)对应的ASCII65@b@【阶段2】线程pool-1-thread-1执行完毕,已将int65转为了字符A@b@【阶段3】线程pool-1-thread-1执行完毕,已将A增加到了结果集[null, C, D, A]中@b@main完成!result=[null, C, D, A]
<<热门下载>>