线程池(ThreadPoolExecutor)

转载:https://blog.csdn.net/rongxiaodong/article/details/126777087

在这里插入图片描述
在这里插入图片描述

 
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
 
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
        implements BlockingQueue<E>, java.io.Serializable {
    ...
 
 
    /**
     * Creates a {@code LinkedBlockingQueue} with a capacity of
     * {@link Integer#MAX_VALUE}.
     */
    public LinkedBlockingQueue() {
        this(Integer.MAX_VALUE);
    }
...
}

在这里插入图片描述

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());

在这里插入图片描述

public ThreadPoolExecutor(int corePoolSize, --核心线程数
                              int maximumPoolSize, --最大线程数
                              long keepAliveTime, --非核心线程数保留时间
                              TimeUnit unit, --keepAliveTime的时间单位
                              BlockingQueue<Runnable> workQueue, --工作队列
                              ThreadFactory threadFactory, --线程工厂
                              RejectedExecutionHandler handler --线程拒绝策略
)

在这里插入图片描述
在这里插入图片描述

//创建一个具有2个核心线程、5个最大线程,
//使用容量为10的ArrayBlockingQueue阻塞队列作为工作队列的线程池,
//使用默认的AbortPolicy拒绝策略
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
            2, 5,
            5, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10),
            new ThreadFactoryBuilder().setNameFormat("demo-threadpool-%d").get(),
            new ThreadPoolExecutor.AbortPolicy());

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值