java并发初探ThreadPoolExecutor拒绝策略

ThreadPoolExecuter构造器

corePoolSize是核心线程池,就是常驻线程池数量;

maximumPoolSize是最大线程池数量,如果队列满了,就会创建新的线程任务。如果与corePoolSize一样大小,

那么就是固定大小的鹅线程池;

keepAliveTime表示线程池中线程空闲时间,当空闲时间达到keepAliveTime,线程会被销毁,直到剩下corePoolSize,

默认情况只有当前的线程数大于corePoolSize时keepAliveTime才会起作用;

unit时间单位;

BlockingQueue阻塞队列。当请求线程数大于corePoolSize时,线程会进入阻塞队列,当达到阻塞队列的上限是,线程池会创建新的

线程池,最大的线程数就是maximumPoolSize;

其他参数:

threadFactory线程工厂,默认是Executors.defaultThreadFactory();

RejectedExecutionHandler拒绝策略的执行对象,当线程池数量大于maximumPoolSize,拒绝策略就会执行;

    /**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@link Executors} factory
* methods instead of this general purpose constructor.
*
* @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.
* @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} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}

拒绝策略

AbortPolicy

默认拒绝策略,丢弃任务,抛出异常RejectedExecutionException

DiscardPolicy

丢弃任务

DiscardOldestPolicy

把最旧的等待任务抛弃,放入等待队列

CallerRunsPolicy

拒绝任务在当前正在执行的线程运行

    /**
* A handler for rejected tasks that throws a
* {@code RejectedExecutionException}.
*/
public static class AbortPolicy implements RejectedExecutionHandler /**
* A handler for rejected tasks that silently discards the
* rejected task.
*/
public static class DiscardPolicy implements RejectedExecutionHandler /**
* A handler for rejected tasks that discards the oldest unhandled
* request and then retries {@code execute}, unless the executor
* is shut down, in which case the task is discarded.
*/
public static class DiscardOldestPolicy implements RejectedExecutionHandler /**
* A handler for rejected tasks that runs the rejected task
* directly in the calling thread of the {@code execute} method,
* unless the executor has been shut down, in which case the task
* is discarded.
*/
public static class CallerRunsPolicy implements RejectedExecutionHandler

例子

package com.java.javabase.thread.base.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; /**
* @author
*/
@Slf4j
public class ThreadPoolAbortPolicyTest {
public static void main(String[] args) {
ThreadPoolExecutor pool =new ThreadPoolExecutor(1,2,0, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1));
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
pool.execute(() -> log.info("thread {} 0", Thread.currentThread().getName()));
pool.execute(() -> log.info("thread {} 1", Thread.currentThread().getName()));
pool.execute(() -> log.info("thread {} 2", Thread.currentThread().getName()));
pool.execute(() -> log.info("thread {} 3", Thread.currentThread().getName()));
pool.execute(() -> log.info("thread {} 4", Thread.currentThread().getName()));
pool.shutdown();
}
}

DiscardOldestPolicy结果

2019-08-14 15:56:18,972   [pool-1-thread-1] INFO  ThreadPoolAbortPolicyTest  - thread pool-1-thread-1 0
2019-08-14 15:56:18,972 [pool-1-thread-1] INFO ThreadPoolAbortPolicyTest - thread pool-1-thread-1 4
2019-08-14 15:56:18,972 [pool-1-thread-2] INFO ThreadPoolAbortPolicyTest - thread pool-1-thread-2 2

CallerRunsPolicy结果

2019-08-14 15:58:03,439   [pool-1-thread-1] INFO  ThreadPoolAbortPolicyTest  - thread pool-1-thread-1 0
2019-08-14 15:58:03,439 [pool-1-thread-2] INFO ThreadPoolAbortPolicyTest - thread pool-1-thread-2 2
2019-08-14 15:58:03,454 [pool-1-thread-1] INFO ThreadPoolAbortPolicyTest - thread pool-1-thread-1 1
2019-08-14 15:58:03,439 [main] INFO ThreadPoolAbortPolicyTest - thread main 3
2019-08-14 15:58:03,454 [pool-1-thread-1] INFO ThreadPoolAbortPolicyTest - thread pool-1-thread-1 4

最新文章

  1. UDS(ISO14229-2006) 汉译(No.6 应用层服务)
  2. 在windows系统上安装VMware Workstation虚拟机,然后在虚拟机VMware Workstation上安装linux系统,在linux系统安装xshell的服务端,在windows系统上安装xshell。用windows系统上的xshell连接到linux
  3. C# 操作数据库就的那点代码
  4. C#后台执行bat
  5. 计算两点距离 ios
  6. caffe 中的一些参数介绍
  7. java笔记之String的应用
  8. 用jersey + spring 实现rest服务及单元测试
  9. Tomcat7的热部署
  10. 基于PlatinumKit库的DLNA服务端开发
  11. 装x玩法:插上你的专有U盘才能开机
  12. Docker系列05—Docker 存储卷详解
  13. vue组件之前嵌套
  14. jupyter notebook 代码自动补齐插件
  15. JVM虚拟机深入理解+GC回收+类加载
  16. springboot 学习之路 3( 集成mybatis )
  17. 说一下Servlet里面得request和response
  18. ss linux终端配置
  19. 为什么你用不好Numpy的random函数?
  20. ScrollView 定位

热门文章

  1. ASCII码排序 题解
  2. 从QC到QA
  3. 【笔记7-部署发布】从0开始 独立完成企业级Java电商网站开发(服务端)
  4. Spring Boot 定时任务 Quartz 使用教程
  5. 转发-【分享】思科无线控制器HA模式升级
  6. 控制面保护(CPPr)
  7. IDEA 查看字节码
  8. 【转】shell处理mysql增删改查
  9. lc 0228
  10. PS进程及杀掉进程!