FutureTask

state状态

1.NEW(初始状态)-COMPLETING(正在设置任务结果)->NORMAL 任务正常执行完毕状态的流程变更
2.NEW(初始状态)-COMPLETING(正在设置任务结果)->EXCEPTIONAL 任务执行过程中发生异常
3.NEW(初始状态) ->CANCELLED 任务被取消 调用 future.cancel()
4.NEW(初始状态)->INTERRUPTING(正在中断执行任务的线程)->INTERRUPTED(任务被中断)

callable 当前要执行的任务

outcome 任务的执行结果 最终通过 futrue.get()获取的值

runner 当前执行cakkable 任务的线程

waiter 单向链表 保存所有等待任务执行结束的线程

CompletableFuture

supplyAsync(_Supplier supplier)_

带有返回值的异步方法

  1. CompletableFuture.supplyAsync(()->{
  2. return "1";
  3. });

supplyAsync(_Supplier supplier,Executor executor)_

带返回值的异步执行方法 可以使用自定义线程池

runAsync(_Runnable runnable)_

不带返回值的异步执行方法 ,默认使用ForkjoinPoll

runAsync(_Runnable runnable,Executor executor)_

不带返回值的异步执行方法 使用自定义线程池

get()

阻塞等待执行结果 在唤醒继续执行
允许被中断

join()

阻塞等待执行结果
不允许被中断

allOf()

接收多个CompletableFuture无返回值的任务当 所有的CompletableFuture任务执行结束后 返回一个新的CompletableFuture对象

  1. CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
  2. System.out.println("111");
  3. });
  4. CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
  5. System.out.println("111");
  6. });
  7. CompletableFuture.allOf(voidCompletableFuture,voidCompletableFuture1).join();

anyOf()

接收多个 CompletableFuture带返回值的任务 当任何一个 CompletableFuture任务执行完成后返回一个新的CompletableFuture对象

thenAccept()

对 anyOf()返回的结果进行进一步的处理

  1. CompletableFuture<String> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
  2. return "1";
  3. });
  4. CompletableFuture<String> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
  5. return "2";
  6. });
  7. CompletableFuture.anyOf(voidCompletableFuture,voidCompletableFuture1).thenAccept(value-> System.out.println(value)).join();

completedFuture()

指定一个返回结果

  1. CompletableFuture.completedFuture("1");

getNow()

如果当前任务已经执行完成 则返回执行结果 否则返回传递进去的参数

消费类型的方法

依赖单个任务完成

thenAccept()
thenAcceptAsync()

依赖两个 任务完成

thenAcceptBoth()
thenAcceptBothAsync()

  1. CompletableFuture<String> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
  2. return "1";
  3. });
  4. CompletableFuture<String> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
  5. return "2";
  6. });
  7. voidCompletableFuture.thenAcceptBothAsync(voidCompletableFuture1,(r1,r2)->{
  8. System.out.println("111");
  9. });

依赖两个任务中的任何一个完成

acceptEither()
acceptEitherAsync()
两个任务中有一个完成就可以继续执行 方法

有返回值类型的方法

依赖单个任务完成

thenAppl()
thenApplyAsync()

依赖两个 任务完成

thenCombine()
thenCombineAsync()

依赖两个任务中的任何一个完成

applyToEither()
applyToEitherAsync()

不消费也不返回新值的方法

不依赖上一阶段返回结果 只要上一阶段完成 就执行指定的任务

依赖单个任务完成

thenRun()
thenRunAsync()

依赖两个 任务完成

runAfterBothAsync()
runAfterBoth()

依赖两个任务中的任何一个完成

runAfterEither()
runAfterEitherAsync()

组合类型的方法

把第一个任务的执行结果作为参数传递给第二个执行
thenCombineAsync()
thenCombine()

异常处理

whenComplete()

  1. .whenComplete((result, e) -> {
  2. if (e != null) {
  3. LogUtil.error(e,LOGGER, "Select Build Dic , {0}", e.getMessage());
  4. }

whenCompleteAsync()

handle

表示前置任务执行完成后 不管前置任务执行状态是正常还是异常 都执行其中的函数

handle()
handleAsync()

exceptionally

接收一个函数fn 当上一个任务出现异常时 会将该异常作为参数传递给函数fn
exceptionally()

源码

  1. volatile Object result; //表示CompletableFuture的返回结果或者一个异常对象的封装对象
  2. volatile Completion stack; //链式异步调用中传递的任务都会被压入 stack中