JUC学习——自定义线程池

本文介绍了自定义Java线程池的关键知识点,包括四大创建方法:单线程、固定大小、缓存和周期性线程池,以及线程池核心参数如corePoolSize、maximumPoolSize、keepAliveTime和工作队列等。同时,详细阐述了四种拒绝策略:AbortPolicy、CallerRunsPolicy、DiscardPolicy和DiscardOldestPolicy,帮助读者掌握自定义线程池的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义线程池需要了解四大方法、七大参数、四种拒绝策略

1、常用的创建线程池的四大方法

(1)创建单个线程的线程池:Executors.newSingleThreadExecutor();

//源码
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

(2)创建固定大小的线程池:Executors.newFixedThreadPool(5);

//源码
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

(3)创建不固定大小的缓存线程池:Executors.newCachedThreadPool();

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

(4)创建周期性线程池: Executors.newScheduledThreadPool(5);

//源码
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }

2、四种创建线程池的方法都是调用了ThreadPoolExecutor类来创建。ThreadPoolExecutor有哪七大参数?

//ThreadPoolExecutor类的构造函数
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
 
    }
  • int corePoolSize 线程池的基本大小
  • int maximumPoolSize 允许的最大线程数(1、CPU密集型 2、IO密集型)
  • long keepAliveTime 线程池中空闲线程等待工作的超时时间
  • TimeUnit unit 时间单位
  • BlockingQueue workQueue 阻塞队列,存放等待的任务
  • ThreadFactory threadFactory 线程工厂,默认一般不用改
  • RejectedExecutionHandler handler 拒绝策略

3、四种拒绝策略

(1)线程数量达到上限,直接抛出异常,也是默认的拒绝策略: ThreadPoolExecutor.AbortPolicy()
(2)线程数量达到上限,把任务队列中的任务放在调用者线程当中运行: ThreadPoolExecutor.CallerRunsPolicy()
(3)线程数量达到上限,会默默丢弃无法处理的任务,不予任何处理: ThreadPoolExecutor.DiscardPolicy()
(4)线程数量达到上限,丢弃最先被添加进去的任务,并尝试再次提交: ThreadPoolExecutor.DiscardOldestPolicy()

四种创建线程池方法的参数对比:

\corePoolSizemaximumPoolSizekeepAliveTimeTimeUnitBlockingQueueThreadFactoryRejectedExecutionHandler
Executors.newSingleThreadExecutor()110LTimeUnit.MILLISECONDSLinkedBlockingQueue(Integer.MAX_VALUE)Executors.defaultThreadFactory()new AbortPolicy()
Executors.newFixedThreadPool(5)550LTimeUnit.MILLISECONDSLinkedBlockingQueue(Integer.MAX_VALUE)Executors.defaultThreadFactory()new AbortPolicy()
Executors.newCachedThreadPool()0Integer.MAX_VALUE60LTimeUnit.MILLISECONDSSynchronousQueue(false)Executors.defaultThreadFactory()new AbortPolicy()
Executors.newScheduledThreadPool(5)5Integer.MAX_VALUEDEFAULT_KEEPALIVE_MILLIS = 10LMILLISECONDSDelayedWorkQueue()Executors.defaultThreadFactory()new AbortPolicy()

4、看到这里大家应该都知道如何自定义创建线程池了吧!可以通过直接调用ThreadPooExecutor类进行创建。



只要保持微笑,生活也会为你抹去眼角的泪水!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讨厌令狐冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值