线程池里如何养鱼

什么是线程池?
线程池(容器)里面有提前创建好若干的线程,等分配任务去执行;执行完任务之后 不会被销毁又会准备去执行其他任务

  • 可以减少线程的创建和销毁的性能开销
  • 提高响应速度,有任务来了无需等待创建新线程就能立马执行
  • 提高线程的可管理性(监控),合理设置线程池大小(避免线程创建太多或太少)可以避免因创建线程过多带来的一系列问题

线程池的工作原理

先来看看线程池的构造方法的每个参数的含义:

	/**
     * 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
     * corePoolSize保留在池中的线程数,即使它们是空闲的,除非设置了{@code allowCoreThreadTimeOut}
     * @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.
     * keepAliveTime当线程数大于核心时,这是多余空闲线程在终止前等待新任务的最长时间
     * @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.
     * 工作队列在任务执行前用于保留任务的队列。此队列将只保存{@code execute}方法提交的{@code Runnable}任务
     * @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.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

下面看一看线程池的执行流程
在这里插入图片描述

newFixedThreadPool

newSingleThreadExecutor

newCachedThreadPool

newScheduledThreadPool

猜你喜欢

转载自blog.csdn.net/blackxc/article/details/108062589