AVA8-CompletableFuture常用API:runAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf

admin
2022-11-22 / 0 评论 / 159 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年11月22日,已超过542天没有更新,若内容或图片失效,请留言反馈。

AVA8-CompletableFuture常用API:runAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf

1、runAfterBoth :2个CompletableFuture都执行完后,再执行其它操作

package com.example.study.java8.completableFutures.api;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureAction2 {
    public static void main(String[] args) throws InterruptedException {
        //API-- 1、runAfterBoth: 2个都执行完后,再执行其它操作
        CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "this is runing 1......");
            return 1;
        })
        .runAfterBoth(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "this is runing 2......");
            return 2;
        }), () -> System.out.println("done"));


        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(10000);
    }
}

输出结果:

ForkJoinPool.commonPool-worker-5this is runing 2......
ForkJoinPool.commonPool-worker-19this is runing 1......
done

2、 applyToEither:其中一个CompletableFuture执行完,就将结果传给另一个Fuction

package com.example.study.java8.completableFutures.api;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureAction2 {
    public static void main(String[] args) throws InterruptedException {
    
        //API-- 2、runAfterBoth: 其中一个CompletableFuture执行完,就将结果传递到另一个Function中。
        CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"this is one future...");
            return 1;
        }).applyToEither(CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"this is two future...");
            return 2;
        }), v->
            10 * v
        ).thenAccept(System.out::println);



        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(10000);
    }
}

输出结果:

ForkJoinPool.commonPool-worker-5this is two future...
20
ForkJoinPool.commonPool-worker-19this is one future...

3、acceptEither :其中一个future执行完,就将结果传递到cutomer中消费

package com.example.study.java8.completableFutures.api;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureAction2 {
    public static void main(String[] args) throws InterruptedException {
    
        //API-- 3、acceptEither: 其中一个CompletableFuture执行完,就将结果传递到另一个Function中。
        CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"this is one future...");
            return 1;
        }).acceptEither(CompletableFuture.supplyAsync(()->{
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"this is two future...");
                    return 2;
                }), System.out::println
        );



        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(10000);
    }
}

输出结果:

ForkJoinPool.commonPool-worker-5this is two future...
2
ForkJoinPool.commonPool-worker-19this is one future...

4、runAfterEither :其中一个CompletableFuture执行完,就可以做其它操作了。不会将结果传递,可以做类似其中一个操作完后的消息通知功能。

package com.example.study.java8.completableFutures.api;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureAction2 {
    public static void main(String[] args) throws InterruptedException {

        //API-- 4、runAfterEither: 其中一个CompletableFuture执行完,就可以做其它操作了。不会将结果传递,可以做类似其中一个操作完后的消息通知功能。
        CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"this is one future...");
            return 1;
        }).runAfterEither(CompletableFuture.supplyAsync(()->{
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"this is two future...");
                    return 2;
                }),()-> System.out.println("其中一个future执行完成")
        );


        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(10000);
    }
}

输出结果:

ForkJoinPool.commonPool-worker-5this is two future...
其中一个future执行完成
ForkJoinPool.commonPool-worker-19this is one future...

5、 allOf: 静态方法,可直接调用。全部future执行完后,再进行消费

package com.example.study.java8.completableFutures.api;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

public class CompletableFutureAction2 {
    public static void main(String[] args) throws InterruptedException {

        //API-- 5、allOf: 静态方法,可直接调用。全部future执行完后,再进行消费
        List<CompletableFuture<Double>> completableFutureList = Arrays.asList(1, 2, 3, 4, 5)
                .stream().map(i -> CompletableFuture.supplyAsync((CompletableFutureAction2::get)))
                .collect(toList());

        CompletableFuture[] completableFuturesArray = completableFutureList.toArray(new CompletableFuture[completableFutureList.size()]);

        CompletableFuture.allOf(completableFuturesArray).thenRun(()-> System.out.println("所有future执行完成"));


        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(50000);
    }
}

输出结果:

0.5653558041825968
0.5720868499613329
0.25643461386243427
0.9522248509043018
0.5483123698045103
所有future执行完成

6、anyOf :静态方法,可直接调用。其中一个future执行完后,就进行消费

package com.example.study.java8.completableFutures.api;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

public class CompletableFutureAction2 {
    private final static Random RANDOM = new Random(System.currentTimeMillis());
    public static void main(String[] args) throws InterruptedException {

        //API-- 6、anyOf: 静态方法,可直接调用。其中一个future执行完后,就进行消费
        List<CompletableFuture<Double>> futureList = Arrays.asList(1, 2, 3, 4, 5)
                .stream()
                .map(i -> CompletableFuture.supplyAsync(CompletableFutureAction2::get))
                .collect(toList());

        CompletableFuture[] futuresArray = futureList.toArray(new CompletableFuture[futureList.size()]);

        CompletableFuture.anyOf(futuresArray).thenRun(()-> System.out.println("其中一个future已执行完成"));


        //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒
        Thread.sleep(50000);
    }

    static double get(){
        try {
            Thread.sleep(RANDOM.nextInt(100));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        double value = RANDOM.nextDouble();
        System.out.println(value);
        return value;
    }
}

输出结果:

0.41392777550489923
其中一个future已执行完成
0.7321854786542472
0.015636586751138104
0.8968276147964326
0.26576407363892185

AVA8-CompletableFuture常用API:

1、runAfterBoth

2、applyToEither

3、acceptEither

4 、runAfterEither

5 、allOf

6、anyOf

3

评论 (0)

取消