Java8 CompletableFuture 异步执行 Java8 CompletableFuture 异步执行操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java8 CompletableFuture 异步执行 Java8 CompletableFuture 异步执行操作

ACGkaka_   2021-06-22 我要评论
想了解Java8 CompletableFuture 异步执行操作的相关内容吗,ACGkaka_在本文为您仔细讲解Java8 CompletableFuture 异步执行的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java8,CompletableFuture,异步执行,Java8,CompletableFuture,下面大家一起来学习吧。

1.简介

CompletableFuture 是 JDK8 提供的一个异步执行工具。

示例1:

public static void main(String[] args) throws ExecutionException, InterruptedException {
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException ignored) {
            }
        }
        System.out.println("Future Finished.");
    });
    System.out.println("Main Thread Finished.");
    future.get();
}

输出结果1:

2.异步执行

CompletableFuture 提供了两个方法用于异步执行:

CompletableFuture.runAsync,没有返回值
CompletableFuture.supplyAsync,有返回值

示例:

public static void main(String[] args) throws ExecutionException, InterruptedException {
    // runAsync 没有返回值
    CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println("future1 executed."));
    // supplyAsync 有返回值
    CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future2 executed.");
        return "result";
    });

    System.out.println("future1.get(): " + future1.get());
    System.out.println("future2.get(): " + future2.get());
}

输出结果:

3.守护线程

CompletableFuture返回的Future默认为守护线程,如果不调用get()获取结果,主线程结束后会自动结束。主要有以下4种情景:

  • 情景1: 执行时间 > 主线程时间,异步线程会执行
  • 情景2: 执行时间 > 主线程,是守护线程,会被杀死,异步线程不会执行
  • 情景3: 执行时间 > 主线程,但是不是守护线程,不会被杀死,异步线程会执行
  • 情景4: ExecutorService.submit(),默认不是守护线程,不会被杀死,异步线程会执行

示例:

public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    // 1.执行时间 < 主线程,会打印
    CompletableFuture<Void> future1 = CompletableFuture.runAsync(() ->
		System.out.println("Thread1 是否为守护线程 : " + Thread.currentThread().isDaemon()));

    // 2.执行时间 > 主线程,是守护线程,会被杀死,不会打印
    CompletableFuture.runAsync(() -> {
        try {
            Thread.sleep(3000L);
            System.out.println("Thread2 是否为守护线程 : " + Thread.currentThread().isDaemon());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }});

    // 3.执行时间 > 主线程,但是不是守护线程,不会被杀死,会打印
    CompletableFuture.runAsync(() -> {
        try {
            Thread.sleep(1000L);
            System.out.println("Thread3 等待1秒");
            System.out.println("Thread3 是否为守护线程 : " + Thread.currentThread().isDaemon());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }}, executorService);

    // 4.ExecutorService.submit(),默认不是守护线程,不会被杀死,会打印。
    executorService.submit(() -> {
        try {
            Thread.sleep(2000L);
            System.out.println("Thread4 等待2秒");
            System.out.println("Thread4 是否为守护线程 : " + Thread.currentThread().isDaemon());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }});

    // 主线程执行完毕
    System.out.println("Main Thread Finished.");
    executorService.shutdown();
}

输出结果2:

4.处理执行结果

CompletableFuture还封装了很多处理执行结果操作。操作太多,列举比较常用的几种:

thenAccept(): 对结果进行使用;
thenApply(): 对结果进行转换;
exceptionally(): 对异常进行处理;
whenComplete(): 相当于 thenAccept() + thenApply() + exceptionally().

示例:

public static void main(String[] args) {
    // thenAccept对结果进行使用
    System.out.println("------------------------------");
    CompletableFuture.supplyAsync(() -> "Thread1 Finished.").thenAccept(System.out::println);

    // thenApply对结果进行转换
    System.out.println("------------------------------");
    CompletableFuture.supplyAsync(() -> "Thread2 Finished.")
        .thenApply(s -> s + " + thenApply()")
        .thenAccept(System.out::println);

    // exceptionally对异常进行处理
    System.out.println("------------------------------");
    CompletableFuture.supplyAsync(() -> {throw new RuntimeException("Thread3 Failed.");})
        .exceptionally(Throwable::toString).thenAccept(System.out::println);

    // 主线程执行完毕
    System.out.println("------------------------------");
    System.out.println("Main Thread Finished.");
}

输出结果:

whenComplete() 示例:

public static void main(String[] args) throws ExecutionException, InterruptedException {
    // thenAccept对结果进行使用
    System.out.println("------------------------------");
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Thread1 Finished.").whenComplete(new BiConsumer<String, Throwable>() {
        @Override
        public void accept(String s, Throwable throwable) {
            System.out.println("result: " + s);
            System.out.println("throwable: " + throwable);
        }
    });

    // exceptionally对异常进行处理
    System.out.println("------------------------------");
    CompletableFuture.supplyAsync(() -> {
        throw new RuntimeException("Thread3 Failed.");
    }).whenComplete(new BiConsumer<Object, Throwable>() {
        @Override
        public void accept(Object s, Throwable throwable) {
            System.out.println("result: " + s);
            System.out.println("throwable: " + throwable);
        }
    });

    System.out.println("------------------------------");
    System.out.println("future.get(): " + future.get());

    // 主线程执行完毕
    System.out.println("------------------------------");
    System.out.println("Main Thread Finished.");
}

输出结果:

整理完毕,完结撒花~

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们