一)ThreadPoolExecutor拒绝策略
1)ThreadPoolExecutor.AbortPolicy:当任务添加到线程池中被拒绝时,它将抛出 RejectedExecutionException异常。
2)ThreadPoolExecutor.CallerRunsPolicy:当任务添加到线程池中被拒绝时,会在线程池当前正在运行的Thread线程池中处理被拒绝的任务。
3)ThreadPoolExecutor.DiscardOldestPolicy:当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,然后将被拒绝的任务添加到等待队列中。
4)ThreadPoolExecutor.DiscardPolicy:当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。
二)多线程测试类
创建一个MyRunnable类,继承Runnable接口,并实现run方法。添加Thread.sleep()方法是为了方便测试。
public class MyRunnable implements Runnable {
private String threadName;
public MyRunnable(String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
try {
System.out.println("当前线程名: " + Thread.currentThread().getName() + ", 任务" + threadName + " is running!");
Thread.sleep(100); // 休眠100毫秒, 方便测试
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
三)AbortPolicy
第一种:当任务添加到线程池中被拒绝时,它将抛出 RejectedExecutionException异常。
该方式是ThreadPoolExecutor中默认的拒绝策略。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AbortPolicyDemo {
public static void main(String[] args) {
// 线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.AbortPolicy());
// 修改ThreadPoolExecutor线程池拒绝策略
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
MyRunnable run = null;
for (int i = 0; i < 5; i++) { // 循环创建线程
run = new MyRunnable("MyRunnable " + i);
pool.execute(run); // 将任务添加到线程池中
}
// 关闭线程池,如不关闭,线程池会一直运行
pool.shutdown();
}
}
运行效果图:
说明:创建一个单线程池,当运行任务超过核心数量时,线程池会抛RejectedExecutionException异常。
四)CallerRunsPolicy
第二种:当任务添加到线程池中被拒绝时,会在线程池当前正在运行的Thread线程池中处理被拒绝的任务。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CallerRunsPolicyDemo {
public static void main(String[] args) {
// 线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.CallerRunsPolicy());
// 修改ThreadPoolExecutor线程池拒绝策略
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
MyRunnable run = null;
for (int i = 0; i < 5; i++) { // 循环创建线程
run = new MyRunnable("MyRunnable " + i);
pool.execute(run); // 将任务添加到线程池中
}
// 关闭线程池,如不关闭,线程池会一直运行
pool.shutdown();
}
}
运行效果图:
说明:当运行的线程任务超过核心数量时,会进行穿插运行。
五)DiscardOldestPolicy
第三种:当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,然后将被拒绝的任务添加到等待队列中。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class DiscardOldestPolicyDemo {
public static void main(String[] args) {
// 线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
// 修改ThreadPoolExecutor线程池拒绝策略
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
MyRunnable run = null;
for (int i = 0; i < 5; i++) { // 循环创建线程
run = new MyRunnable("MyRunnable " + i);
pool.execute(run); // 将任务添加到线程池中
}
// 关闭线程池,如不关闭,线程池会一直运行
pool.shutdown();
}
}
运行效果图:
说明:当创建线程任务时,会先运行第0个线程,然后第1、2、3、4个线程处于阻塞状态,当第0个线程运行完之后,线程池会抛弃最先进行等待的线程,而运行最近等待的线程。
六)DiscardPolicy
第四种:当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class DiscardPolicyDemo {
public static void main(String[] args) {
// 线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.DiscardPolicy());
// 修改ThreadPoolExecutor线程池拒绝策略
//pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
MyRunnable run = null;
for (int i = 0; i < 5; i++) { // 循环创建线程
run = new MyRunnable("MyRunnable " + i);
pool.execute(run); // 将任务添加到线程池中
}
// 关闭线程池,如不关闭,线程池会一直运行
pool.shutdown();
}
}
运行效果图:
说明:先运行前面的线程,当线程池任务数量超过核心数量时,把阻塞的线程任务抛弃掉。
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!