美文网首页
JUC系列 - ThreadPoolExecutor 源码分析

JUC系列 - ThreadPoolExecutor 源码分析

作者: FX_SKY | 来源:发表于2017-03-20 18:31 被阅读68次

    ThreadPoolExecutor是大家在日常开发中经常用到的类,java.util.concurrent包提供了 java.util.concurrent.Executors工具类方便大家创建线程池。

    Executors

    1、创建固定大小线程池

    ExecutorService es = Executors.newFixedThreadPool(20);
    

    Executors.newFixedThreadPool() 源码如下:

    public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }
    

    2、创建单线程线程池

    ExecutorService es = Executors.newSingleThreadExecutor();
    

    Executors.newSingleThreadExecutor()源码如下:

    public static ExecutorService newSingleThreadExecutor() {
            return new FinalizableDelegatedExecutorService
                (new ThreadPoolExecutor(1, 1,
                                        0L, TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<Runnable>()));
        }
    

    3、创建缓存的线程池

    ExecutorService es = Executors.newCachedThreadPool();
    

    Executors.newCachedThreadPool() 源码如下:

    public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());
        }
    

    ** 不建议大家使用 java.util.concurrent.Executors,推荐直接使用 new ThreadPoolExecutor() 来创建,这样更清楚线程池的参数配置。**

    ThreadPoolExecutor

    首先,看看 java.util.concurrent.ThreadPoolExecutor 的构造方法,如下:

    package java.util.concurrent;
    
    public class ThreadPoolExecutor extends AbstractExecutorService {
        
        /**构造方法**/
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 Executors.defaultThreadFactory(), defaultHandler);
        }
    
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 threadFactory, defaultHandler);
        }
    
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  RejectedExecutionHandler handler) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 Executors.defaultThreadFactory(), handler);
        }
    
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> 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;
        }
    }
    

    参考资料

    https://juejin.im/post/58e7544c0ce463005851eb92

    相关文章

      网友评论

          本文标题:JUC系列 - ThreadPoolExecutor 源码分析

          本文链接:https://www.haomeiwen.com/subject/unsinttx.html