DiscardOldestPolicy策略是当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,将被拒绝的任务添加到等待队列中。
线程执行代码如下:
- public class TheRunnable implements Runnable {
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- private String username;
-
- public TheRunnable(String username){
- super();
- this.username=username;
- }
-
-
-
- @Override
- public void run() {
- try {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
- System.out.println("线程名:"+username+" 开始时间:"+simpleDateFormat.format(new Date()));
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
运行类代码如下:
- @Slf4j
- public class DiscardOldestPolicyRun {
- public static void main(String[] args) {
- ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
- ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy());
- for (int i = 0; i < 5 ; i++) {
- TheRunnable runnable = new TheRunnable("Runnable " + i );
- threadPoolExecutor.execute(runnable);
- }
- try {
- Thread.sleep(500);
- Iterator iterator = arrayBlockingQueue.iterator();
- while (iterator.hasNext()){
- Object object = iterator.next();
- log.info("线程名:{}",((TheRunnable)object).getUsername());
- }
- threadPoolExecutor.execute(new TheRunnable("Runnable 6"));
- threadPoolExecutor.execute(new TheRunnable("Runnable 7"));
- iterator = arrayBlockingQueue.iterator();
- while(iterator.hasNext()){
- Object object = iterator.next();
- log.info("线程名:{}",((TheRunnable)object).getUsername());
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
运行结果如下:
线程名:Runnable 4 开始时间:16:47:54
线程名:Runnable 1 开始时间:16:47:54
线程名:Runnable 0 开始时间:16:47:54
16:47:55.509 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 2
16:47:55.516 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 3
16:47:55.517 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 6
16:47:55.517 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 7
线程名:Runnable 7 开始时间:16:47:56
线程名:Runnable 6 开始时间:16:47:56
从运行结果看出早期放入队列中两个任务被取消了,执行了后来添加的两个任务。
DiscardPolicy策略是当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。
运行类代码如下:
- @Slf4j
- public class DiscardPolicyRun {
- public static void main(String[] args) throws InterruptedException {
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- try {
- Thread.sleep(5000);
- log.info(Thread.currentThread().getName()+"执行结束");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
-
- ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
- ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardPolicy());
- for (int i = 0; i < 8 ; i++) {
- threadPoolExecutor.execute(runnable);
- }
- Thread.sleep(8000);
- log.info("线程池中最大线程数:{},队列数:{}",threadPoolExecutor.getPoolSize(),arrayBlockingQueue.size());
- }
- }
运行结果如下:
17:40:14.301 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-1执行结束
17:40:14.301 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-2执行结束
17:40:14.301 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-3执行结束
17:40:17.278 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - 线程池中最大线程数:3,队列数:0
17:40:19.326 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-1执行结束
17:40:19.326 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-2执行结束
从运行结果看出多余的任务直接被取消执行。