前言
线程池的拒绝策略,在面试中经常会被问到,本篇文章主要是总结一下对新添加的Task任务有不同的处理策略,有默认的,有我们主动自己添加的,首先说一下有哪几种不同的策略:
AbortPolicy
官方介绍当任务添加到线程池中被拒绝时,它将抛出RejectedExecutionException异常。
CallerRunsPolicy
当我们task添加到线程池中被拒绝时,会使用调用线程池的Thread线程对象处理被拒绝的任务
DiscardOldestPolicy
当Task任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,然后将被拒绝的Task添加到等待队列中
DiscardPolicy
当Task添加到线程池中被拒绝时,线程池将丢弃被拒绝的Task
Demo
AbortPolicy-Demo
package com.zcw.demo;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @ClassName : AbortPolicyDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-22 12:55
*/
@Slf4j
public class AbortPolicyDemo {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
log.info(Thread.currentThread().getName()+"end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,5,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(2),
new ThreadPoolExecutor.AbortPolicy());
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
}
}
运行结果:
通过运行Demo我们发现报错,这是因为在使用AbortPolicy策略后,当我们在实际项目开发中线程的数量超出了max值时,线程池就会抛出异常
CallerRunsPolicy
package com.zcw.demo;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @ClassName : MyThreadA
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-22 13:05
*/
@Slf4j
public class MyThreadA extends Thread {
@Override
public void run(){
MyThreadB myThreadB = new MyThreadB();
LinkedBlockingDeque linkedBlockingDeque = new LinkedBlockingDeque(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,5,
TimeUnit.SECONDS,linkedBlockingDeque,
new ThreadPoolExecutor.CallerRunsPolicy());
log.info("开始"+Thread.currentThread().getName()+""+System.currentTimeMillis());
threadPoolExecutor.execute(myThreadB);
threadPoolExecutor.execute(myThreadB);
threadPoolExecutor.execute(myThreadB);
threadPoolExecutor.execute(myThreadB);
threadPoolExecutor.execute(myThreadB);
threadPoolExecutor.execute(myThreadB);
log.info("a 结束"+Thread.currentThread().getName()+" "+System.currentTimeMillis());
log.info("a 结束"+System.currentTimeMillis());
}
public static void main(String[] args) {
MyThreadA myThreadA = new MyThreadA();
myThreadA.setName("HH");
myThreadA.start();
log.info("程序运行结束。。。。");
}
}
package com.zcw.demo;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.SynchronousQueue;
/**
* @ClassName : MyThreadB
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-22 13:06
*/
@Slf4j
public class MyThreadB extends Thread {
@Override
public void run(){
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("end" + Thread.currentThread().getName()+""+ System.currentTimeMillis());
}
}
运行结果:
DiscardOldestPolicy
package com.zcw.demo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @ClassName : DiscardOldestPolicyDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-22 13:14
*/
@Data
@Slf4j
public class DiscardOldestPolicyDemo implements Runnable{
private String name;
public DiscardOldestPolicyDemo(String name){
super();
this.name = name;
}
@Override
public void run(){
log.info(name+"跑起来....");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,5,
TimeUnit.SECONDS,arrayBlockingQueue,
new ThreadPoolExecutor.DiscardOldestPolicy());
for(int i=0;i<5;i++){
DiscardOldestPolicyDemo discardOldestPolicyDemo = new DiscardOldestPolicyDemo("Runnable"+(i+1));
threadPoolExecutor.execute(discardOldestPolicyDemo);
}
Thread.sleep(50);
Iterator iterator = arrayBlockingQueue.iterator();
while(iterator.hasNext()){
Object object = iterator.next();
log.info(((DiscardOldestPolicyDemo)object).getName());
}
threadPoolExecutor.execute(new DiscardOldestPolicyDemo("Runnable6"));
threadPoolExecutor.execute(new DiscardOldestPolicyDemo("Runnable7"));
iterator =arrayBlockingQueue.iterator();
while(iterator.hasNext()){
Object object = iterator.next();
log.info(((DiscardOldestPolicyDemo)object).getName());
}
}
}
运行结果:
DiscardPolicy
package com.zcw.demo;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @ClassName : DiscardPolicyDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-22 13:39
*/
@Slf4j
public class DiscardPolicyDemo {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName()+"end");
}
};
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,3,5,
TimeUnit.SECONDS,arrayBlockingQueue,
new ThreadPoolExecutor.DiscardOldestPolicy());
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(runnable);
Thread.sleep(8000);
log.info(threadPoolExecutor.getPoolSize()+" "+arrayBlockingQueue.size());
}
}