CompletableFuture源码解析与应用实战

前言:我们在实际的业务代码中经常会出现这样的场景:业务调用方给我们发送一个指令, 希望我们接受到指令后立即返回表示已经收到指令,同时开始进行导出文件的操作,并上传到某个地方去,等这个过程全部结束后,再调用业务提供方的服务进行告知任务已经完成。这样的操作不会阻塞业务调用方的主流程,尤其在移动端的应用场景中,用户体验会比较好。我们这篇文章讨论使用jdk1.8中的一个新类CompletableFuture进行实现。

这种业务场景,我们想着大概有如下两种实现方案:

1、双方使用mq进行交互,基于生产-消费模式,双方都是mq或者kafka的生产和消费方,在接到消息之后再去做相应的事情。

2、利用java1.8才有的这个CompletableFuture异步完成的特性 supplyAsync

/**
 * A {@link Future} that may be explicitly completed (setting its
 * value and status), and may be used as a {@link CompletionStage},
 * supporting dependent functions and actions that trigger upon its
 * completion.
    CompletableFuture 是一个明确完成的设置值和状态的future,也可以当成CompletionStage使用,
支持独立的功能完成时候进行触发
 *
 * <p>When two or more threads attempt to
 * {@link #complete complete},
 * {@link #completeExceptionally completeExceptionally}, or
 * {@link #cancel cancel}
 * a CompletableFuture, only one of them succeeds.
 * 当两个或者以上的线程试图使用一个CompletableFuture对象的complete,completeExceptionally或者cancel 方法时,只有其中的一个线程可以成功。那么别的线程会返回什么呢?
 * <p>In addition to these and related methods for directly
 * manipulating status and results, CompletableFuture implements
 * interface {@link CompletionStage} with the following policies: <ul>
 * 除了这些相关的直接操作状态和结果的方法除外,CompletableFuture基于以下几种策略实现CompletionStage接口
 * <li>Actions supplied for dependent completions of
 * <em>non-async</em> methods may be performed by the thread that
 * completes the current CompletableFuture, or by any other caller of
 * a completion method.</li>
 *  为非异步方法的依赖完成提供的操作可以由完成当前CompletableFuture的线程执行,也可以由完成方法的任何其他调用方执行
 * <li>All <em>async</em> methods without an explicit Executor
 * argument are performed using the {@link ForkJoinPool#commonPool()}
 * (unless it does not support a parallelism level of at least two, in
 * which case, a new Thread is created to run each task).  To simplify
 * monitoring, debugging, and tracking, all generated asynchronous
 * tasks are instances of the marker interface {@link
 * AsynchronousCompletionTask}. </li>
 *所有没有显式执行器参数的异步方法都使用{@link ForkJoinPool}commonPool()执行(除非它不支持至少两个并行级别,否则在在这种情况下,将创建一个新线程来运行每个任务)。简化监视、调试和跟踪,所有这些都是异步生成的任务是标记接口{@link的实例AsynchronousCompletionTask}
 * <li>All CompletionStage methods are implemented independently of
 * other public methods, so the behavior of one method is not impacted
 * by overrides of others in subclasses.  </li> </ul>
 *<li>所有CompletionStage方法都是独立于其他公共方法实现的,因此一个方法的行为不会受到子类中其他方法重写的影响</li></ul>
 * <p>CompletableFuture also implements {@link Future} with the following
 * policies: <ul>
 *
 * <li>Since (unlike {@link FutureTask}) this class has no direct
 * control over the computation that causes it to be completed,
 * cancellation is treated as just another form of exceptional
 * completion.  Method {@link #cancel cancel} has the same effect as
 * {@code completeExceptionally(new CancellationException())}. Method
 * {@link #isCompletedExceptionally} can be used to determine if a
 * CompletableFuture completed in any exceptional fashion.</li>
 *
 * <li>In case of exceptional completion with a CompletionException,
 * methods {@link #get()} and {@link #get(long, TimeUnit)} throw an
 * {@link ExecutionException} with the same cause as held in the
 * corresponding CompletionException.  To simplify usage in most
 * contexts, this class also defines methods {@link #join()} and
 * {@link #getNow} that instead throw the CompletionException directly
 * in these cases.</li> </ul>
 *<p>CompletableFuture还使用以下策略实现{@link Future}:<ul>

<li>因为(与{@link FutureTask}不同),这个类对导致它完成的计算没有直接控制,所以取消被视为另一种形式的异常完成。方法{@link#cancel-cancel}与{@code completeeexceptional(new CancellationException())}具有相同的效果。方法{@link#isCompletedExceptionally}可用于确定一个CompletableFuture是否以任何异常方式完成</li>

<li>如果使用CompletionException进行异常完成,则方法{@link#get()}和{@link#get(long,TimeUnit)}会抛出一个{@link ExecutionException},其原因与相应CompletionException中的原因相同。为了简化在大多数Contexts中的使用,此类还定义了方法{@link#join()}和{@link#getNow},它们在这些情况下直接抛出CompletionException。</li></ul>
 * @author Doug Lea
 * @since 1.8
 */

我们利用下面的一些代码来验证下

package com.thread;

import sun.awt.windows.ThemeReader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @description: 两个线程的交替输出问题(wait,notify),另外还可以描述为主线程输出一些再转移到子线程
 * 再分析一下CompletableFuture的异步输出问题,用自定义线程池和默认线程池
 * @author: H.K
 * @create: 2021-08-30 10:40
 */
