CompletableFuture

  前面我们使用过jdk5 提出future的用法,但是在获取结果上并不是那么友好

  在Java8中,CompletableFuture提供了非常强大的Future的扩展功能,可以帮助我们简化异步编程的复杂性,并且提供了函数式编程的能力,可以通过回调的方式处理计算结果,也提供了转换和组合 CompletableFuture 的方法。

  没有用过的同学,我们先来一个入门使用了解一下

public class CompletableFutureAction {

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    public static void main(String[] args) {

        //很少有这么使用的.
CompletableFuture<Double> completableFuture = new CompletableFuture<>(); new Thread(()->{
double v = get();
completableFuture.complete(v);
}).start(); System.out.println("程序执行");
//当程序完成时自动回调,不需要阻塞
completableFuture.whenComplete((v,t)->{
Optional.ofNullable(v).ifPresent(System.out::println);
Optional.ofNullable(t).ifPresent(x->{t.printStackTrace();});
});
System.out.println("程序执行后..."); } static double get(){
try {
System.out.println("执行耗时任务");
Thread.sleep(RANDOM.nextInt(10000)); } catch (InterruptedException e) {
e.printStackTrace();
}
return RANDOM.nextDouble();
} }

CompletableFuture.supplyAsync

我们一般不使用new的方式创建completableFuture

在main 方法中我们在使用一个例子来介绍它的使用

因为在mian方法中,主线程可能会提前结束所以我们在做这个例子的时候,需要对线程做一些阻塞

public class CompletableFutureAction1 {
public static void main(String[] args) throws InterruptedException {
    //另一种方式来等到结果输出在结束
ExecutorService executorService = Executors.newFixedThreadPool(2,r -> {
Thread t = new Thread(r);
//设置成守护线程->结果不一定能等到执行结束
t.setDaemon(false);
return t;
});
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
CompletableFuture.supplyAsync(CompletableFutureAction::get,executorService).whenComplete((v,t)->
{
Optional.of(v).ifPresent(System.out::println);
atomicBoolean.set(false);
executorService.shutdown();
Optional.ofNullable(t).ifPresent(x->{t.printStackTrace();});
}); System.out.println("没有进入阻塞");
// Thread.currentThread().join();
/* while (atomicBoolean.get()){ }*/ executorService.execute(()-> System.out.println("2222")); }
} 我们可以使用这样的方式来判断获取到结果
  AtomicBoolean atomicBoolean = new AtomicBoolean(true);
CompletableFuture.supplyAsync(CompletableFutureAction::get).whenComplete((v,t)->
{
Optional.of(v).ifPresent(System.out::println);
atomicBoolean.set(false);
executorService.shutdown();
Optional.ofNullable(t).ifPresent(x->{t.printStackTrace();});
}); System.out.println("没有进入阻塞");
// Thread.currentThread().join();
while (atomicBoolean.get()){ }
 

comletableFuture流水线的工作

 CompletableFuture.supplyAsync(CompletableFutureAction::get,executorService)
.thenApply(CompletableFutureAction2::multiply).whenComplete((v,t)->{
Optional.of(v).ifPresent(System.out::println);
// executorService.shutdown();
Optional.ofNullable(t).ifPresent(x->{t.printStackTrace();} ); });
  public static double multiply(double value){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return value*10d;
}
   static double get(){
try {
System.out.println("=======执行耗时任务");
Thread.sleep(RANDOM.nextInt(10000)); } catch (InterruptedException e) {
e.printStackTrace();
}
double v = RANDOM.nextDouble();
System.out.println(v);
return v;
} 执行的结果:

=======执行耗时任务
没有进入阻塞
0.808110430680034
8.08110430680034

 

多任务调度方式模拟:

        List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5);

        Stream<CompletableFuture<Double>> completableFutureStream = ids.parallelStream().map(i -> CompletableFuture.supplyAsync(() -> queryByid(i), executorService));
  //这边也是并行执行
List<Double> collect = completableFutureStream.map(future -> future.thenApply(CompletableFutureAction2::multiply)).map(CompletableFuture::join).collect(Collectors.toList());
System.out.println(collect);
public static double queryByid(double i){
return CompletableFutureAction.get();
}
 

comletableFuture API

最新文章

  1. 正确获取访问者ip
  2. [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏
  3. 【SQL】区分新来顾客和再访顾客
  4. Mysql编码, Mysql编码流程, Mysql编码顺序, Mysql编码原理, Mysql编码修改依据
  5. linux下重启oracle服务:监听器和实例
  6. [Cocos2d-x For WP8]Transition 场景切换
  7. Java数据库连接——JDBC基础知识(操作数据库:增删改查)
  8. linix container &amp; cgroup note
  9. MM--发票校验 及基于采购订单的MIRO发票校验过程(
  10. JDE报表开发笔记(数据选择及继承)
  11. mvc 权限管理 demo
  12. ActiveMQ主从配置
  13. C# Interface显式实现和隐式实现
  14. Compass 更智能的搜索引擎(2)--进阶
  15. iframe 加form提交数据
  16. phpstudy 2016 切换Nginx+php7.0版本所需运行库 vc14 + 安装redis拓展
  17. html常用文本标签(转)
  18. tls 双向认证 client端代码例子
  19. java 1.8新特性(二) 关于 function 接口的使用
  20. Java Object part1

热门文章

  1. Linux 桌面玩家指南:16. 使用 CUDA 发挥显卡的计算性能
  2. leaflet 学习备忘
  3. 来一波C#发送邮件
  4. 从零开始学习PYTHON3讲义(十四)写一个mp3播放器
  5. Docker系列之入门篇
  6. 多元线性回归公式推导及R语言实现
  7. vue + element 动态渲染、移除表单并添加验证
  8. .net 笔试面试总结(1)
  9. vue -webkit-box-orient: vertical webpack打包后被过滤掉了 线上没有这行代码
  10. 5分钟入门LingaScript-尝鲜中文版TypeScript