谷粒商城异步编排(三十一)

CompletableFuture异步编排(193-202)

/**
 * @author WGR
 * @create 2020/7/7 -- 19:34
 */
public class ThreadTest {
    public static ExecutorService executor =  Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        System.out.println("main......start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).whenCompleteAsync((t, e) -> {
            System.out.println("结果是:" + t + "异常:" + e);
        }).exceptionally(throwable -> {
            return 10;
        });
        Integer integer = future.get();
        System.out.println("main......end");
    }
}

代码:

  public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).handle((res, thr) -> {
            if (res!=null) {
                return res*2;
            }
            if(thr!=null){
                return 0;
            }
            return 0;
        });
        Integer integer = future.get();
        System.out.println("main......end"+integer);
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
         CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenRunAsync(() ->{
            System.out.println("线程二开始");
        },executor);
        System.out.println("main......end");
    }

日志:

main......start
main......end
当前线程:11
运行结果:5
线程二开始

总结:thenRunAsync,不能获取上一步执行的数据,也没结果

public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
         CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenAcceptAsync( i ->{
             System.out.println("任务二拿到的结果:" + i);
         });
        System.out.println("main......end");
    }
main......start
main......end
当前线程:11
运行结果:5
任务二拿到的结果:5

总结:thenAcceptAsync能获取上一步执行的数据,没结果

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).thenApplyAsync(res -> {
            System.out.println("任务2启动了。。。。" + res);
            return res * 2;
        }, executor);
        System.out.println(future.get());
        System.out.println("main......end");
    }
main......start
当前线程:11
运行结果:5
任务2启动了。。。。5
10
main......end

两个任务组合之都要完成

thenCombine:组合2个future,获取2个future的返回结果,并返回当前任务的返回值。

thenAcceptBoth:组合2个future,获取2个future任务的返回结果,然后处理任务,没用返回值。

runAfter:组合2个future,不需要获取future的结果,只需2个future处理完任务后,处理该任务。

    public static void main(String[] args)  {
        System.out.println("main......start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        future01.runAfterBothAsync(future02,()->{
            System.out.println("任务3开始。。。");
        },executor);
        System.out.println("main......end");
    }

结果:

main......start
main......end
任务1线程:11
任务1结束
任务2线程:12
任务2结束
任务3开始。。。

代码:

    public static void main(String[] args)  {
        System.out.println("main......start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        future01.thenAcceptBothAsync(future02,(f1,f2)->{
            System.out.println("任务3开始。。。之前的结果"+f1+">>>"+f2);
        },executor);
        System.out.println("main......end");
    }

日志:

main......start
main......end
任务1线程:11
任务2线程:12
任务1结束
任务2结束
任务3开始。。。之前的结果3>>>Hello
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
            return "结果" + f1 + ">>>" + f2;
        }, executor);
        System.out.println("main......end"+future.get());
    }

结果:

main......start
任务1线程:11
任务1结束
任务2线程:12
任务2结束
main......end结果3>>>Hello

2个任务一个完成

applyToEither:2个任务有一个执行完成,获取它的返回值,处理任务有新的返回值。

acceptEither:2个任务有一个执行完成,获取它的返回值,处理任务,没有新的返回值。

applyToEither:2个任务有一个执行完成,不需要获取future的结果,处理任务,也没有返回值

public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        future01.runAfterEitherAsync(future02,()->{
            System.out.println("任务3开始");
        },executor);
        System.out.println("main......end");
    }

日志:

main......start
任务1线程:11
任务1结束
任务2线程:12
main......end
任务3开始
任务2结束
  public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        future01.acceptEitherAsync(future02,(res)->{
            System.out.println("任务3开始"+res);
        },executor);
        System.out.println("main......end");
    }

日志:

main......start
任务1线程:11
任务1结束
任务2线程:12
main......end
任务3开始3
任务2结束
public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return "3";
        }, executor);
        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
            System.out.println("任务3开始." + res);
            return "dalianpai";
        }, executor);
        System.out.println("main......end"+future.get());
    }

日志:

main......start
任务1线程:11
任务1结束
任务2线程:12
任务3开始.3
main......enddalianpai
任务2结束

代码:

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hellp.jpg";
        }, executor);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        }, executor);
        CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, executor);
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        allOf.get();  //阻塞
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        Object o = anyOf.get();
        System.out.println("main......end"+o);
    }

日志:

main......start
查询商品的图片信息
查询商品的属性
main......endhellp.jpg
查询商品介绍
posted @ 2020-07-07 22:32  天宇轩-王  阅读(405)  评论(0编辑  收藏  举报