解决 CompletableFuture.runAsync 异步线程不执行的问题

场景

接口执行较慢,直接采用异步处理,复杂的业务流程由异步线程去做。伪代码如下:

public static void main(String[] args) throws InterruptedException, ExecutionException {
        CompletableFuture.runAsync(() ->{
            try {
                method();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println("main +++++++++++++++++++++++");
    }

    private static void method() throws InterruptedException {
        System.out.println("start==================");
        Thread.sleep(10000);
        System.out.println("end====================");
    }

控制台打印结果如下:

"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\Soft\IntelliJ ...
cn.net.hylink.data.service.imple.demo
main +++++++++++++++++++++++
start==================

Process finished with exit code 0

发现并没有打印 System.out.println(“end====================”);

原因

代码中通过Thread.sleep(10000),导致异步线程阻塞了10s,主线程执行完,异步线程挂了。。。为啥挂了,等有时间看下,或者评论告诉我。

解决

1)使用使用CompletableFuture的get方法来等待异步任务完成并获取结果

代码如下:

public static void main(String[] args) throws InterruptedException, ExecutionException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() ->{
            try {
                method();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println("main +++++++++++++++++++++++");
        future.get();
    }

    private static void method() throws InterruptedException {
        System.out.println("start==================");
        Thread.sleep(10000);
        System.out.println("end====================");
    }

控制台打印结果如下:

"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\Soft\IntelliJ ...
main +++++++++++++++++++++++
start==================
end====================

Process finished with exit code 0

2) 指定线程池

代码如下:

private static final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
         CompletableFuture.runAsync(() ->{
            try {
                method();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },cachedThreadPool);
        System.out.println("main +++++++++++++++++++++++");
    }

    private static void method() throws InterruptedException {
        System.out.println("start==================");
        Thread.sleep(10000);
        System.out.println("end====================");
    }

控制台打印结果如下:

"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\Soft\IntelliJ ...
main +++++++++++++++++++++++
start==================
end====================

Process finished with exit code 0

补充

异步线程抛异常,外部try{}catch(){}捕捉不到。

有错误欢迎指正,有问题欢迎提问。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值