zoukankan      html  css  js  c++  java
  • java8新特性CompletableFuture

    public class Main {
        public static void main(String[] args) throws Exception {
            // 两个CompletableFuture执行异步查询:
            CompletableFuture<String> cfQueryFromSina = CompletableFuture.supplyAsync(() -> {
                return queryCode("中国石油", "https://finance.sina.com.cn/code/");
            });
            CompletableFuture<String> cfQueryFrom163 = CompletableFuture.supplyAsync(() -> {
                return queryCode("中国石油", "https://money.163.com/code/");
            });
    
            // 用anyOf合并为一个新的CompletableFuture:
            CompletableFuture<Object> cfQuery = CompletableFuture.anyOf(cfQueryFromSina, cfQueryFrom163);
    
            // 两个CompletableFuture执行异步查询:
            CompletableFuture<Double> cfFetchFromSina = cfQuery.thenApplyAsync((code) -> {
                return fetchPrice((String) code, "https://finance.sina.com.cn/price/");
            });
            CompletableFuture<Double> cfFetchFrom163 = cfQuery.thenApplyAsync((code) -> {
                return fetchPrice((String) code, "https://money.163.com/price/");
            });
    
            // 用anyOf合并为一个新的CompletableFuture:
            CompletableFuture<Object> cfFetch = CompletableFuture.anyOf(cfFetchFromSina, cfFetchFrom163);
    
            // 最终结果:
            cfFetch.thenAccept((result) -> {
                System.out.println("price: " + result);
            });
            // 主线程不要立刻结束,否则CompletableFuture默认使用的线程池会立刻关闭:
            Thread.sleep(200);
        }
    
        static String queryCode(String name, String url) {
            System.out.println("query code from " + url + "...");
            try {
                Thread.sleep((long) (Math.random() * 100));
            } catch (InterruptedException e) {
            }
            return "601857";
        }
    
        static Double fetchPrice(String code, String url) {
            System.out.println("query price from " + url + "...");
            try {
                Thread.sleep((long) (Math.random() * 100));
            } catch (InterruptedException e) {
            }
            return 5 + Math.random() * 20;
        }
    }

    除了anyOf()可以实现“任意个CompletableFuture只要一个成功”,allOf()可以实现“所有CompletableFuture都必须成功”,这些组合操作可以实现非常复杂的异步流程控制。

    最后我们注意CompletableFuture的命名规则:

    • xxx():表示该方法将继续在已有的线程中执行;
    • xxxAsync():表示将异步在线程池中执行。

    小结

    CompletableFuture可以指定异步处理流程:

    • thenAccept()处理正常结果;
    • exceptional()处理异常结果;
    • thenApplyAsync()用于串行化另一个CompletableFuture
    • anyOf()allOf()用于并行化多个CompletableFuture

    例如:

      CompletableFuture<Integer> a=CompletableFuture.supplyAsync(()->{
                return 1;
            });
            CompletableFuture<Integer> b=CompletableFuture.supplyAsync(()->{
                return 1;
            });
            CompletableFuture.allOf(a,b).join();

    参考:https://www.liaoxuefeng.com/wiki/1252599548343744

  • 相关阅读:
    JS计算字符串长度(兼容后端PHP)
    使用iview-admin2构建的项目,热更新无法启动
    关于HSTS的总结
    (转)javascript兼容问题总结
    js写一个通讯录
    HTML5+通讯录获取指定多个人的信息
    MUI封装的选择器调用
    心得小细节(一)
    读后感(一) web运作原理探析
    码农和软件开发工程师
  • 原文地址:https://www.cnblogs.com/liran123/p/14677590.html
Copyright © 2011-2022 走看看