Java CompletableFuture的使用详解

CompletableFuture​

它代表某个同步或异步计算的一个阶段。你可以把它理解为是一个为了产生有价值最终结果的计算的流水线上的一个单元。这意味着多个指令可以链接起来从而一个阶段的完成可以触发下一个阶段的执行。

任务开启

supplyAsync 开启一个子线程去执行有返回结果

开启一个子线程用来执行执行事务,可以通过返回值的join来得到返回值.

例如:

print("去煮饭了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮饭中....");
 sleep();
 sleep();
 sleep();
 print("煮饭完成");
 return "盛米饭";
});
sleep();
print("炒完菜了");
sleep();
print(completableFuture.join()+"!开吃");

返回结果:

runAsync 开启一个子线程去执行无结果

任务结束

get\join 获得返回值

join 隐性抛出异常、get显性抛出异常

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
try {
 Assertions.assertEquals(future1.get(),8);
} catch (InterruptedException e) {
 e.printStackTrace();
} catch (ExecutionException e) {
 e.printStackTrace();
}
Assertions.assertEquals(future2.join(),9);

串行任务

thenApply\thenApplyAsync 串行将异步结果进行同步\异步的处理

​ 在当前阶段正常执行完成后(正常执行是指没有抛出异常)对前者的结果进行的操作。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 2).thenApply(t1 -> t1*2);
Assertions.assertEquals(future.join(),16);

handle\handleAsync 允许有异常的情况下任然进行异步任务执行

handle方法和 thenApply方法处理方式基本一样。不同的是 handle是在任务完成后再执行,还可以处理异常的任务。thenApply只可以执行正常的任务,任务出现异常则不执行 thenApply方法。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 0).handle((t1, e) -> {
 System.out.println("handle=" + e.getMessage());
 return Integer.MAX_VALUE;
});
Assertions.assertEquals(future.join(),Integer.MAX_VALUE);

thenAccept\thenAcceptAsync 同步\异步穿行消费前任务无返回结果

CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> getRemoteUser(familyName))
	.thenAccept(list -> list.forEach(System.out::println));
System.out.println(String.format("总执行耗时[%d]毫秒", stopwatch.elapsed(TimeUnit.MILLISECONDS)));
future.join();

thenRun\thenRunAsync 不关注前任务的执行结果

不关心任务的处理结果。只要上面的任务正确的执行完成,就开始执行。同样其也无返回值

 CompletableFuture future = CompletableFuture.supplyAsync(() -> 12 / 1).thenRun(() -> System.out.println("无返回值的执行"));
 System.out.println(future.join());

thenCompose\thenComposeAsync 允许多个任务Future流水线执行

​ 允许你对两个任务进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作。你可以将多个任务嵌套的进行见例2

例1:

print("去煮饭了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮饭中....");
 sleep();
 sleep();
 print("煮饭完成");
 return "米饭";
}).thenCompose(rice -> CompletableFuture.supplyAsync(() ->{
 print("洗碗");
 sleep();
 print("洗碗洗完了");
 return rice+"盛好了";
}));
sleep();
print("炒完菜了");
print(completableFuture.join()+"!开吃");

返回结果:

例2:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 2)
 .thenComposeAsync(t1 -> CompletableFuture.supplyAsync(() -> t1 / 2)
   .thenComposeAsync(t2 -> CompletableFuture.supplyAsync(() -> t2 / 2)));
Assertions.assertEquals(future.join(),2);

结论:可以看出supplyAsync执行了异步方法,thenCompose将上一个异步的结果(文中的rice)拿到以后通过一个线程去执行了当前异步任务,并将结果在future.join()中输出了。

whenComplete\whenCompleteAsync 串行将异步结果进行同步\异步的处理

​ 与thenAccept很像,区别在于whenComplete的执行会将前任务的返回结果给返回而thenAccept无返回结果。

//whenComplete
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future = future1.whenComplete((t1, e) -> {
 Assertions.assertEquals(Thread.currentThread().getName(),"main");
 Assertions.assertEquals(t1, 8);
 Assertions.assertNull(e);
 t1 = 10;
});
Assertions.assertEquals(future.join(), 8);
//thenAccept
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Void> future3 = future2.thenAccept(t1 -> {
 Assertions.assertEquals(Thread.currentThread().getName(), "main");
 Assertions.assertEquals(t1, 8);
});
Assertions.assertNull(future3.join());
//thenApply
CompletableFuture<Integer> future4 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future5 = future4.thenApply(t1 -> {
 Assertions.assertEquals(Thread.currentThread().getName(), "main");
 Assertions.assertEquals(t1, 8);
 return t1*2;
});
Assertions.assertEquals(future5.join(),16);
System.out.println("------OK-------");

