使用JAVA线程池(Executor/ThreadPoolExecutor)

鸣谢:如果您觉得本文对您有帮助,请点赞和收藏,Thanks。

一、为什么使用线程池

可以减少创建线程、销毁线程和切换线程带来的资源消耗

二、线程池应该设置多大

方法一:公式计算

  • 最佳线程数目 = ((线程等待时间+线程CPU时间)/ 线程CPU时间 )*CPU数目

比如平均每个线程CPU运行时间为0.5s,而线程等待时间(非CPU运行时间,比如IO)为1.5s,CPU核心数为8,那么根据上面这个公式估算得到:((0.5+1.5)/0.5)*8=32。这个公式进一步转化为:

  • 最佳线程数目 = (线程等待时间与线程CPU时间之比 + 1)* CPU数目

方法二:按CPU核数

  • CPU密集型(CPU性能差,IO操作效率高)
    服务器的CPU性能相对硬盘和内存来说很多,在运行任务是CPU通常处于接近满载的状态,硬盘操作和IO操作能在很短的时间完成,CPU的使用率很高,这种情况线程池通常设置为:
    线程池大小 = CPU核数 + 1
  • IO密集型(CPU性能好,IO操作效率低)
    服务器的CPU性能相对硬盘和内存来说很多,运行任务是大部分时间都是在等待IO读写,IO操作频繁,CPU使用率不高,可以让CPU等待IO的时候处理别的任务,这种情况通常设置为:
    线程池大小 = CPU核数 * 2

三、创建线程池的方法

使用jdk自带的Executor框架,其UML图如下:
在这里插入图片描述
1. 隐式创建(阿里巴巴规范不推荐,无法直观看到线程池信息)
Java通过Executors提供四种线程池,分别为:
①newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需 要,可灵活回收空闲线程,若无可回收,则新建线程。
②newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的 线程会在队列中等待。
③newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执 行。
④newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
创建方法:Executors.newScheduledThreadPool(10);
2. 显示创建(推荐,更加直观地看到线程池的信息)
利用实现类创建线程池,此类提供了四个构造方法,分别是:

public ThreadPoolExecutor(int corePoolSize,
                         int maximumPoolSize,
                         long keepAliveTime,
                         TimeUnit unit,
                         BlockingQueue<Runnable> workQueue) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
        Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
                         int maximumPoolSize,
                         long keepAliveTime,
                         TimeUnit unit,
                         BlockingQueue<Runnable> workQueue,
                         ThreadFactory threadFactory) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
        threadFactory, defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
                         int maximumPoolSize,
                         long keepAliveTime,
                         TimeUnit unit,
                         BlockingQueue<Runnable> workQueue,
                         RejectedExecutionHandler handler) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
        Executors.defaultThreadFactory(), handler);
}
public ThreadPoolExecutor(int corePoolSize,
                         int maximumPoolSize,
                         long keepAliveTime,
                         TimeUnit unit,
                         BlockingQueue<Runnable> workQueue,
                         ThreadFactory threadFactory,
                         RejectedExecutionHandler handler) {
   if (corePoolSize < 0 ||
       maximumPoolSize <= 0 ||
       maximumPoolSize < corePoolSize ||
       keepAliveTime < 0)
       throw new IllegalArgumentException();
   if (workQueue == null || threadFactory == null || handler == null)
       throw new NullPointerException();
   this.acc = System.getSecurityManager() == null ?
           null :
           AccessController.getContext();
   this.corePoolSize = corePoolSize;
   this.maximumPoolSize = maximumPoolSize;
   this.workQueue = workQueue;
   this.keepAliveTime = unit.toNanos(keepAliveTime);
   this.threadFactory = threadFactory;
   this.handler = handler;
}

创建方法:

//设置线程名称
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
//创建线程池(部分参数可默认)
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5, 30L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);

下面解析下各项参数的意义:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190520172422833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM5OTg0NjY=,size_16,color_FFFFFF,t_

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值