public class SwitchThread {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 200, 0L,
            TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024));
    public static void main(String[] args)  {
        System.out.println("main ===="+ new SwitchThread().completableFutureSupplyAsynDefaultPool());
        System.out.println("getActiveCount " +new SwitchThread().threadPoolExecutor.getActiveCount());

    }

    /**
     * 测试completableFuture#SupplyAsyn方法
     * @return
     */
    private  String completableFutureSupplyAsyn(){
        String res = "return message";
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                costTime();
                return "CompletableFuture supplyAsync complete get()";
            }
        },threadPoolExecutor);
        System.out.println(threadPoolExecutor.getCompletedTaskCount());
        System.out.println(threadPoolExecutor.getActiveCount());
        future1.thenAccept(e-> System.out.println("xxxxx"+e));
//        try {
//            future1.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }


//        Supplier<StringBuffer> supplier  = new Supplier<StringBuffer>() {
//            @Override
//            public StringBuffer get() {
//                return new StringBuffer("null??");
//            }
//        };
//        System.out.println(supplier.get());
        return res;
    }

    /**
     * 用默认的线程池,看看是否还会造成completablefuture执行完了之后,程序不退出
     * @return
     */
    private  String completableFutureSupplyAsynDefaultPool(){
        String res = "return message";
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                costTime();
                return "CompletableFuture supplyAsync complete get()";
            }
        });
        System.out.println(threadPoolExecutor.getCompletedTaskCount());
        System.out.println(threadPoolExecutor.getActiveCount());
        future1.thenAccept(e-> System.out.println("xxxxx"+e));
//        try {
//            future1.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }


//        Supplier<StringBuffer> supplier  = new Supplier<StringBuffer>() {
//            @Override
//            public StringBuffer get() {
//                return new StringBuffer("null??");
//            }
//        };
//        System.out.println(supplier.get());
        return res;
    }

    private void costTime()  {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("aiyou");
    }
    /**
     * 比较join sleep方法,在主线程和子线程
     */
    public static void joinSleep(){
        Thread mainThread = Thread.currentThread();
        Thread threadA = new Thread(() -> {
            try {
                /**
                 * 一定是当前线程调用此方法,当前线程进入TIMED_WAITING状态,
                 * 但不释放对象锁,millis后线程自动苏醒进入就绪状态。
                 * 作用:给其它线程执行机会的最佳方式
                 */
                System.out.println("wtf"+Thread.currentThread().getName());
                System.out.println("wtf"+mainThread.getName());
                Thread.sleep(10000);
                System.out.println("mainThread.getState()"+mainThread.getState());
//                System.out.println("threadA.getState()"+.getState());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread threadB = new Thread(() -> {
            try {
                /**
                 * thread.join()/thread.join(long millis),当前线程里调用其它线程t的join方法,
                 * t进入WAITING/TIMED_WAITING状态,当前线程不会释放已经持有的对象锁。
                 * 线程t执行完毕或者millis时间到,当前线程一般情况下进入RUNNABLE状态,
                 * 也有可能进入BLOCKED状态(因为join是基于wait实现的)
                 */
                System.out.println("ok "+Thread.currentThread().getName());
                threadA.join(1000);
                System.out.println("B mainThread.getState()"+mainThread.getState());
//                System.out.println("threadA.getState()"+.getState());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadA.start();
        System.out.println("threadA"+threadA.getState());
        threadB.start();
        System.out.println("threadA"+threadA.getState());
        System.out.println("threadB"+threadB.getState());
//        threadA.join(1000);
//        Thread.sleep(1000);
        System.out.println("threadA 1 "+threadA.getState());
        System.out.println("threadB 1 "+threadB.getState());
        System.out.println("threadM 1 "+mainThread.getState());
//        Thread.sleep(10000);
//        System.out.println("threadA 2"+threadA.getState());
//        System.out.println("threadB 2"+threadB.getState());
//        System.out.println("threadM 2"+mainThread.getState());
    }
}

通过上面的代码的completableFutureSupplyAsyn()方法,通过自定义的线程池,可以看到程序异步执行了我们在CompletableFuture里面的代码,并且执行完后,main方法并没有退出。当我们不用自定义线程池的时候,程序正常退出,但是没有执行完我们CompletableFuture里面的代码。看下面的supplyAsync源码,分析有可能是ForkJoinPool导致的。所以下一节,我们来深入研究一下forkJoinPool。

 /**
     * Common pool parallelism. To allow simpler use and management
     * when common pool threads are disabled, we allow the underlying
     * common.parallelism field to be zero, but in that case still report
     * parallelism as 1 to reflect resulting caller-runs mechanics.
     */
    static final int commonParallelism;

  private static final boolean useCommonPool =
        (ForkJoinPool.getCommonPoolParallelism() > 1);
 
/**
     * Default executor -- ForkJoinPool.commonPool() unless it cannot
     * support parallelism.
     */
    private static final Executor asyncPool = useCommonPool ?
        ForkJoinPool.commonPool() : new ThreadPerTaskExecutor();


   /**
     * Returns a new CompletableFuture that is asynchronously completed
     * by a task running in the {@link ForkJoinPool#commonPool()} with
     * the value obtained by calling the given Supplier.
     *
     * @param supplier a function returning the value to be used
     * to complete the returned CompletableFuture
     * @param <U> the function's return type
     * @return the new CompletableFuture
     */
    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
        return asyncSupplyStage(asyncPool, supplier);
    }

static <U> CompletableFuture<U> asyncSupplyStage(Executor e,
                                                     Supplier<U> f) {
        if (f == null) throw new NullPointerException();
        CompletableFuture<U> d = new CompletableFuture<U>();
        e.execute(new AsyncSupply<U>(d, f));
        return d;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值