CompletableFuture 使用详解

CompletableFuture 使用详解

 

1、 runAsync 和 supplyAsync方法

CompletableFuture 提供了四个静态方法来创建一个异步操作。

public static CompletableFuture runAsync(Runnable runnable)
public static CompletableFuture runAsync(Runnable runnable, Executor executor)
public static  CompletableFuture supplyAsync(Supplier supplier)
public static  CompletableFuture supplyAsync(Supplier supplier, Executor executor)

没有指定Executor的方法会使用ForkJoinPool.commonPool() 作为它的线程池执行异步代码。如果指定线程池,则使用指定的线程池运行。以下所有的方法都类同。

  • runAsync方法不支持返回值。

  • supplyAsync可以支持返回值。

示例

    @Test
    public void test1() throws Exception {
        //无返会值
        Runnable runnable = () -> {
            System.out.println("runnable");
        };
        CompletableFuture future1 = CompletableFuture.runAsync(runnable);
        System.out.println("future1=" + future1.get());

        //有返回值
        Supplier supplier = () -> {
            System.out.println("supplier");
            return "supplier";
        };
        CompletableFuture future2 = CompletableFuture.supplyAsync(supplier);
        System.out.println("future2=" + future2.get());

        sleep(100);
    }

CompletableFuture 使用详解_第1张图片

自定义线程池

    //线程池
    @Test
    public void test8() throws Exception {
        
        Supplier supplier = () -> {
            return Thread.currentThread().getName();
        };
        //使用默认线程池
        CompletableFuture future1 = CompletableFuture.supplyAsync(supplier);
        System.out.println("future1=" + future1.get());

        //使用自定义线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture future2 = CompletableFuture.supplyAsync(supplier, threadPool);
        System.out.println("future2=" + future2.get());

        sleep(100);
        threadPool.shutdown();
    }

 

CompletableFuture 使用详解_第2张图片

2、whenComplete/whenCompleteAsync 方法

当CompletableFuture的计算结果完成,或者抛出异常的时候,可以执行特定的Action。主要是下面的方法:

public CompletableFuture whenComplete(BiConsumer action)
public CompletableFuture whenCompleteAsync(BiConsumer action)
public CompletableFuture whenCompleteAsync(BiConsumer action, Executor executor)
public CompletableFuture exceptionally(Function fn)

可以看到Action的类型是BiConsumer它可以处理正常的计算结果,或者异常情况。

whenComplete 和 whenCompleteAsync 的区别:

1、whenComplete方法比较复杂,当 X 线程执行了CompletableFuture.complete()方法后意味着CompletableFuture.get() 已有返回值 ,这是一个十分关键时间点 A。 如果CompletableFuture.whenComplete()在时间点A 前被调用,则CompletableFuture.complete()完成后就马上调用CompletableFuture.whenComplete(),并且线程不切换。如果在时间点 A 后被用,由于调用CompletableFuture.complete()方法的线程已执行别的任务,这时CompletableFuture.whenComplete()中的任务只能由执行CompletableFuture.whenComplete()方法的线程代替完成。

2、whenCompleteAsync的场合,就简单很多。一句话就是线程池里面拿一个空的线程或者新启一个线程来执行回调。和执行f.complete的线程以及执行whenCompleteAsync的线程无关。

示例

whenComplete的使用:

    @Test
    public void test9() throws Exception {
        System.out.println("本方法所用线程 " + Thread.currentThread().getName());
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        //一个5秒任务
        Supplier supplier = () -> {
            sleep(5);
            String s = "supplier " + Thread.currentThread().getName();
            System.out.println(s);
            return s;
        };
        //任务完成后的行为
        BiConsumer action = (result, exception) -> {
            String s = "action " + Thread.currentThread().getName();
            System.out.println(s);
        };

        //将任务交给线程池处理,任务结束会自动调用CompletableFuture.complete()方法。
        CompletableFuture future1 = CompletableFuture.supplyAsync(supplier, threadPool);
        //睡1秒,这时任务还没结束,这时调用whenComplete方法,将会和supplier的执行使用相同的线程。
        sleep(1);
        CompletableFuture future2 = future1.whenComplete(action);

        //睡10秒,这时任务已结束,这时调用whenComplete方法。只能使用调用本方法的线程
        sleep(10);
        CompletableFuture future3 = future1.whenComplete(action);

        sleep(100);
    }

CompletableFuture 使用详解_第3张图片                     

