最近在学习线程池, 看到不同的饱和策略, 分析如下:
ThreadPoolExecutor的构造方法如下:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
其中:
corePoolSize: 核心池的大小。 当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到后,就会把到达的任务放到缓存队列当中
maximumPoolSize: 线程池最大线程数,它表示在线程池中最多能创建多少个线程;
keepAliveTime: 表示线程没有任务执行时最多保持多久时间会终止。
unit: 参数keepAliveTime的时间单位,有7种取值,在TimeUnit类中有7种静态属性:
workQueue: 一个阻塞队列,用来存储等待执行的任务。
threadFactory: 线程工厂,主要用来创建线程;
handler: 表示当拒绝处理任务时的策略,有以下四种取值:
AbortPolicy: 丢弃任务并抛出RejectedExecutionException异常。
DiscardPolicy:也是丢弃任务,但是不抛出异常。
DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
CallerRunsPolicy:只要线程池不关闭,该策略直接在调用者线程中,运行当前被丢弃的任务
个人认为这4中策略不友好,最好自己定义拒绝策略,实现RejectedExecutionHandler接口
我们把线程池最大数量和核心池数量都设置为 3 ,队列大小设置为2,放六个任务进去,这样就会有一个任务多出来, 我们看不同策略下,如何处理这个任务
/**
* anji-allways.com Inc.
* Copyright (c) 2016-2017 All Rights Reserved.
*/
package boot.demo.facade;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* <pre>
*
* </pre>
*
* @author sxl
* @version $Id: Demo.java, v 0.1 2019年8月12日 上午10:44:20 sxl Exp $
*/
public class Demo {
public static void main(String[] args) {
ThreadPoolExecutor t = new ThreadPoolExecutor(
3, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2));
for (int i = 0; i < 6; i++) {
t.execute(new MyRunnable(Integer.valueOf(i).toString()));
}
}
}
class MyRunnable implements Runnable {
String i = "";
MyRunnable (String i) {
this.i = i;
}
@Override
public void run() {
int z = 1;
while (z < 3) {
System.out.println(Thread.currentThread().getName() + "#### " + i);
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
z++;
}
}
}
1、默认是AbortPolicy (直接抛出异常)
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
结果 直接抛出异常 新任务 5 没有执行
pool-1-thread-3#### 2
pool-1-thread-1#### 0
pool-1-thread-2#### 1
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task boot.demo.facade.MyRunnable@33909752 rejected from java.util.concurrent.ThreadPoolExecutor@55f96302[Running, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.reject(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
at boot.demo.facade.Demo.main(Demo.java:26)
pool-1-thread-2#### 1
pool-1-thread-3#### 2
pool-1-thread-1#### 0
pool-1-thread-3#### 3
pool-1-thread-1#### 4
pool-1-thread-1#### 4
pool-1-thread-3#### 3
2、DiscardPolicy(丢弃,不执行)
ThreadPoolExecutor t = new ThreadPoolExecutor(
3, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)
,Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());
结果如下, 任务5 还是没有执行, 但是没有抛出异常, 任务被直接丢弃了
pool-1-thread-2#### 1
pool-1-thread-1#### 0
pool-1-thread-3#### 2
pool-1-thread-1#### 0
pool-1-thread-3#### 2
pool-1-thread-2#### 1
pool-1-thread-1#### 3
pool-1-thread-2#### 4
pool-1-thread-1#### 3
pool-1-thread-2#### 4
3,DiscardOldestPolicy(丢弃最先进入队列的一个任务, 执行当前任务)
ThreadPoolExecutor t = new ThreadPoolExecutor(
3, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)
,Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
结果如下 任务 5 被执行了, 但是最先进入队列的任务3被抛弃了
pool-1-thread-1#### 0
pool-1-thread-3#### 2
pool-1-thread-2#### 1
pool-1-thread-2#### 1
pool-1-thread-3#### 2
pool-1-thread-1#### 0
pool-1-thread-2#### 4
pool-1-thread-3#### 5
pool-1-thread-2#### 4
pool-1-thread-3#### 5
4、CallerRunsPolicy(只用调用者所在线程来执行任务)
ThreadPoolExecutor t = new ThreadPoolExecutor(
3, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)
,Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
结果如下, 当前调用者所在线程是主线程 所以main线程执行了任务 5
main#### 5
pool-1-thread-1#### 0
pool-1-thread-2#### 1
pool-1-thread-3#### 2
pool-1-thread-1#### 0
pool-1-thread-3#### 2
main#### 5
pool-1-thread-2#### 1
pool-1-thread-3#### 3
pool-1-thread-1#### 4
pool-1-thread-3#### 3
pool-1-thread-1#### 4
最后,我们也可以实现RejectedExecutionHandler接口,根据业务自定义策略,如记录日志或者持久化不能处理的任务等等....