一、CompletableFuture方法的介绍和使用二、总结1、CompletableFuture方法的介绍和使用我们对CompletableFuture有了一个大概的了解和初步的使用。接下来我们系统的介绍一下CompletableFuture类的方法。1.0基本运行方法//无返回值,类似于Runnable,可以选择传入一个线程池publicstaticCompletableFuturerunAsync(Runnablerunnable)publicstaticCompletableFuturerunAsync(Runnablerunnable,Executorexecutor)//有返回值,和Future类似,可以选择传入一个线程池>supplier,Executorexecutor)1.1获取结果和触发计算的方法publicTget();//不断阻塞获取结果,和Future的get()方法一样publicTget(longtimeout,TimeUnitunit);//在一定时间内获取结果,如果没有获取到结果抛出异常publicTgetNow(TvalueIfAbsent);//立即获取结果,如果没有获取到结果,返回默认值传入参数。公共T加入();//方法和get()一样,不同的是join()不会抛异常,get()会抛异常publicbooleancomplete(Tvalue)//当计算没有完成时,立即中断get方法并立即返回括号值1.2处理计算结果的方法thenApply()//计算结果之间存在依赖关系,先执行上一步再执行下一步//由于存在依赖关系(当前步错误,不往下走一步),如果当前步出现异常,则停止。publicclassCompletableFutureDemo2{publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{//当一个线程依赖另一个线程时,使用thenApply方法序列化两个线程,CompletableFuture.supplyAsync(()->{//暂停线程几秒钟try{TimeUnit.SECONDS.sleep(1);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println("111");return1024;})。thenApply(f->{System.out.println("222");returnf+1;}).thenApply(f->{//intage=10/0;//异常情况:如果有则停止那一步错误。System.out.println("333");返回f+1;}).whenCompleteAsync((v,e)->{//完成时System.out.println("*****v:"+v);}).exceptionally(e->{//异常时发生e.printStackTrace();returnnull;});System.out.println("-----主线程结束,END");//主线程不要立即结束,否则CompletableFuture使用的默认线程池会立即关闭:try{TimeUnit.SECONDS.sleep(2);}catch(InterruptedExceptione){e.printStackTrace();}}}//如果有异常,可以进行下一步,可以根据异常参数进一步处理。handle()publicclassCompletableFutureDemo2{publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{//当一个线程依赖于另一个线程的handle方法来序列化这两个线程时使用,//异常情况:可以去如果有异常则下一步。CompletableFuture.supplyAsync(()->{//暂停线程几秒钟try{TimeUnit.SECONDS.sleep(1);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println("111");return1024;}).handle((f,e)->{intage=10/0;System.out.println("222");returnf+1;}).handle((f,e)->{System.out.println("333");返回f+1;}).whenCompleteAsync((v,e)->{System.out.println("*****v:"+v);}).exceptionally(e->{e.printStackTrace();returnnull;});System.out.println("-----主线程结束,END");//不要立即结束主线程,否则CompletableFuture会默认立即关闭使用的线程池:try{TimeUnit.SECONDS.sleep(2);}catch(InterruptedExceptione){e.printStackTrace();}}}1.3消费计算结果的方法//接收任务的处理结果,消费处理,无返回结果thenAccept();//任务A执行完B,B不需要A的thenRun()的结果;//任务A执行完B,B需要A的结果,但是任务B没有返回值thenAccept();//任务A执行完B,B需要A的结果,任务B有返回值thenApply();publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{CompletableFuture.supplyAsync(()->{return1;}).thenApply(f->{returnf+2;}).thenApply(f->{returnf+3;}).thenApply(f->{returnf+4;}).thenAccept(r->System.out.println(r));}1.4选择计算速度的方法//谁用谁它很快applyToEither()publicclassCompletableFutureDemo2{publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{CompletableFuturecompletableFuture1=CompletableFuture.supplyAsync(()->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");//暂停几秒线程序try{TimeUnit.SECONDS.sleep(2);}catch(InterruptedExceptione){e.printStackTrace();}return10;});CompletableFuturecompletableFuture2=CompletableFuture.supplyAsync(()->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");try{TimeUnit.SECONDS.sleep(1);}catch(InterruptedExceptione){e.printStackTrace();}return20;});CompletableFuturethenCombineResult=completableFuture1.applyToEither(completableFuture2,f->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");returnf+1;});System.out.println(Thread.currentThread().getName()+"\t"+thenCombineResult.get());}}1.5合并计算结果的方法//两个CompletionStage任务都完成后,两个任务的结果最终可以交给thenCombine处理thenCombine()publicclassCompletableFutureDemo2{publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{CompletableFuturecompletableFuture1=CompletableFuture.supplyAsync(()->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");返回10;});CompletableFuturecompletableFuture2=CompletableFuture.supplyAsync(()->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");return20;});CompletableFuturethenCombineResult=completableFuture1.thenCombine(completableFuture2,(x,y)->{System.out.println(Thread.currentThread().getName()+"\t"+"---进来");返回x+y;});System.out.println(thenCombineResult.get());}}2.为了总结这篇博客我们总结了CompletableFutrue的基本方法,生产中还需要多练习,忘记api的时候可以看看源码或者再看这篇博客