【Java】线程池(一) 参数

* @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

几个关键参数:

1.corePoolSize:核心线程数,将会永久存在线程池中;

2.maximumPoolSize:最大线程数,如果任务队列满了,新来的任务将会额外创建临时线程来处理,但是总的线程数目不会超过这个值;

3.keepAliveTime:指的是额外创建的临时线程过期阈值;

 

线程池的参数作用:如果线程数小于corePoolSize,那么每一个任务进来都会创建线程;如果大于等于corePoolSize,那么新来的任务将会进入队列等待处理;如果队列超过阈值,那么就会新建临时线程,直到线程数到达maximumPoolSize。如果到达maximumPoolSize,那么会根据拒绝策略拒绝后来的任务。

 

线程池原本的模型就是池化一定数目的线程,然后处理用户请求,如果用户请求多了,那么就扔进队列里,等待池化的线程处理完手头工作再处理 新的请求。那么如果队列也满了,说明系统不堪重负,此时需要临时添加一下线程救急,一旦过了高峰期,这些临时线程就会被回收,这就是线程池的概念模型。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值