线程池

2021-11-29  本文已影响0人  公子请留步LookMe

JDK自带线程池

自定义线程池中七个参数代表的含义

四种拒绝策略

简化案例:

/*      int corePoolSize = 2;最小线程数
        int maximumPoolSize = 5;最大线程数
        long keepAliveTime = 2L;存活时间
        TimeUnit timeUnit = TimeUnit.SECONDS; //时间单位
        ArrayBlockingQueue 阻塞队列
        Executors.defaultThreadFactory()  默认的线程工厂
        ThreadPoolExecutor.AbortPolicy()) 拒绝策略
*/
public class demo1 {
    public static void main(String[] args) {
        int corePoolSize = 2;//最小线程数
        int maximumPoolSize = 5;//最大线程数
        long keepAliveTime = 2L;// 存活时间
        TimeUnit timeUnit = TimeUnit.SECONDS; //时间单位
        //ArrayBlockingQueue 阻塞队列
        //  Executors.defaultThreadFactory()  默认的线程工厂
        // ThreadPoolExecutor.AbortPolicy()) 拒绝策略
        ExecutorService executor = new ThreadPoolExecutor(
                2, 5, 2l,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    }
}
上一篇 下一篇

猜你喜欢

热点阅读