CompletableFuture学习笔记

创建异步对象

CompletableFuture.runAsync(new Runnable() {
    @Override
    public void run() {
        System.out.println("当前线程:"+ Thread.currentThread().getId());
        int i = 10/2;
        System.out.println("任务1运行结果:"+ i);
    }
}, executor);
Integer integer = future.get();

计算完成时回调方法

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
    //完成后的处理
}, executor).whenComplete(new BiConsumer<Integer, Throwable>() {
    @Override
    public void accept(Integer integer, Throwable throwable) {
        System.out.println("异步任务成功完成了。。。结果是。"+integer+"异常是:"+throwable);
    }
    //异常情况下的处理
}).exceptionally(new Function<Throwable, Integer>() {
    @Override
    public Integer apply(Throwable throwable) {
        //感知异步情况默认返回
        return 10;
    }
});
Integer integer = future.get();

handler方法

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor).handle(new BiFunction<Integer, Throwable, Integer>() {
    @Override
    public Integer apply(Integer result, Throwable throwable) {
        if(result!=null) {
            return result*2;
        }
        if(throwable!=null){
            return 0;
        }
        return 0;
    }
});
Integer integer = future.get();

线程串行化

  • 不能感知上一步返回结果
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor).thenRunAsync(new Runnable() {
    @Override
    public void run() {
        System.out.println("任务2启动了");
    }
});
  • 能感知上一步返回结果,无返回值
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor).thenAcceptAsync(new Consumer<Integer>() {
    @Override
    public void accept(Integer integer) {
        System.out.println("任务2启动了。。。"+integer);
    }
});
  • 既能感知上一步结果,又能有返回值
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor).thenApplyAsync(new Function<Integer, String>() {
    @Override
    public String apply(Integer integer) {
        System.out.println("任务2启动了");
        return "任务2启动的返回,并携带上一步信息"+integer;
    }
}, executor);
System.out.println(future.get());

两个线程都完成

  • 无返回值
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor);

CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
    @Override
    public String get() {
        System.out.println("任务2线程:" + Thread.currentThread().getId());
        System.out.println("任务2结束并返回'hello'");
        return "hello";
    }
}, executor);
future01.runAfterBothAsync(future02, new Runnable() {
    @Override
    public void run() {
        System.out.println("任务3线程开始。。。");
    }
},executor);
  • 获取两个线程返回值
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor);

CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
    @Override
    public String get() {
        System.out.println("任务2线程:" + Thread.currentThread().getId());
        System.out.println("任务2结束并返回'hello'");
        return "hello";
    }
}, executor);
future01.thenAcceptBothAsync(future02, new BiConsumer<Integer, String>() {
    @Override
    public void accept(Integer integer, String s) {
        System.out.println("任务3开始。。。之前接过:"+integer+"-->>>"+s);
    }
},executor);
  • 获取两个线程的返回值并处理
System.out.println("main---start---");
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor);

CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
    @Override
    public String get() {
        System.out.println("任务2线程:" + Thread.currentThread().getId());
        System.out.println("任务2结束并返回'hello'");
        return "hello";
    }
}, executor);
CompletableFuture<Object> future = future01.thenCombineAsync(future02, new BiFunction<Integer, String, Object>() {
    @Override
    public Object apply(Integer integer, String s) {
        return integer + ":" + s + "-->>haha";
    }
}, executor);
System.out.println("main...end..."+future.get());

完成任意一个线程返回结果

  • 不带返回值
System.out.println("main---start---");
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    @Override
    public Integer get() {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
        return i;
    }
}, executor);

CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
    @Override
    public String get() {
        System.out.println("任务2线程:" + Thread.currentThread().getId());
        try {
            Thread.sleep(3000);
            System.out.println("任务2结束并返回'hello'");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello";
    }
}, executor);
future01.runAfterEither(future02, new Runnable() {
    @Override
    public void run() {
        System.out.println("任务3开始。。。执行结果");
    }
});
System.out.println("main...end...");
  • 感知优先返回数据的线程信息,无返回值
System.out.println("main---start---");
CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
    int i = 0;
    try {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        Thread.sleep(2000);
        i = 10 / 2;
        System.out.println("任务1运行结果:" + i);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return i;
}, executor);

CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
    System.out.println("任务2线程:" + Thread.currentThread().getId());
    System.out.println("任务2结束并返回'hello'");
    return "hello";
}, executor);
future01.acceptEitherAsync(future02, result -> {
    System.out.println("感知到的返回信息"+result);
},executor);
System.out.println("main...end...");

多任务组合

  • 多个任务都执行完成
System.out.println("main---start---");

        CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync(() -> {
        System.out.println("查询商品图片信息");
        return "hello.jpg";
        }, executor);
        CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync(() -> {
        System.out.println("查询商品属性信息");
        return "黑色+256G";
        }, executor);
        CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
        System.out.println("查询商品详情信息");
        return "华为P40";
        }, executor);

//全部任务执行完后处理
        CompletableFuture.allOf(futureImg,futureAttr,futureDesc);

        System.out.println("main...end..."+futureImg.get()+"->"+futureAttr.get()+"->"+futureDesc.get());

  • 多个任务任意一个完成
System.out.println("main---start---");

CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync(() -> {
    System.out.println("查询商品图片信息");
    return "hello.jpg";
}, executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync(() -> {
    System.out.println("查询商品属性信息");
    return "黑色+256G";
}, executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
    System.out.println("查询商品详情信息");
    return "华为P40";
}, executor);

//任意任务执行完后处理
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);

System.out.println("main...end..."+anyOf.get());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值