ThreadPoolExecutor线程池的拒绝策略

ThreadPoolExecutor线程池的拒绝策略

Creates a new ThreadPoolExecutor with the given initial parameters.创建一个新的线程池,给予初始化的参数。
Params:
corePoolSize – the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
//线程池中保留的线程数,即使他们闲着,除非超过允许存在的时间;
maximumPoolSize – the maximum number of threads to allow in the pool
//线程池中最多允许出现线程的数量
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.
unit – the time unit for the keepAliveTime argument
//池中线程数超过了核心线程数,这是多余的空闲线程在终止之前等待新任务的最长时间;
workQueue – the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
//队列被用来存放任务在执行前,这个队列仅保存通过excute方法添加的线程任务;
threadFactory – the factory to use when the executor creates a new thread
//执行程序创建新线程要使用的工厂;
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: corePoolSize < 0 keepAliveTime < 0 maximumPoolSize <= 0 maximumPoolSize < corePoolSize
NullPointerException – if workQueue or threadFactory or 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;
    }
RejectedExecutionHandler拒绝的接口,通过实现接口 重写rejectedExecution方法,可以定义拒绝的执行方式,现有四种拒绝策略:
CallerRunsPolicy:A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method, unless the executor has been shut down, in which case the task is discarded. 直接执行通过excute方法添加的任务,除非线程池关闭了,关闭了抛弃当前任务
AbortPolicy:A handler for rejected tasks that throws a RejectedExecutionException.直接抛出异常
DiscardPolicy:A handler for rejected tasks that silently discards the rejected task.直接拒绝丢弃任务
DiscardOldestPolicy:A handler for rejected tasks that discards the oldest unhandled request and then retries execute, unless the executor is shut down, in which case the task is discarded.丢弃最老的未执行的任务,然后重试excute,除非线程池关闭了,关闭了抛弃当前任务

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值