// 收集结果 List<?> res = futureList.stream() .map(CompletableFuture::join) .flatMap(List::stream) .toList();
相关知识点
ThreadPoolExecutor 线程池
Future & CompletableFuture
ThreadLocalMap
我们都知道想要实现一个线程一份独立的数据的时候可以使用 ThreadLocal 来管理数据
ThreadLocal的底层实际上是基于他的静态内部类ThreadLocalMap
JDK文档:
ThreadLocalMap 是一个专门用于维护 ThreadLocal 值而定制化设计的 HashMap,所有对 ThreadLocalMap 的操作只会发生在 TheadLocal 类内部。为应对 very large and long-lived usages,HashMap 的 entry 使用弱引用作为key (?)
ThreadLocalMap 的 key 是 TheadLocal, value 是 ThreadLocal 存储的数据值
0711
线程池核心参数
一般我们都会使用线程池对象的构造函数来结合不同的场景定义线程池
1 2 3 4 5 6 7
publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {}
/** * A handler for rejected tasks that runs the rejected task * directly in the calling thread of the {@code execute} method, * unless the executor has been shut down, in which case the task * is discarded. */ publicstaticclassCallerRunsPolicyimplementsRejectedExecutionHandler { /** * Creates a {@code CallerRunsPolicy}. */ publicCallerRunsPolicy() { }
/** * Executes task r in the caller's thread, unless the executor * has been shut down, in which case the task is discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
/** * A handler for rejected tasks that silently discards the * rejected task. */ publicstaticclassDiscardPolicyimplementsRejectedExecutionHandler { /** * Creates a {@code DiscardPolicy}. */ publicDiscardPolicy() { }
/** * Does nothing, which has the effect of discarding task r. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e) { //do nothing } }
/** * A handler for rejected tasks that discards the oldest unhandled * request and then retries {@code execute}, unless the executor * is shut down, in which case the task is discarded. */ publicstaticclassDiscardOldestPolicyimplementsRejectedExecutionHandler { /** * Creates a {@code DiscardOldestPolicy} for the given executor. */ publicDiscardOldestPolicy() { }
/** * Obtains and ignores the next task that the executor * would otherwise execute, if one is immediately available, * and then retries execution of task r, unless the executor * is shut down, in which case task r is instead discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ publicvoidrejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
/** * A {@link Future} that is {@link Runnable}. Successful execution of * the {@code run} method causes completion of the {@code Future} * and allows access to its results. * @see FutureTask * @see Executor * @since 1.6 * @author Doug Lea * @param <V> The result type returned by this Future's {@code get} method */ publicinterfaceRunnableFuture<V> extendsRunnable, Future<V> { /** * Sets this Future to the result of its computation * unless it has been cancelled. */ voidrun(); }