科技报道|一次性搞清楚,Java并发编程在各主流框架中的应用,保证看懂( 五 )

J.U.C 包的实际应用线程池 ThreadPoolExecutor
首先通过 ThreadPoolExecutor 的源码 看一下线程池的主要参数及方法 。
public class ThreadPoolExecutor extends AbstractExecutorService {/*** 核心线程数* 当向线程池提交一个任务时 , 若线程池已创建的线程数小于corePoolSize , 即便此时存在空闲线程 ,* 也会通过创建一个新线程来执行该任务 , 直到已创建的线程数大于或等于corePoolSize*/private volatile int corePoolSize;/*** 最大线程数* 当队列满了 , 且已创建的线程数小于maximumPoolSize , 则线程池会创建新的线程来执行任务 。* 另外 , 对于无界队列 , 可忽略该参数*/private volatile int maximumPoolSize;/*** 线程存活保持时间* 当线程池中线程数 超出核心线程数 , 且线程的空闲时间也超过 keepAliveTime时 ,* 那么这个线程就会被销毁 , 直到线程池中的线程数小于等于核心线程数*/private volatile long keepAliveTime;/*** 任务队列* 用于传输和保存等待执行任务的阻塞队列*/private final BlockingQueue workQueue;/*** 线程工厂* 用于创建新线程 。threadFactory 创建的线程也是采用 new Thread() 方式 , threadFactory* 创建的线程名都具有统一的风格:pool-m-thread-n(m为线程池的编号 , n为线程池中线程的编号*/private volatile ThreadFactory threadFactory;/*** 线程饱和策略* 当线程池和队列都满了 , 再加入的线程会执行此策略*/private volatile RejectedExecutionHandler handler;/*** 构造方法提供了多种重载 , 但实际上都使用了最后一个重载 完成了实例化*/public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), defaultHandler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,threadFactory, defaultHandler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,RejectedExecutionHandler handler) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), handler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {if (corePoolSize < 0 ||maximumPoolSize <= 0 ||maximumPoolSize < corePoolSize ||keepAliveTime < 0)throw new IllegalArgumentException();if (workQueue == null || threadFactory == null || handler == null)throw new NullPointerException();this.corePoolSize = corePoolSize;this.maximumPoolSize = maximumPoolSize;this.workQueue = workQueue;this.keepAliveTime = unit.toNanos(keepAliveTime);this.threadFactory = threadFactory;this.handler = handler;}/*** 执行一个任务 , 但没有返回值*/public void execute(Runnable command) {if (command == null)throw new NullPointerException();int c = ctl.get();if (workerCountOf(c) < corePoolSize) {if (addWorker(command, true))return;c = ctl.get();}if (isRunning(c)if (! isRunning(recheck)else if (workerCountOf(recheck) == 0)addWorker(null, false);}else if (!addWorker(command, false))reject(command);}/*** 提交一个线程任务 , 有返回值 。 该方法继承自其父类 AbstractExecutorService , 有多种重载 , 这是最常用的一个 。* 通过future.get()获取返回值(阻塞直到任务执行完)*/publicFuture submit(Callable task) {if (task == null) throw new NullPointerException();RunnableFuture ftask = newTaskFor(task);execute(ftask);return ftask;}/*** 关闭线程池 , 不再接收新的任务 , 但会把已有的任务执行完*/public void shutdown() {final ReentrantLock mainLock = this.mainLock;mainLock.lock();try {checkShutdownAccess();advanceRunState(SHUTDOWN);interruptIdleWorkers();onShutdown(); // hook for ScheduledThreadPoolExecutor} finally {mainLock.unlock();}tryTerminate();}/*** 立即关闭线程池 , 已有的任务也会被抛弃*/public List


推荐阅读