1.介绍
当线程池线程数大于最大线程数(maximumPoolSize)时,多余的任务,程序应该按照什么拒绝策略处理。
2.拒绝策略4个
AbortPolicy:丢弃任务,并抛出RejectedExecutionException异常(需要在调用线程处捕获异常,即执行submit线程处)
DiscardPolicy:丢弃任务,不抛出异常
DiscardOldestPolicy:丢弃等待队列最前面任务
CallerRunsPolicy:由调用线程处理该任务
3.示例
3.1.AbortPolicy捕获异常
public class RejectedAbortPolicy { static class MyRunnable implements Runnable { private String jobName; MyRunnable(String jobName) { this.jobName = jobName; } @Override public void run() { while (true) { try { Thread.currentThread(); Thread.sleep(1000); System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException { ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), Executors.defaultThreadFactory(), new AbortPolicy()); es.submit(new MyRunnable("job-1")); es.submit(new MyRunnable("job-2")); es.submit(new MyRunnable("job-3")); Thread.sleep(5000); try { Future<?> f = es.submit(new MyRunnable("job-4")); f.get();// 这一句永远执行不到 } catch (Exception e) { System.err.println("job-4 submit Thread pool mistake"); } } }
3.2.DiscardOldestPolicy测试丢弃等待队列最前面任务
public class RejectedDiscardOldestPolicy { static class MyRunnable implements Runnable { private String jobName; MyRunnable(String jobName) { this.jobName = jobName; } public String getJobName(){ return jobName; } @Override public void run() { while (true) { try { Thread.currentThread(); Thread.sleep(1000); System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException { BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1); ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, workQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy()); es.execute(new MyRunnable("job-1")); es.execute(new MyRunnable("job-2")); es.execute(new MyRunnable("job-3")); Runnable firstRunnable = workQueue.poll(); System.err.println("submit job-3 检查队列当前 Runnable名称:" +((MyRunnable) firstRunnable).getJobName()); workQueue.add(firstRunnable); Thread.sleep(5000); es.execute(new MyRunnable("job-4")); Runnable SecondRunnable = workQueue.poll(); System.err.println("submit job-4 检查队列当前 Runnable名称应该为job-4,实际为:" +((MyRunnable) SecondRunnable).getJobName()); workQueue.add(SecondRunnable); }
3.3.CallerRunsPolicy主线程处理这个任务
public class RejectedCallerRunsPolicy { static class MyRunnable implements Runnable { private String jobName; MyRunnable(String jobName) { this.jobName = jobName; } public String getJobName(){ return jobName; } @Override public void run() { while (true) { try { Thread.currentThread(); Thread.sleep(1000); System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException { BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1); ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, workQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); es.execute(new MyRunnable("job-1")); es.execute(new MyRunnable("job-2")); es.execute(new MyRunnable("job-3")); Thread.sleep(5000); es.execute(new MyRunnable("job-4")); } }