并行任务

thenCombine 并列多任务执行并结果汇总​

同时执行两个异步任务,并且在最后通过BiFunction将两个结果综合起来进行结果输出.

例如:

print("去煮饭了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮饭中....");
 sleep();
 sleep();
 print("煮饭完成");
 return "米饭";
}).thenCombine(CompletableFuture.supplyAsync(() ->{
 print("洗碗");
 sleep();
 print("洗碗洗完了");
 return "碗好了";
}),(rice,bowl) -> {
 print("盛个饭");
 return "盛个饭";
});
sleep();
print("炒完菜了");
print(completableFuture.join()+"!开吃");

返回结果:

结论:可以看出supplyAsync执行了异步方法,thenCombine又起了一个新的线程并把两者的结果综合到一起(rice/bowl),由BiFunction进行计算,并将结果在future.join()中输出了。

thenAcceptBoth\thenAcceptBothAsync 并列多任务执行并消费结果无返回值

thenCombine差不多,区别是thenAcceptBoth无返回值

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2).thenApply(t1 -> t1/2);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3).thenApply(t1 -> t1/3);
CompletableFuture<Void> completableFuture = future1.thenAcceptBoth(future2, (t1, t2) -> {
 Assertions.assertEquals(t1 + t2, 7);
});
completableFuture.join();

applyToEither\applyToEitherAsync 两个任务并行进行用快的那个的结果作为后续处理

​ 两个任务,谁执行返回的结果快,我就用那个任务的结果进行下一步的操作。

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
 sleep(Integer.MAX_VALUE);
 return 16 / 2;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
CompletableFuture<Integer> future = future1.applyToEither(future2, t -> t);
Assertions.assertEquals(future.join(),9);
Assertions.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) < 1000);

runAfterBoth/runAfterBothAsync 两个任务都完成了不关注执行结果的进行下一步操作

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
 sleep(2000);
 return 16 / 2;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
CompletableFuture<Void> future = future1.runAfterBothAsync(future2,() -> System.out.println("1234"));
future.join();
Assertions.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) > 2000);

以上就是Java CompletableFuture的使用详解的详细内容,更多关于Java CompletableFuture的资料请关注我们其它相关文章!

(0)

