ThreadPoolExecutor的阻塞队列满了,再增加新的任务后,执行顺序

 如题,阻塞队列满了,但是线程池的线程数量小于maxPoolExecutor,此时增加新的任务,会立即执行新的任务,而不会从队列中取任务

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadSequence {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                6,
                10,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(3));
        for (int i=0;i<10;i++){
            int finalI = i;
            threadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName() + "is executing" + finalI);
                try {
                    Thread.sleep(10000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}

pool-1-thread-1is executing0
pool-1-thread-2is executing1
pool-1-thread-3is executing2
pool-1-thread-4is executing6
pool-1-thread-5is executing7
pool-1-thread-6is executing8
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.ncu.testThread.ThreadSequence$$Lambda$1/1452126962@6acbcfc0 rejected from java.util.concurrent.ThreadPoolExecutor@5f184fc6[Running, pool size = 6, active threads = 6, queued tasks = 3, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
    at com.ncu.testThread.ThreadSequence.main(ThreadSequence.java:12)


版权声明:本文为qq_35828475原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。