Java8 异步非阻塞做法:CompletableFuture 两万字详解

本文详细介绍了Java8中的CompletableFuture,它实现了CompletionStage接口和Future接口,增强了异步回调和流式处理能力。文章通过示例展示了如何创建异步任务,包括submit、supplyAsync和runAsync的使用,以及如何通过thenApply、thenAccept、thenRun等方法进行异步回调。此外,还探讨了异常处理和多个CompletableFuture的组合操作,如allOf和anyOf的应用。
摘要由CSDN通过智能技术生成

CompletableFuture实现了CompletionStage接口和Future接口,前者是对后者的一个扩展,增加了异步回调、流式处理、多个Future组合处理的能力,使Java在处理多任务的协同工作时更加顺畅便利。

一、创建异步任务

1、Future.submit

通常的线程池接口类ExecutorService,其中execute方法的返回值是void,即无法获取异步任务的执行状态,3个重载的submit方法的返回值是Future,可以据此获取任务执行的状态和结果,示例如下:

 
 

@Test public void test3() throws Exception { // 创建异步执行任务: ExecutorService executorService= Executors.newSingleThreadExecutor(); Future<Double> cf = executorService.submit(()->{ System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis()); try { Thread.sleep(2000); } catch (InterruptedException e) { } if(false){ throw new RuntimeException("test"); }else{ System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis()); return 1.2; } }); System.out.println("main thread start,time->"+System.currentTimeMillis()); //等待子任务执行完成,如果已完成则直接返回结果 //如果执行任务异常,则get方法会把之前捕获的异常重新抛出 System.out.println("run result->"+cf.get()); System.out.println("main thread exit,time->"+System.currentTimeMillis()); }

执行结果如下:

添加图片注释,不超过 140 字(可选)

子线程是异步执行的,主线程休眠等待子线程执行完成,子线程执行完成后唤醒主线程,主线程获取任务执行结果后退出。

很多博客说使用不带等待时间限制的get方法时,如果子线程执行异常了会导致主线程长期阻塞,这其实是错误的,子线程执行异常时其异常会被捕获,然后修改任务的状态为异常结束并唤醒等待的主线程,get方法判断任务状态发生变更,就终止等待了,并抛出异常。将上述用例中if(false)改成if(true) ,执行结果如下:

添加图片注释,不超过 140 字(可选)

get方法抛出异常导致主线程异常终止。

2、supplyAsync / runAsync

supplyAsync表示创建带返回值的异步任务的,相当于ExecutorService submit(Callable<T> task) 方法,runAsync表示创建无返回值的异步任务,相当于ExecutorService submit(Runnable task)方法,这两方法的效果跟submit是一样的,测试用例如下:

 
 

@Test public void test2() throws Exception { // 创建异步执行任务,有返回值 CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{ System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis()); try { Thread.sleep(2000); } catch (InterruptedException e) { } if(true){ throw new RuntimeException("test"); }else{ System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis()); return 1.2; } }); System.out.println("main thread start,time->"+System.currentTimeMillis()); //等待子任务执行完成 System.out.println("run result->"+cf.get()); System.out.println("main thread exit,time->"+System.currentTimeMillis()); } Test public void test4() throws Exception { // 创建异步执行任务,无返回值 CompletableFuture cf = CompletableFuture.runAsync(()->{ System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis()); try { Thread.sleep(2000); } catch (InterruptedException e) { } if(false){ throw new RuntimeException("test"); }else{ System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis()); } }); System.out.println("main thread start,time->"+System.currentTimeMillis()); //等待子任务执行完成 System.out.println("run result->"+cf.get()); System.out.println("main thread exit,time->"+System.currentTimeMillis()); }

这两方法各有一个重载版本,可以指定执行异步任务的Executor实现,如果不指定,默认使用ForkJoinPool.commonPool(),如果机器是单核的,则默认使用ThreadPerTaskExecutor,该类是一个内部类,每次执行execute都会创建一个新线程。测试用例如下:

 
 

@Test public void test2() throws Exception { ForkJoinPool pool=new ForkJoinPool(); // 创建异步执行任务: CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{ System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis()); try { Thread.sleep(2000); } catch (InterruptedException e) { } if(true){ throw new RuntimeException("test"); }else{ System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis()); return 1.2; } },pool); System.out.println("main thread start,time->"+System.currentTimeMillis()); //等待子任务执行完成 System.out.println("run result->"+cf.get()); System.out.println("main thread exit,time->"+System.currentTimeMillis()); } @Test public void test4() throws Exception { ExecutorService executorService= Executors.newSingleThreadExecutor(); // 创建异步执行任务: CompletableFuture cf = CompletableFuture.runAsync(()->{ System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis()); try { Thread.sleep(2000); } catch (InterruptedException e) { } if(false){ throw new RuntimeException("test"); }else{ System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis()); } },executorService); System.out.println("main thread start,time->"+System.currentTimeMillis()); //等待子任务执行完成 System.out.println("run result->"+cf.get()); System.out.println("main thread exit,time->"+System.currentTimeMillis()); }

二、异步回调

1、thenApply / thenApplyAsync

thenApply 表示某个任务执行完成后执行的动作,即回调方法,会将该任务的执行结果即方法返回值作为入参传递到回调方法中,测试用例如下:

 
 

@Test public void test5() throws Exception { ForkJoinPool pool=new ForkJoinPool(); // 创建异步执行任务: CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{ System.out.println(Thread.currentThread()+" start job1,time->"+System.current

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值