JavaKeyPoint:CompletableFuture 异步执行多业务逻辑

常规开发中经常有三个场景:

  1. 提前响应前端,响应前异步处理其它业务,对结果不关心;
  2. 根据主键或者其他批量查询,量太大需要拆批查询;
  3. 查主表信息,再查主表对应的多张表或者多个业务信息,这些附属信息之间无关联,需要结果;

1.普通异步处理,对结果不关心

public class AsyncOthers {
    public static void main(String[] args) {
        try {
            // 正常业务执行
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        CompletableFuture.runAsync(AsyncOthers::method1, Executors.newFixedThreadPool(10));
        System.out.println("return 前端:" + System.currentTimeMillis());
    }

    /**
     * 需要异步执行的方法
     */
    private static void method1() {
        try {
            // 该方法执行一次需要5秒
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("异步执行: " + System.currentTimeMillis());
    }
}
//return 前端:1679720215927
//异步执行: 1679720220936

 2. 根据主键或者其他批量查询,量太大需要拆批查询, 需要结果

public class AsyncSameMethod {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        // 分片
        List<String> prdCodes = Lists.newArrayList("1", "2", "3");
        List<List<String>> partitionList = Lists.partition(prdCodes, 1);

        // 根据分片异步查询
        List<CompletableFuture<String>> compList = Lists.newArrayList();
        partitionList.forEach(p -> {
            CompletableFuture<String> completableFuture = CompletableFuture
                    .supplyAsync(AsyncSameMethod::method1, Executors.newFixedThreadPool(10));
            compList.add(completableFuture);
        });

        // 封装数据
        StringBuilder sb = new StringBuilder();
        CompletableFuture
                .allOf(compList.toArray(new CompletableFuture[0]))
                .whenComplete((v1, v2) -> {
                    compList.forEach(c -> {
                        String str = null;
                        try {
                            str = c.get();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        sb.append(str);
                    });
                })
                .join();
        System.out.println(sb);
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
    }
    // method1 method1 method1
    // 耗时:5080

    private static String method1() {
        try {
            // 该方法执行一次需要5秒
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "method1 ";
    }
}

3. 查主表信息,再查主表对应的多个无关联业务信息,需要结果

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;

/**
 * 异步执行不同业务 每一个方法相当于一个业务逻辑 有的执行需要5秒 有的需要10秒
 */
public class AsyncDifferentMethod {
    public static void main(String[] args) {
        long syncStart = System.currentTimeMillis();
        syncMethod();
        long syncEnd = System.currentTimeMillis();
        System.out.println("sync耗时:" + (syncEnd - syncStart));

        System.out.println("====================================");

        System.out.println("asyncStart!!!");
        long asyncStart = System.currentTimeMillis();
        asyncMethod();
        long asyncEnd = System.currentTimeMillis();
        System.out.println("sync耗时:" + (asyncEnd - asyncStart));
    }
    //syncStart!!!
    //method1 method2 method3
    //sync耗时:20027
    //====================================
    //asyncStart!!!
    //method1 method2 method3
    //sync耗时:10065

    private static void syncMethod() {
        System.out.println("syncStart!!!");
        String s1 = method1();
        String s2 = method2();
        String s3 = method3();
        System.out.println(s1 + " " + s2 + " " + s3);
    }

    private static void asyncMethod() {
        CompletableFuture<String> comp1 = CompletableFuture
                .supplyAsync(AsyncDifferentMethod::method1, Executors.newFixedThreadPool(3));
        CompletableFuture<String> comp2 = CompletableFuture
                .supplyAsync(AsyncDifferentMethod::method2, Executors.newFixedThreadPool(3));
        CompletableFuture<String> comp3 = CompletableFuture
                .supplyAsync(AsyncDifferentMethod::method3, Executors.newFixedThreadPool(3));
        StringBuilder sb = new StringBuilder();
        CompletableFuture
                .allOf(comp1, comp2, comp3)
                .whenComplete((v1, v2) -> {
                    try {
                        String s4 = comp1.get();
                        String s5 = comp2.get();
                        String s6 = comp3.get();
                        sb.append(s4).append(" ").append(s5).append(" ").append(s6);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                })
                // 这里的join一定要加 否则代码并不会等待comp执行完任务
                .join();
        System.out.println(sb.toString());
    }



    private static String method1() {
        try {
            // 该方法执行一次需要5秒
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "method1";
    }

    private static String method2() {
        try {
            // 该方法执行一次需要5秒
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "method2";
    }

    private static String method3() {
        try {
            // 该方法执行一次需要10秒
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "method3";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值