线程池七大参数-实验解析

package com.thread.excutor;

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

/**
 * public ThreadPoolExecutor(int corePoolSize,
 *                               int maximumPoolSize,
 *                               long keepAliveTime,
 *                               TimeUnit unit,
 *                               BlockingQueue<Runnable> workQueue,
 *                               ThreadFactory threadFactory,
 *                               RejectedExecutionHandler handler){}
 *
 *
 * Creates a new ThreadPoolExecutor with the given initial parameters.
 * Params:
 * corePoolSize – 核心线程池大小:线程池中会维护一个最小的线程数量,即使这些线程处理空闲状态,他们也不会被销毁, 除非设置了allowCoreThreadTimeOut
 * maximumPoolSize – 线程池不会无限制的去创建新线程,它会有一个最大线程数量的限制,这个数量即由maximunPoolSize指定
 * keepAliveTime – 一个线程如果处于空闲状态,并且当前的线程数量大于corePoolSize,那么在指定时间后,这个空闲线程会被销毁,
 *                 如果allowCoreThreadTimeOut被设置为true时,无论线程数多少,线程处于空闲状态超过一定时间就会被销毁掉
 * unit – keepAliveTime参数的时间单位
 * workQueue – 用于在执行任务之前保存任务的队列,此队列将仅保存execute方法提交的Runnable任务
 * threadFactory – executor创建新线程时要使用的工厂
 * handler – 当由于达到线程边界和队列容量而阻止执行时要使用的处理程序
 * Throws:
 * IllegalArgumentException – if one of the following holds:
 * corePoolSize < 0
 * keepAliveTime < 0
 * maximumPoolSize <= 0
 * maximumPoolSize < corePoolSize
 * NullPointerException – if workQueue or threadFactory or handler is null
 */
public class SevenParametersTest {
    public static void main(String[] args) {
        ThreadPoolExecutor executorService = (ThreadPoolExecutor) buildThreadPoolExecutor_3();
        int activeCount = -1;
        int queueSize = -1;

        while (true) {
            if (activeCount != executorService.getActiveCount() || queueSize != executorService.getQueue().size()) {
                // 返回正在执行任务的大致线程数。
                System.out.println("getActiveCount = " + executorService.getActiveCount());
                System.out.println("getCorePoolSize = " + executorService.getCorePoolSize());
                System.out.println("getQueue = " + executorService.getQueue().size());
                System.out.println("getMaximumPoolSize = " + executorService.getMaximumPoolSize());

                activeCount = executorService.getActiveCount();
                queueSize = executorService.getQueue().size();
                System.out.println("=======================");
            }
        }

    }
    
    // 1.coresize=1, Maxsize=2, blockingQueue size = 1, what happen when submit 1 task?
    private static ExecutorService buildThreadPoolExecutor() {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Thread::new,
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("The ThreadPoolExecutor create done.");
        executorService.execute(() -> sleepSeconds(100L));
        return executorService;
        /**
         * The ThreadPoolExecutor create done.
         *  ---- Thread-0 ----
         * getActiveCount = 1
         * getCorePoolSize = 1
         * getQueue = 0
         * getMaximumPoolSize = 2
         * =======================
         */
    }

    // 1.coresize=1, Maxsize=2, blockingQueue size = 1, what happen when submit 2 task?
    private static ExecutorService buildThreadPoolExecutor_1() {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Thread::new,
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("The ThreadPoolExecutor create done.");
        executorService.execute(() -> sleepSeconds(100L));
        executorService.execute(() -> sleepSeconds(100L));
        return executorService;
        /**
         * 有新任务进入,无论如何-首先进入阻塞队列中。
         * The ThreadPoolExecutor create done.
         *  ---- Thread-0 ----
         * getActiveCount = 1
         * getCorePoolSize = 1
         * getQueue = 1
         * getMaximumPoolSize = 2
         * =======================
         */
    }
    
    // 1.coresize=1, Maxsize=2, blockingQueue size = 1, what happen when submit 3 task?
    private static ExecutorService buildThreadPoolExecutor_2() {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Thread::new,
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("The ThreadPoolExecutor create done.");
        executorService.execute(() -> sleepSeconds(10L));
        executorService.execute(() -> sleepSeconds(9L));
        executorService.execute(() -> sleepSeconds(9L));
        return executorService;
        /**
         * The ThreadPoolExecutor create done.
         *  ---- Thread-0 ----
         *  ---- Thread-1 ----
         * getActiveCount = 2
         * getCorePoolSize = 1
         * getQueue = 1
         * getMaximumPoolSize = 2
         * =======================
         *  ---- Thread-1 ----
         * getActiveCount = 2
         * getCorePoolSize = 1
         * getQueue = 0
         * getMaximumPoolSize = 2
         * =======================
         * getActiveCount = 1
         * getCorePoolSize = 1
         * getQueue = 0
         * getMaximumPoolSize = 2
         * =======================
         * getActiveCount = 0
         * getCorePoolSize = 1
         * getQueue = 0
         * getMaximumPoolSize = 2
         * =======================
         */

    }
    // 1.coresize=1, Maxsize=2, blockingQueue size = 1, what happen when submit 4 task?
    private static ExecutorService buildThreadPoolExecutor_3() {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Thread::new,
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("The ThreadPoolExecutor create done.");
        executorService.execute(() -> sleepSeconds(10L));
        executorService.execute(() -> sleepSeconds(10L));
        executorService.execute(() -> sleepSeconds(10L));
        executorService.execute(() -> sleepSeconds(10L));
        return executorService;
        /**
         *The ThreadPoolExecutor create done.
         *  ---- Thread-0 ----
         *  ---- Thread-1 ----
         * Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.thread.excutor.SevenParametersTest$$Lambda$5/917142466@76ccd017 rejected from java.util.concurrent.ThreadPoolExecutor@182decdb[Running, pool size = 2, active threads = 2, queued tasks = 1, 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.thread.excutor.SevenParametersTest.buildThreadPoolExecutor_3(SevenParametersTest.java:165)
         * 	at com.thread.excutor.SevenParametersTest.main(SevenParametersTest.java:38)
         *  ---- Thread-0 ----
         */

    }
    

    private static void sleepSeconds(Long seconds) {
        try {
            System.out.println(" ---- " + Thread.currentThread().getName() + " ---- ");
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值