【并发编程】- 线程池使用ArrayBlockingQueue和最大线程数的关系

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第28天,点击查看活动详情

接着上一篇文章-【并发编程】- 线程池使用LinkedBlockingDeque和最大线程数的关系

修改运行类代码如下:

public class LinkedBlockingQueueRun {
    public static void main(String[] args) {
        FirstRunnable firstRunnable = new FirstRunnable();
        LinkedBlockingDeque<Runnable> linkedBlockingDeque = new LinkedBlockingDeque<Runnable>(2);
        System.out.println("队列个数:"+linkedBlockingDeque.size());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, linkedBlockingDeque);
        for (int i = 0; i < 6; i++) {
            threadPoolExecutor.execute(firstRunnable);
        }
        System.out.println("线程池正在执行的任务个数:"+threadPoolExecutor.getPoolSize());
        System.out.println("线程池使用的队列个数:"+linkedBlockingDeque.size());
    }
}

运行结果如下:

队列个数:0
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.ozx.concurrentprogram.executor.service.FirstRunnable@29444d75 rejected from java.util.concurrent.ThreadPoolExecutor@44e81672[Running, pool size = 3, active threads = 3, queued tasks = 2, 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.ozx.concurrentprogram.executor.controller.LinkedBlockingQueueRun.main(LinkedBlockingQueueRun.java:22)
pool-1-thread-2  开始时间:14:59:16
pool-1-thread-3  开始时间:14:59:16
pool-1-thread-1  开始时间:14:59:16
pool-1-thread-3  结束时间:14:59:17
pool-1-thread-1  结束时间:14:59:17
pool-1-thread-2  结束时间:14:59:17
pool-1-thread-1  开始时间:14:59:17
pool-1-thread-3  开始时间:14:59:17
pool-1-thread-3  结束时间:14:59:18
pool-1-thread-1  结束时间:14:59:18

从运行结果看出,其中一个任务被拒绝执行了,其他任务正常执行。

线程池使用ArrayBlockingQueue队列和线程池最大线程数关系

线程执行代码如下:

public class FirstRunnable implements Runnable {


    @Override
    public void run() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        try {
            System.out.println(Thread.currentThread().getName() +"  开始时间:"+simpleDateFormat.format(new Date()));
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() +"  结束时间:"+simpleDateFormat.format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行类代码如下:

public class ArrayBlockingQueueRun {
    public static void main(String[] args) {
        FirstRunnable firstRunnable = new FirstRunnable();
        ArrayBlockingQueue<Runnable> arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(2);
        System.out.println("队列个数:"+arrayBlockingQueue.size());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, arrayBlockingQueue);
        for (int i = 0; i < 5 ; i++) {
            threadPoolExecutor.execute(firstRunnable);
        }
        System.out.println("线程池正在执行的任务个数:"+threadPoolExecutor.getPoolSize());
        System.out.println("线程池使用的队列个数:"+arrayBlockingQueue.size());
    }
}

猜你喜欢

转载自juejin.im/post/7111960181465939981