java 多线程中对于一个任务A完成了,任务B才开始;任务B完成了,任务C才开始;’任务C完成,任务D才开始;每个任务都是一个异步任务列。
废话少说,直接看代码,调试理解
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors() * 2,
            (int)(Runtime.getRuntime().availableProcessors() /0.1), 120,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000));

    public static void main(String[] args) {
        List<Integer> numberList = IntStream.rangeClosed(1, 10)
                .boxed() // 将int转换为Integer
                .toList();

        CompletableFuture<Void> voidCompletableFuture = taskA(numberList)
                .thenRun(() -> taskB(numberList).join()) // 此处和下面两行可以去掉join()测试观察异同
                .thenRun(() -> taskC(numberList).join())
                .thenRun(() -> taskD(numberList).join())
                        .thenRunAsync(() -> {
                            executor.shutdown();
                        });
       // voidCompletableFuture.join();  
    }


    private static CompletableFuture<Void> taskD(List<Integer> numberList) {
        System.out.println();
        List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.print("taskD--" + x);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor)).toList();
        return CompletableFuture.allOf(
                futureList.toArray(new CompletableFuture[0])
        );
    }

    private static CompletableFuture<Void> taskC(List<Integer> numberList) {
        System.out.println();
        List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.print("taskC--" + x);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor)).toList();
        return CompletableFuture.allOf(
                futureList.toArray(new CompletableFuture[0])
        );
    }

    private static CompletableFuture<Void> taskB(List<Integer> numberList) {
        System.out.println();

        List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.print("taskB--" + x);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, executor)).toList();
        return CompletableFuture.allOf(
                futureList.toArray(new CompletableFuture[0])
        );
    }

    private static CompletableFuture<Void> taskA(List<Integer> numberList) {
        System.out.println();

        List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.print("taskA--" + x);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor)).toList();
        return CompletableFuture.allOf(
                futureList.toArray(new CompletableFuture[0])
        );
    }

执行结果

{988EF5F1-F1EE-42D4-B56F-53F21CFF01F8}

 

posted on 2025-10-24 18:22  yuluoxingkong  阅读(1)  评论(0)    收藏  举报