请问当队列满了之后,线程池会创建线程,那么新创建的线程还是会消费队列头的任务,刚提交的任务进任务队列,还是直接运行刚提交任务?

来源:3-2 增加线程

宝慕林5151746

2020-01-25

写回答

2回答

悟空

2020-01-25

直接运行刚提交任务


2
3
萨思给
回复
悟空
public class Test { public static void main(String[] args) { ExecutorService executorService = new ThreadPoolExecutor(2, 5, 0, TimeUnit.DAYS, new ArrayBlockingQueue<>(2), new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); return thread; } }); executorService.execute(() -> { System.out.println("任务1@" + Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { } }); //回复字数限制问题不能贴完全代码,复制executorService.execute块,修改输出任务数字1-7 } } 任务1@Thread-0 任务2@Thread-1 任务5@Thread-2 任务6@Thread-3 任务7@Thread-4 任务3@Thread-1 任务4@Thread-4 任务5,6, 7比任务3,4运行的早,看起来3和4都是被加到队列里了,队列满了后,新进来的任务(5,6,7)是直接创建线程执行的,然后再从队列头开始执行,所以是不是新任务来了不是必须经过队列的(5,6,7)
2021-03-06
共3条回复

萨思给

2021-03-06

public class Test {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(2,
                5, 0, TimeUnit.DAYS,
                new ArrayBlockingQueue<>(2), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        });
 
        executorService.execute(() -> {
            System.out.println("任务1@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
 
        executorService.execute(() -> {
            System.out.println("任务2@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
 
        executorService.execute(() -> {
            System.out.println("任务3@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
 
        executorService.execute(() -> {
            System.out.println("任务4@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
        executorService.execute(() -> {
            System.out.println("任务5@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
        executorService.execute(() -> {
            System.out.println("任务6@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
        executorService.execute(() -> {
            System.out.println("任务7@" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        });
    }
一个运行结果是
任务1@Thread-0
任务2@Thread-1
任务5@Thread-2
任务6@Thread-3
任务7@Thread-4
任务3@Thread-1
任务4@Thread-4
任务5,6, 7比任务3,4运行的早,看起来3和4都是被加到队列里了,队列满了后,新进来的任务(5,6,7)是直接创建线程执行的,然后再从队列头开始执行


0
1
悟空
是的,你说得对的,谢谢你的指出。
2021-03-07
共1条回复

深度解密Java并发工具,精通JUC,成为并发多面手

JUC全方位讲解,构建并发工具类知识体系

1599 学习 · 573 问题

查看课程