JDK17 的 ThreadPoolExecutor 不会在 execut(runnable) 后立即执行,这是什么新特性吗?

查看 22|回复 0
作者:yodhcn   
用 IDEA 新建了两个测试项目,一个 JDK8 ,另一个 JDK17
测试代码如下:
public class Task implements Runnable {
    private int no;
    private int size;
    public Task(int no, int size) {
        this.no = no;
        this.size = size;
    }
    @Override
    public void run() {
        try {
            System.out.println("执行中, Task: " + no + ", Thread: " + Thread.currentThread().getName() + ", queue.size: " + size);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Main {
    private static ExecutorService pool;
    public static void main(String[] args) {
        BlockingQueue queue = new ArrayBlockingQueue(8);
        pool = new ThreadPoolExecutor(
                2,
                5,
                1000,
                TimeUnit.MILLISECONDS,
                queue,
                Executors.defaultThreadFactory(),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("拒绝中, " + r.toString() + ", queue.size: " + queue.size());
                    }
                });
        for (int i = 0; i
JDK 8 环境下的运行结果:
执行中, Task: 1, Thread: pool-1-thread-2, queue.size: 0
执行中, Task: 10, Thread: pool-1-thread-3, queue.size: 8
执行中, Task: 11, Thread: pool-1-thread-4, queue.size: 8
执行中, Task: 12, Thread: pool-1-thread-5, queue.size: 8
执行中, Task: 0, Thread: pool-1-thread-1, queue.size: 0
拒绝中, [email protected], queue.size: 8
拒绝中, [email protected], queue.size: 8
执行中, Task: 2, Thread: pool-1-thread-4, queue.size: 0
执行中, Task: 3, Thread: pool-1-thread-3, queue.size: 1
执行中, Task: 4, Thread: pool-1-thread-1, queue.size: 2
执行中, Task: 5, Thread: pool-1-thread-2, queue.size: 3
执行中, Task: 6, Thread: pool-1-thread-5, queue.size: 4
执行中, Task: 7, Thread: pool-1-thread-4, queue.size: 5
执行中, Task: 9, Thread: pool-1-thread-3, queue.size: 7
执行中, Task: 8, Thread: pool-1-thread-1, queue.size: 6
JDK 17 环境下的运行结果:
拒绝中, [email protected], queue.size: 8
拒绝中, [email protected], queue.size: 8
执行中, Task: 11, Thread: pool-1-thread-4, queue.size: 8
执行中, Task: 10, Thread: pool-1-thread-3, queue.size: 8
执行中, Task: 12, Thread: pool-1-thread-5, queue.size: 8
执行中, Task: 0, Thread: pool-1-thread-1, queue.size: 0
执行中, Task: 1, Thread: pool-1-thread-2, queue.size: 0
执行中, Task: 3, Thread: pool-1-thread-4, queue.size: 1
执行中, Task: 2, Thread: pool-1-thread-3, queue.size: 0
执行中, Task: 4, Thread: pool-1-thread-5, queue.size: 2
执行中, Task: 5, Thread: pool-1-thread-2, queue.size: 3
执行中, Task: 6, Thread: pool-1-thread-1, queue.size: 4
执行中, Task: 7, Thread: pool-1-thread-4, queue.size: 5
执行中, Task: 8, Thread: pool-1-thread-3, queue.size: 6
执行中, Task: 9, Thread: pool-1-thread-2, queue.size: 7
结论
可见 JDK17 的 ThreadPoolExecutor ,在通过 execut 提交 runnable 后,不会立即执行被提交的 runnable ,而是等待一段时间。如果在这段等待时间内没有新的 runnable 提交,才开始执行。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部