ThreadPoolExecutor的使用

本文详细介绍了线程池的工作原理及参数配置,包括线程池的构造方法、核心线程数、最大线程数等关键概念。同时探讨了线程饥饿死锁现象。
在更大的线程池中,如果所有正在执行任务的线程都由于等待其他仍处于工作队列中的任务而阻塞,这种现象叫线程饥饿死锁(Thread Starvation DeadLock)


线程池的使用


  /**
     * Creates a new <tt>ThreadPoolExecutor</tt> with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the
     * pool, even if they are idle.
     * @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 keepAliveTime
     * argument.
     * @param workQueue the queue to use for holding tasks before they
     * are executed. This queue will hold only the <tt>Runnable</tt>
     * tasks submitted by the <tt>execute</tt> 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 corePoolSize or
     * keepAliveTime less than zero, or if maximumPoolSize less than or
     * equal to zero, or if corePoolSize greater than maximumPoolSize.
     * @throws NullPointerException if <tt>workQueue</tt>
     * or <tt>threadFactory</tt> or <tt>handler</tt> are 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 为核心池大小
 maximumpoolSize最大线程池大小
 keepAliveTime 当前线程大小 大于核心池大小时,线程空闲的时间超过keepAliveTime,则终止该线程
 unit      为keepAliveTime 的单位
 workQueue 为工作队列
threadFactory  创建线程的方式
 handler 饱和策略


ThreadPoolExecutor 运作机制详解:
a . 当线程池大小小于 corepoolSize ,并处理请求
b .当线程池大小等于corepoolSize ,把请求放入workQueue中,池子里的空闲线程就去从workQueue中取任务并处理
c .当workQueue放不下新入的任务时,新建线程入池,并处理请求,如果池子大小撑到maximumpoolSize就用RejectedExecutionHandler来做拒绝处理
d .当线程池的线程数大于corePoolSize的时候,多余的线程会等待KeepAliveTime长的时间,如果无请求就自行销毁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值