CompletableFuture异步计算结果(2) 按顺序执行异步任务

1.thenAccept方法,前置子线程运行成功才执行,能获取前一个任务结果,无返回结果
CompletableFuture异步计算结果(2) 按顺序执行异步任务
thenAccept 前置执行成功,后续仍使用前置的子线程执行,无返回结果
thenAcceptAsync一个参数方法 前置执行成功,后续使用守护线程执行,无返回结果
thenAcceptAsync两个参数方法 前置执行成功,后续使用新的自定义线程执行,无返回结果
1.1 thenAccept 测试

public class ThenAcceptTest {
    public static void main(String[] args) {
        System.out.println("主线程"+Thread.currentThread().getName());
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程"+Thread.currentThread().getName());
            return "123";
        },executorService).thenAccept(value-> {
            System.out.println(value + "子线程" + Thread.currentThread().getName());
            executorService.shutdown();
        });
        System.out.println("主线程结束"+Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务

1.2 thenAcceptAsync 一个参数方法测试

public class ThenAcceptTest {
    public static void main(String[] args) {
        System.out.println("主线程"+Thread.currentThread().getName());
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程"+Thread.currentThread().getName());
            return "123";
        },executorService).thenAcceptAsync(value-> {
            System.out.println(value + "子线程" + Thread.currentThread().getName());
            executorService.shutdown();
        });
        System.out.println("主线程结束"+Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务
1.3 thenAcceptAsync 两个参数方法测试

public class ThenAcceptTest {
    public static void main(String[] args) {
        System.out.println("主线程"+Thread.currentThread().getName());
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程"+Thread.currentThread().getName());
            return "123";
        },executorService).thenAcceptAsync(value-> {
            System.out.println(value + "子线程" + Thread.currentThread().getName());
            executorService.shutdown();
        },executorService);
        System.out.println("主线程结束"+Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务
2.thenApply方法,前置子线程运行成功才执行,能获取前一个任务结果,有返回结果
CompletableFuture异步计算结果(2) 按顺序执行异步任务
thenApply 前置执行成功,后续仍使用前置的子线程执行,有返回结果
thenApplyAsync 一个参数方法 前置执行成功,后续使用守护线程执行,有返回结果
thenApplyAsync 两个参数方法 前置执行成功,后续使用新的自定义线程执行,有返回结果
2.1 thenApply 测试

public class ThenApplyTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程" + Thread.currentThread().getName());
            return "123";
        }, executorService).thenApply(value -> {
            System.out.println("thenApply子线程" + Thread.currentThread().getName());
            executorService.shutdown();
            return value + "thenApply";
        });
        String s = null;
        try {
            s = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s+"主线程结束" + Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务
2.2 thenApplyAsync 一个参数方法测试

public class ThenApplyTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程" + Thread.currentThread().getName());
            return "123";
        }, executorService).thenApplyAsync(value -> {
            System.out.println("thenApply子线程" + Thread.currentThread().getName());
            executorService.shutdown();
            return value + "thenApply";
        });
        String s = null;
        try {
            s = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s+"主线程结束" + Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务
2.3 thenApplyAsync 两个参数方法测试

public class ThenApplyTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程" + Thread.currentThread().getName());
            return "123";
        }, executorService).thenApplyAsync(value -> {
            System.out.println("thenApply子线程" + Thread.currentThread().getName());
            executorService.shutdown();
            return value + "thenApply";
        },executorService);
        String s = null;
        try {
            s = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s+"主线程结束" + Thread.currentThread().getName());
    }
}

CompletableFuture异步计算结果(2) 按顺序执行异步任务

上一篇:多线程从入门到高级(12)--LockSupport与AQS


下一篇:lamda表达式