代码先锋网 代码片段及技术文章聚合

Executors.newFixedThreadPool 源码解析

Executors 有个常用静态方法newFixedThreadPool(int nThreads),来构造线程池
今天我们其源码实现,探一探究竟


//new LinkedBlockingQueue<Runnable>()这里可以看出 是声明的无界队列大小,默认大小为Integer.MAX_VALUE
   public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

点进去看其实现
corePoolSize、maximumPoolSize 两个值设置为一样,keepAliveTime为空闲线程存活时间,when the number of threads is greater than the core,这里该值为0

//Executors.defaultThreadFactory() 用来构造线程的 Returns a default thread factory used to create new threads.
//defaultHandler: the default rejected execution handler,
//A handler for rejected tasks that throws a RejectedExecutionException 当 Runnable task处理不过来时会派上用场

//private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();

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,
                              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;
    }

至此调用完成。

总结:

  • newFixedThreadPool 提供一种快速构造线程池的接口,里面设置了很多默认参数,最终还是调用
    ThreadPoolExecutor来构造 ExecutorService,ThreadPoolExecutor本身是
    ExecutorService接口类的实现类。

  • 当默认参数不满足需要是,直接 使用 ThreadPoolExecutor进行构造线程池

版权声明:本文为weixin_30202885原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30202885/article/details/95001736

智能推荐

关于Executors.newFixedThreadPool何时创建新线程

2019独角兽企业重金招聘Python工程师标准>>>     Executors.newFixedThreadPool 本意是创建一个固定大小的线程池,但这个线程池是什么执行的呢? 1、当一个任务提交执行后,就会先判断是否线程池的数量已经达到上限,如果没有则创建新的线程来执行任务,即使原来创建过的线程是空闲的也不会使用。看源码: 即只要线...

线程池的简单运用Executors.newFixedThreadPool

原文链接:https://blog.csdn.net/u011441473/article/details/117450881...

Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得

newFixedThreadPool使用范例:          输出: task: 1 task: 2 thread start0 task: 3 task: 4 task: 5 task: 6 task: 7 thread start1 task: 8 task: 9 task: 10 task: 11 task: 12 task: 13 ta...

猜你喜欢

ExecutorService为创建的线程池ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE)

ExecutorService就是要创建的线程池  JAVA中线程池用类ExecutorService代表 ,案例ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE) 底层都是通过ThreadPoolExecutor实现的 http://www.mamicode.com/info-detail-257791...

Java> 线程池的简单使用-Executors.newFixedThreadPool(int num)

线程池的简单使用 ExecutorService thread_pool_num = Executors.newFixedThreadPool(线程数量) thread_pool_num.submit(new implements_Runnable_Class);...

为什么不能用ThreadPoolExecutor t = Executors.newFixedThreadPool(10)?它错在哪里?

为什么不能用ThreadPoolExecutor t = Executors.newFixedThreadPool(10) 一、创建线程池(以newFixedThreadPool为例) 二、找到创建 executorService 对象的类 三、提出疑问 一、创建线程池(以newFixedThreadPool为例) 首先,在我们使用线程池的方式来创建多线程的时候,会使用工具类 Executors ...

数据集合与分组操作:数据聚合,逐列及多函数应用,返回不含行索引的聚合数据, 数据透视表与交叉表(crosstab)

2.1 数据聚合 聚合是指所有根据数组产生标量值的数据转换过程。就像mean count min sum等, 函数 描述 count 分组中的非NA 值数量 sum 非NA值的累计和 mean 非NA 值的均值 median 非NA 值的算术中位数 std,var 无偏的(n-1 分母)标准差和方差 min max 非NA 值的最大值最小值 prod 非NA 值 的乘积 first,last 非N...