whenCompleteAsync的使用 

    @Test
    public void test10() throws Exception {
        System.out.println("本方法所用线程 " + Thread.currentThread().getName());
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        //一个5秒任务
        Supplier supplier = () -> {
            sleep(5);
            String s = "supplier " + Thread.currentThread().getName();
            System.out.println(s);
            return s;
        };
        //任务完成后的行为
        BiConsumer action = (result, exception) -> {
            String s = "action " + Thread.currentThread().getName();
            System.out.println(s);
        };

        //将任务交给线程池处理
        CompletableFuture future1 = CompletableFuture.supplyAsync(supplier, threadPool);
        //睡1秒,使用默认线程池
        sleep(1);
        CompletableFuture future2 = future1.whenCompleteAsync(action);

        //睡10秒,whenCompleteAsync可以指定执行的线程池
        sleep(10);
        CompletableFuture future3 = future1.whenCompleteAsync(action,threadPool);

        sleep(100);
    }

CompletableFuture 使用详解_第4张图片 

3、 thenApply/thenApplyAsync 方法

当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。

public  CompletableFuture thenApply(Function fn)
public  CompletableFuture thenApplyAsync(Function fn)
public  CompletableFuture thenApplyAsync(Function fn, Executor executor)

Function
T:上一个任务返回结果的类型
U:当前任务的返回值类型

thenApply/thenApplyAsync的区别和whenComplete/whenCompleteAsync 区别是一样的,详见上文。

示例

    @Test
    public void test10() throws Exception {
        System.out.println("本方法所用线程 " + Thread.currentThread().getName());
        //一个5秒任务
        Supplier supplier = () -> {
            sleep(5);
            String s = "supplier " + Thread.currentThread().getName();
            System.out.println(s);
            return 5;
        };

        //一个后继任务
        Function function = (result) -> {
            String s = "function " + Thread.currentThread().getName();
            System.out.println(s);
            return "结果是:"+result;
        };

        //将任务交给线程池处理
        CompletableFuture future1 = CompletableFuture.supplyAsync(supplier);
        //睡1秒
        sleep(1);
        CompletableFuture future2 = future1.thenApply(function);
        System.out.println(future2.get());
        //睡10秒
        sleep(10);
        CompletableFuture future3 = future1.thenApply(function);
        System.out.println(future3.get());

        sleep(100);
    }

CompletableFuture 使用详解_第5张图片 

第二个任务依赖第一个任务的结果。

4、 handle/handleAsync 方法

handle 是执行任务完成时对结果的处理。
handle 方法和 thenApply 方法处理方式基本一样。不同的是 handle 是在任务完成后再执行,还可以处理异常的任务。thenApply 只可以执行正常的任务,任务出现异常则不执行 thenApply 方法。

public  CompletionStage handle(BiFunction fn);
public  CompletionStage handleAsync(BiFunction fn);
public  CompletionStage handleAsync(BiFunction fn,Executor executor);

示例

public static void handle() throws Exception{
    CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {

        @Override
        public Integer get() {
            int i= 10/0;
            return new Random().nextInt(10);
        }
    }).handle(new BiFunction() {
        @Override
        public Integer apply(Integer param, Throwable throwable) {
            int result = -1;
            if(throwable==null){
                result = param * 2;
            }else{
                System.out.println(throwable.getMessage());
            }
            return result;
        }
     });
    System.out.println(future.get());
}

从示例中可以看出,在 handle 中可以根据任务是否有异常来进行做相应的后续处理操作。而 thenApply 方法,如果上个任务出现错误,则不会执行 thenApply 方法。

5、 thenAccept 消费处理结果

接收任务的处理结果,并消费处理,无返回结果。

public CompletionStage thenAccept(Consumer action);
public CompletionStage thenAcceptAsync(Consumer action);
public CompletionStage thenAcceptAsync(Consumer action,Executor executor);

示例

public static void thenAccept() throws Exception{
    CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            return new Random().nextInt(10);
        }
    }).thenAccept(integer -> {
        System.out.println(integer);
    });
    future.get();
}

从示例代码中可以看出,该方法只是消费执行完成的任务,并可以根据上面的任务返回的结果进行处理。并没有后续的输错操作。

6、thenRun 方法

跟 thenAccept 方法不一样的是,不关心任务的处理结果。只要上面的任务执行完成,就开始执行 thenAccept 。

public CompletionStage thenRun(Runnable action);
public CompletionStage thenRunAsync(Runnable action);
public CompletionStage thenRunAsync(Runnable action,Executor executor);

示例

public static void thenRun() throws Exception{
    CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            return new Random().nextInt(10);
        }
    }).thenRun(() -> {
        System.out.println("thenRun ...");
    });
    future.get();
}

该方法同 thenAccept 方法类似。不同的是上个任务处理完成后,并不会把计算的结果传给 thenRun 方法。只是处理玩任务后,执行 thenAccept 的后续操作。

7、thenCombine 合并任务

thenCombine 会把 两个 CompletionStage 的任务都执行完成后,把两个任务的结果一块交给 thenCombine 来处理。

