JAVA线程池Executor框架

本文深入探讨JAVA线程池的使用,包括为什么要使用线程池、线程池的基本知识如固定大小、单线程和缓存线程池,以及线程池的7个重要参数。介绍了线程池的拒绝策略,如AbortPolicy、CallerRunsPolicy等,并讨论了如何自定义线程池以及如何确定线程池最大数,遵循阿里巴巴Java开发手册的建议避免使用Executors创建线程池。
摘要由CSDN通过智能技术生成

JAVA线程池

为什么用线程池

线程池做的工作主要是控制运行的线程的数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量超出数量的线程排队等候,等其它线程执行完毕,再从队列中取出任务来执行。他的主要特点为∶线程复用;控制最大并发数;管理线程。

  • 第一∶降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的消耗。
  • 第二∶提高响应速度。当任务到达时,任务可以不需要的等到线程创建就能立即执行。
  • 第三∶提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控

线程池基本知识

架构说明

线程池继承关系

Java 中的线程池是通过Executor框架实现的,该框架中用到了Executor,Executors,ExecutorService, ThreadPoolExecutor这几个类。
类比其他工具类如Arrays,Collections,线程池的工具类是Executors。

JDK提供的五种线程池

支持的线程池种类五种,除了java8新出的Executors.newWorkStealingPool()底层是ForkJoinPool,其他四个底层都是ThreadPoolExecutor。

//需指定线程数量,一池固定多线程,适用于执行长期任务
        Executors.newFixedThreadPool(10);
        //一池一线程,适用于执行单个任务
        Executors.newSingleThreadExecutor();
        //一池多线程,自动扩容,适用于大量短期异步小任务,负载轻的服务
        Executors.newCachedThreadPool();
        //调度带时间
        Executors.newScheduledThreadPool(10);
        //java8 新出
        Executors.newWorkStealingPool();
Executors.newFixedThreadPool()
  • 构造源码:
/**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
   
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
  • 主要特点如下∶
    1. 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
    2. newFixedThreadPool创建的线程池corePoolSize和maximumPoolSize值是相等的,它使用的LinkedBlockingQueue;
  • 代码示例:
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        try {
   
            //threadPool.submit(); //用于执行带返回值的任务
            //threadPool.execute();
            // 模拟10个线程需要执行
            for (int i = 0; i < 10; i++) {
   
                fixedThreadPool.execute(()->{
   
                    System.out.println(Thread.currentThread()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胡宝全

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

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

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

打赏作者

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

抵扣说明:

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

余额充值