相关推荐

  • Java并发 CompletableFuture异步编程的实现

    前面我们不止一次提到,用多线程优化性能,其实不过就是将串行操作变成并行操作.如果仔细观察,你还会发现在串行转换成并行的过程中,一定会涉及到异步化,例如下面的示例代码,现在是串行的,为了提升性能,我们得把它们并行化. // 以下两个方法都是耗时操作 doBizA(); doBizB(); //创建两个子线程去执行就可以了,两个操作已经被异步化了. new Thread(()->doBizA()) .start(); new Thread(()->doBizB()) .start(); 异步化,是

  • Java8 CompletableFuture详解

    Java 8来了,是时候学一下新的东西了.Java 7和Java 6只不过是稍作修改的版本,而Java 8将会发生重大的改进.或许是Java 8太大了吧?今天我会给你彻底地解释JDK 8中的新的抽象 – CompletableFuture.众所周知,Java 8不到一年就会发布,因此这篇文章是基于JDK 8 build 88 with lambda support的.CompletableFuture extends Future提供了方法,一元操作符和促进异步性以及事件驱动编程模型,它并不止步

  • Java8新的异步编程方式CompletableFuture实现

    一. Future JDK 5引入了Future模式.Future接口是Java多线程Future模式的实现,在java.util.concurrent包中,可以来进行异步计算. Future模式是多线程设计常用的一种设计模式.Future模式可以理解成:我有一个任务,提交给了Future,Future替我完成这个任务.期间我自己可以去做任何想做的事情.一段时间之后,我就便可以从Future那儿取出结果. Future的接口很简单,只有五个方法. public interface Future<

  • Java CompletableFuture的使用详解

    CompletableFuture​ 它代表某个同步或异步计算的一个阶段.你可以把它理解为是一个为了产生有价值最终结果的计算的流水线上的一个单元.这意味着多个指令可以链接起来从而一个阶段的完成可以触发下一个阶段的执行. 任务开启 supplyAsync 开启一个子线程去执行有返回结果 开启一个子线程用来执行执行事务,可以通过返回值的join来得到返回值. 例如: print("去煮饭了"); CompletableFuture<String> completableFutu

  • java异步编程CompletableFuture使用示例详解

    目录 一.简单介绍 二.常见操作 1.使用默认线程池 2.使用自定义线程池 3.获取线程的执行结果 三.处理异步结算的结果 四.异常处理 五.组合 CompletableFuture 六.并行运行多个 CompletableFuture 七.案例 1.从多个平台获取书价格 2.从任意一个平台获取结果就返回 一.简单介绍 CompletableFuture 同时实现了 Future 和 CompletionStage 接口. public class CompletableFuture<T> i

  • Java多线程开发工具之CompletableFuture的应用详解

    做Java编程,难免会遇到多线程的开发,但是JDK8这个CompletableFuture类很多开发者目前还没听说过,但是这个类实在是太好用了,了解它的一些用法后相信你会对它爱不释手(呸渣男,咋对谁都爱不释手呢),好了我先简单举个列子,告诉你用它有多好.Single Dog拿一个Appointment来举个列子,如下: /** * 女神化完妆之后,还需要一小会选衣服,不过分吧. * 也就是说我们现在有2个异步任务,第一个是化妆,第二个是选衣服. * 选衣服要在化妆完成之后进行,这两个任务是串行

  • Java中的静态内部类详解及代码示例

    1. 什么是静态内部类 在Java中有静态代码块.静态变量.静态方法,当然也有静态类,但Java中的静态类只能是Java的内部类,也称为静态嵌套类.静态内部类的定义如下: public class OuterClass { static class StaticInnerClass { ... } } 在介绍静态内部类之前,首先要弄清楚静态内部类与Java其它内部类的区别. 2. 内部类 什么是内部类?将一个类的定义放在另一个类的内部,就是内部类.Java的内部类主要分为成员内部类.局部内部类.

  • Java多线程ForkJoinPool实例详解

    引言 java 7提供了另外一个很有用的线程池框架,Fork/Join框架 理论 Fork/Join框架主要有以下两个类组成. * ForkJoinPool 这个类实现了ExecutorService接口和工作窃取算法(Work-Stealing Algorithm).它管理工作者线程,并提供任务的状态信息,以及任务的执行信息 * ForkJoinTask 这个类是一个将在ForkJoinPool执行的任务的基类. Fork/Join框架提供了在一个任务里执行fork()和join()操作的机制

  • java HttpServletRequest和HttpServletResponse详解

    java  HttpServletRequest和HttpServletResponse详解 最近由于CAS相关的JAR包的重新封装,所以想尽量做到0配置,而这个过程中大量使 用HttpServletRequest,现在整理如下,以便以后查阅.(表格为从别的地方复制的,排版渣了点,酬和看吧.) 请求与响应相关的类和接口非常多,下表是主要的与请求和接口相关的类以及接口. 主要的与请求和接口相关的类及接口 方    法 说    明 ServletInputStream Servlet的输入流 Se

  • java 抽象类的实例详解

    java 抽象类的实例详解 前言: 什么是抽象类?这名字听着就挺抽象的,第一次听到这个名字还真有可能被唬住.但是,就像老人家所说的,一切反动派都是纸老虎,一切有着装x名字的概念也是纸老虎.好吧,我们已经从战略上做到了藐视它,现在就要战术上重视它,如同要解决纸老虎,就要一个牙齿一个牙齿地敲,一个爪子一个爪子地拔:解决这种抽象概念也一样,先要把它具体化,细分化,然后一个一个地来. 我一般遇到新的概念都会问三个问题: 1.这个东西有什么用?用来干什么的?它的意义在哪里?(显然,如果是没用的东西,就没必

  • Java 多线程优先级实例详解

    Java 多线程优先级实例详解 线程的优先级将该线程的重要性传递给调度器.尽管CPU处理现有线程集的顺序是不确定的,但是调度器将倾向于让优先权最高的线程先执行. 你可以用getPriority()来读取现有线程的优先级,并且在任何时刻都可以通过setPriority()来修改优先级. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SimplePrio

  • Java NumberFormat 类的详解及实例

     Java NumberFormat 类的详解及实例 概要: NumberFormat 表示数字的格式化类, 即:可以按照本地的风格习惯进行数字的显示. 此类的定义如下: public abstract class NumberFormat extends Format MessageFormat .DateFormat .NumberFormat 是 Format 三个常用的子类,如果要想进一步完成一个好的国际化程序,则肯定需要同时使用这样三个类完成,根据不同的国家显示贷币的形式. 此类还是在

  • java LinkedList的实例详解

    java LinkedList的实例详解 站在Java的角度看,玩队列不就是玩对象引用对象嘛! 实例代码: public class LinkedList<E> implements List<E>, Deque<E> { Node<E> first; Node<E> last; int size; public boolean add(E e) { final Node<E> l = last; final Node<E>

随机推荐