Java 线程池 创建销毁过程入门学习笔记

这篇博客主要介绍了Java线程池的核心属性,包括核心线程数corePoolSize、最大线程数maximumPoolSize、空闲线程存活时间keepAliveTime、时间单位unit、工作队列workQueue和拒绝策略handler。同时,深入源码分析了线程池的执行流程,从execute方法到Worker线程的创建与任务执行,详细解释了线程回收的条件。
摘要由CSDN通过智能技术生成

目录

学习目标:

学习内容:

1、线程池属性

2、源码学习


学习目标:


1、线程池的属性学习

2、参考资料,源码学习


学习内容:

1、线程池属性

线程池的构造函数

    /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    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;
    }

参数简介(参考构造方法注释及相关博客,个人理解,有错请指出):


  • corePoolSize:核心线程数,除非设置 allowCoreThreadTimeOut 属性,否则核心线程数创建后一直存在。核心线程数的创建过程为需要时,一个一个创建,非一次性创建。

  • maximumPoolSize:最大线程数,线程池中,最大可同时存在的线程数。

  • keepAliveTime:大于核心线程数的、空闲的(idle)线程等待回收的时间。

  • unit:keepAliveTime的时间单位。

  • workQueue:存放待执行的任务的队列。

  • threadFactory:线程工厂

  • handler:拒绝策略,当核心线程—>队列—>最大线程数都达到最大值时。资源即达到设置的最大值,如新加入任务时,则根据handler的策略,进行后续处理。

     


    2、源码学习

线程池使用例子:

ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值