专栏
天翼云开发者社区

CompletableFuture使用方法

2023-09-11 10:15:18 4阅读

1. supplyAsync

实现异步执行任务:supplyAsync是创建带有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

    void task1(){
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println("task1 do something....");
            return "task1";
        });

        //等待任务执行完成
        log.info("结果->" + cf.get());
    }    
void task2(){
        // 自定义线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println("task2 do something....");
            return "task2";
        }, executorService);

        //等待子任务执行完成
        log.info("结果->" + cf.get());
    }

2. runAsync

runAsync是创建没有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

void task3(){
        CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
            System.out.println("task3 do something....");
        });

        //等待任务执行完成
        log.info("task3 结果->" + cf.get());
    }

    void task4(){
        // 自定义线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
            System.out.println("task4 do something....");
        }, executorService);

        //等待任务执行完成
        log.info("task4结果->" + cf.get());
    }

3.thenApply和thenApplyAsync

thenApply 表示某个任务执行完成后执行的动作,即回调方法,会将该任务的执行结果即方法返回值作为入参传递到回调方法中,带有返回值。

    @Test
    void test5() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread() + " cf1 do something....");
            return 1;
        });

        CompletableFuture<Integer> cf2 = cf1.thenApplyAsync((result) -> {
            System.out.println(Thread.currentThread() + " cf2 do something....");
            result += 2;
            return result;
        });
        //等待任务1执行完成
        log.info("cf1结果->" + cf1.get());
        //等待任务2执行完成
        log.info("cf2结果->" + cf2.get());
    }

 

@Test
    void test6() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
            log.info(Thread.currentThread() + " cf1 do something....");
            return 1;
        });

        CompletableFuture<Integer> cf2 = cf1.thenApply((result) -> {
            log.info(Thread.currentThread() + " cf2 do something....");
            result += 2;
            return result;
        });
        //等待任务1执行完成
        log.info("test6 cf1结果->" + cf1.get());
        //等待任务2执行完成
        log.info("test6 cf2结果->" + cf2.get());
    }

thenApply和thenApplyAsync区别在于,使用thenApply方法时子任务与父任务使用的是同一个线程,而thenApplyAsync在子任务中是另起一个线程执行任务,并且thenApplyAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

 

4.allOf / anyOf

allOf:CompletableFuture 是多个任务都执行完成后才会执行,只有有一个任务执行异常,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回null。

anyOf :CompletableFuture 是多个任务只要有一个任务执行完成,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回执行完成任务的结果。

void task7() throws ExecutionException, InterruptedException {
        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            try {
                log.info(Thread.currentThread() + " cf1 do something....");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf1 任务完成");
            return "task7 cf1 任务完成";
        });

        CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread() + " cf2 do something....");
                int a = 1/0;
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf2 任务完成");
            return "task7 cf2 任务完成";
        });

        CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread() + " cf2 do something....");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf3 任务完成");
            return "task7 cf3 任务完成";
        });

        CompletableFuture<Void> cfAll = CompletableFuture.allOf(cf1, cf2, cf3);
//     CompletableFuture<Void> cfAll = CompletableFuture.anyOf(cf1, cf2, cf3);
        log.info("task7 cfAll结果->" + cfAll.get());
    }
  • 0
  • 0
  • 0
0 评论
0/1000
评论(0) 发表评论
y****n

y****n

3 篇文章 0 粉丝
关注

CompletableFuture使用方法

2023-09-11 10:15:18 4阅读

1. supplyAsync

实现异步执行任务:supplyAsync是创建带有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

    void task1(){
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println("task1 do something....");
            return "task1";
        });

        //等待任务执行完成
        log.info("结果->" + cf.get());
    }    
void task2(){
        // 自定义线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println("task2 do something....");
            return "task2";
        }, executorService);

        //等待子任务执行完成
        log.info("结果->" + cf.get());
    }

2. runAsync

runAsync是创建没有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

void task3(){
        CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
            System.out.println("task3 do something....");
        });

        //等待任务执行完成
        log.info("task3 结果->" + cf.get());
    }

    void task4(){
        // 自定义线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
            System.out.println("task4 do something....");
        }, executorService);

        //等待任务执行完成
        log.info("task4结果->" + cf.get());
    }

3.thenApply和thenApplyAsync

thenApply 表示某个任务执行完成后执行的动作,即回调方法,会将该任务的执行结果即方法返回值作为入参传递到回调方法中,带有返回值。

    @Test
    void test5() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread() + " cf1 do something....");
            return 1;
        });

        CompletableFuture<Integer> cf2 = cf1.thenApplyAsync((result) -> {
            System.out.println(Thread.currentThread() + " cf2 do something....");
            result += 2;
            return result;
        });
        //等待任务1执行完成
        log.info("cf1结果->" + cf1.get());
        //等待任务2执行完成
        log.info("cf2结果->" + cf2.get());
    }

 

@Test
    void test6() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
            log.info(Thread.currentThread() + " cf1 do something....");
            return 1;
        });

        CompletableFuture<Integer> cf2 = cf1.thenApply((result) -> {
            log.info(Thread.currentThread() + " cf2 do something....");
            result += 2;
            return result;
        });
        //等待任务1执行完成
        log.info("test6 cf1结果->" + cf1.get());
        //等待任务2执行完成
        log.info("test6 cf2结果->" + cf2.get());
    }

thenApply和thenApplyAsync区别在于,使用thenApply方法时子任务与父任务使用的是同一个线程,而thenApplyAsync在子任务中是另起一个线程执行任务,并且thenApplyAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

 

4.allOf / anyOf

allOf:CompletableFuture 是多个任务都执行完成后才会执行,只有有一个任务执行异常,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回null。

anyOf :CompletableFuture 是多个任务只要有一个任务执行完成,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回执行完成任务的结果。

void task7() throws ExecutionException, InterruptedException {
        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            try {
                log.info(Thread.currentThread() + " cf1 do something....");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf1 任务完成");
            return "task7 cf1 任务完成";
        });

        CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread() + " cf2 do something....");
                int a = 1/0;
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf2 任务完成");
            return "task7 cf2 任务完成";
        });

        CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread() + " cf2 do something....");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("task7 cf3 任务完成");
            return "task7 cf3 任务完成";
        });

        CompletableFuture<Void> cfAll = CompletableFuture.allOf(cf1, cf2, cf3);
//     CompletableFuture<Void> cfAll = CompletableFuture.anyOf(cf1, cf2, cf3);
        log.info("task7 cfAll结果->" + cfAll.get());
    }
文章来自专栏

技术知识分享

2 篇文章 1 订阅
0 评论
0/1000
评论(0) 发表评论
  • 0
    点赞
  • 0
    收藏
  • 0
    评论