public  CompletionStage thenCombine(CompletionStage other,BiFunction fn);
public  CompletionStage thenCombineAsync(CompletionStage other,BiFunction fn);
public  CompletionStage thenCombineAsync(CompletionStage other,BiFunction fn,Executor executor);

示例

private static void thenCombine() throws Exception {
    CompletableFuture future1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public String get() {
            return "hello";
        }
    });
    CompletableFuture future2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public String get() {
            return "hello";
        }
    });
    CompletableFuture result = future1.thenCombine(future2, new BiFunction() {
        @Override
        public String apply(String t, String u) {
            return t+" "+u;
        }
    });
    System.out.println(result.get());
}

8、thenAcceptBoth

当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行消耗

public  CompletionStage thenAcceptBoth(CompletionStage other,BiConsumer action);
public  CompletionStage thenAcceptBothAsync(CompletionStage other,BiConsumer action);
public  CompletionStage thenAcceptBothAsync(CompletionStage other,BiConsumer action,     Executor executor);

示例

private static void thenAcceptBoth() throws Exception {
    CompletableFuture f1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture f2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.thenAcceptBoth(f2, new BiConsumer() {
        @Override
        public void accept(Integer t, Integer u) {
            System.out.println("f1="+t+";f2="+u+";");
        }
    });
}

9、applyToEither 方法

两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的转化操作。

public  CompletionStage applyToEither(CompletionStage other,Function fn);
public  CompletionStage applyToEitherAsync(CompletionStage other,Function fn);
public  CompletionStage applyToEitherAsync(CompletionStage other,Function fn,Executor executor);

示例

private static void applyToEither() throws Exception {
    CompletableFuture f1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
    CompletableFuture f2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    
    CompletableFuture result = f1.applyToEither(f2, new Function() {
        @Override
        public Integer apply(Integer t) {
            System.out.println(t);
            return t * 2;
        }
    });

    System.out.println(result.get());
}

10、acceptEither 方法

两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的消耗操作。

public CompletionStage acceptEither(CompletionStage other,Consumer action);
public CompletionStage acceptEitherAsync(CompletionStage other,Consumer action);
public CompletionStage acceptEitherAsync(CompletionStage other,Consumer action,Executor executor);

示例

private static void acceptEither() throws Exception {
    CompletableFuture f1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture f2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.acceptEither(f2, new Consumer() {
        @Override
        public void accept(Integer t) {
            System.out.println(t);
        }
    });
}

11、runAfterEither 方法

两个CompletionStage,任何一个完成了都会执行下一步的操作(Runnable)

public CompletionStage runAfterEither(CompletionStage other,Runnable action);
public CompletionStage runAfterEitherAsync(CompletionStage other,Runnable action);
public CompletionStage runAfterEitherAsync(CompletionStage other,Runnable action,Executor executor);

示例

private static void runAfterEither() throws Exception {
    CompletableFuture f1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture f2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.runAfterEither(f2, new Runnable() {
        
        @Override
        public void run() {
            System.out.println("上面有一个已经完成了。");
        }
    });
}

12、runAfterBoth

两个CompletionStage,都完成了计算才会执行下一步的操作(Runnable)

public CompletionStage runAfterBoth(CompletionStage other,Runnable action);
public CompletionStage runAfterBothAsync(CompletionStage other,Runnable action);
public CompletionStage runAfterBothAsync(CompletionStage other,Runnable action,Executor executor);

示例

private static void runAfterBoth() throws Exception {
    CompletableFuture f1 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture f2 = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Integer get() {
            int t = new Random().nextInt(3);
            try {
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.runAfterBoth(f2, new Runnable() {
        
        @Override
        public void run() {
            System.out.println("上面两个任务都执行完成了。");
        }
    });
}

13、thenCompose 方法

thenCompose 方法允许你对两个 CompletionStage 进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作。

public  CompletableFuture thenCompose(Function> fn);
public  CompletableFuture thenComposeAsync(Function> fn) ;
public  CompletableFuture thenComposeAsync(Function> fn, Executor executor) ;

示例

private static void thenCompose() throws Exception {
        CompletableFuture f = CompletableFuture.supplyAsync(new Supplier() {
            @Override
            public Integer get() {
                int t = new Random().nextInt(3);
                System.out.println("t1="+t);
                return t;
            }
        }).thenCompose(new Function>() {
            @Override
            public CompletionStage apply(Integer param) {
                return CompletableFuture.supplyAsync(new Supplier() {
                    @Override
                    public Integer get() {
                        int t = param *2;
                        System.out.println("t2="+t);
                        return t;
                    }
                });
            }
            
        });
        System.out.println("thenCompose result : "+f.get());
    }

你可能感兴趣的:(java基础进阶)