作家
登录

Java CompletableFuture详解

作者: 来源: 2017-12-22 09:13:58 阅读 我要评论

  •  
  • public CompletableFuture<T>  whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) 
  •  
  • public CompletableFuture<T>  exceptionally(Function<Throwable,? extends T> fn) 
    1. public CompletableFuture<Void>  thenAccept(Consumer<? super T> action
    2.  
    3. public CompletableFuture<Void>  thenAcceptAsync(Consumer<? super T> action
    4.  
    5. public CompletableFuture<Void>  thenAcceptAsync(Consumer<? super T> action, Executor executor) 

    留意这几个办法都邑返回CompletableFuture,当Action履行完毕后它的结不雅返回原始的CompletableFuture的计算结不雅或者返回异常。

    1. public class Main { 
    2.  
    3.     private static Random rand = new Random(); 
    4.  
    5.     private static long t = System.currentTimeMillis(); 
    6.  
    7.     static int getMoreData() { 
    8.  
    9.         System.out.println("begin to start compute"); 
    10.  
    11.         try { 
    12.  
    13.             Thread.sleep(10000); 
    14.  
    15.         } catch (InterruptedException e) { 
    16.  
    17.             throw new RuntimeException(e); 
    18.  
    19.         } 
    20.  
    21.         System.out.println("end to start compute. passed " + (System.currentTimeMillis() - t)/1000 + " seconds"); 
    22.  
    23.         return rand.nextInt(1000); 
    24.  
    25.     } 
    26.  
    27.     public static void main(String[] args) throws Exception { 
    28.  
    29.         CompletableFuture<Integer> future = CompletableFuture.supplyAsync(Main::getMoreData); 
    30.  
    31.         Future<Integer> f = future.whenComplete((v, e) -> { 
    32.  

        推荐阅读

        iOS中触摸事件的传递和响应机制

      【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞?【编辑推荐】2017 Android和iOS几回再三被曝Bug,它们都怎么了?谷歌苹不雅竟齐翻车!谈谈安卓8/iOS11那些BugiOS消息转发>>>详细阅读


      本文标题:Java CompletableFuture详解

      地址:http://www.17bianji.com/lsqh/40058.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)