ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Executors工具类中的线程池使用及问题

2021-05-27 14:05:30  阅读:154  来源: 互联网

标签:return threadFactory Executors static 线程 new public 类中


概述

Executors工具类中定义了4种线程池,应对不同的使用场景,其实这四个线程池都是通过线程池构造函数,构造出来的,只是参数不同而已。

SingleThreadPool

定义

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

第一个为单线程池,方法返回一个只有一个线程的线程池。若多余一个任务被提交到该线程池,任务会被保存在一个任务队列中,待线程空闲,按先入先出的顺序执行队列中的任务。特点是核心线程数和最大线程数都为1,队列没有定义长度,即最大长度为Integer.MAX_VALUE

使用

ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(new MyThreadFactory("single-thread-pool-"));

FixedThreadPool

定义

// 构造方法1
 public static ExecutorService newFixedThreadPool(int nThreads) {
 return new ThreadPoolExecutor(nThreads, nThreads,
                               0L, TimeUnit.MILLISECONDS,
                               new LinkedBlockingQueue<Runnable>());
}
//构造方法2
public static ExecutorService newFixedThreadPool(int nThreads, 
ThreadFactory threadFactory) {
 return new ThreadPoolExecutor(nThreads, nThreads,
                               0L, TimeUnit.MILLISECONDS,
                               new LinkedBlockingQueue<Runnable>(),
                               threadFactory);
}

第二个是固定线程池,该方法返回一个固定线程数量的线程池。该线程池中的线程数量始终不变。当有一个新的任务提交时,线程池中若有空闲线程,则立即执行。若没有,则新的任务会被暂存在一个任务队列中,待有线程空闲时,便处理在任务队列中的任务。特点是核心线程数和最大线程数一样,由用户自定义,队列长度和SingleThreadPool一样,都是Integer.MAX_VALUE

使用

  ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5,new MyThreadFactory("fixed-thread-pool-"));

CacheThreadPool

定义

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

第三个为缓存线程池,该方法返回一个可根据实际情况调整线程数量的线程池。线程池的线程数量不确定,但若有空闲线程可以复用,则会优先使用可复用的线程。若所有线程均在工作,又有新的任务提交,则会创建新的线程处理任务。所有线程在当前任务执行完毕后,将返回线程池进行复用。特点是核心线程数为0,最大线程数无限制,为Integer.MAX_VALUE

使用

ExecutorService cachedThreadPool = Executors.newCachedThreadPool(new MyThreadFactory("cached-thread-pool-"));

ScheduleThreadPool

定义

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor
(ThreadFactory threadFactory) {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1, threadFactory));
}
public static ScheduledExecutorService newScheduledThreadPool
(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
public ScheduledThreadPoolExecutor(int corePoolSize,
                                   ThreadFactory threadFactory) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue(), threadFactory);
}

第四个为调度线程池, 可以设置线程执行的间隔,比如每小时执行一次,可以设定第一次执行的时间,马上执行还是延迟多久执行。特点是线程数无限制,为Integer.MAX_VALUE

使用

ScheduledExecutorService scheduledThreadPool = Executors
.newScheduledThreadPool(5, new MyThreadFactory("scheduled-thread-pool-"));
// 过多久后执行一次,只执行一次
scheduledThreadPool.schedule(new RunTest(), 1, TimeUnit.SECONDS);
scheduledThreadPool.schedule(Executors.callable(new RunTest()), 2, TimeUnit.SECONDS);
// task 第一次执行延迟时间 每次执行的间隔 间隔的时间类型
// 上一个执行完成 才会执行下一个 上一个执行超过时间间隔,则立即执行下一个
scheduledThreadPool.scheduleAtFixedRate(new RunTest(), 0, 1, 
TimeUnit.SECONDS);
// 上一次执行完成 再延迟固定时间后再执行下一次。
scheduledThreadPool.scheduleWithFixedDelay(new RunTest(), 0, 1, 
TimeUnit.SECONDS);

问题

工具类中的线程池使用不当可能会导致内存溢出,原因在于:

  • FixedThreadPool 和 SingleThreadExecutor : 允许请求的队列长度为 Integer.MAX_VALUE ,可能堆积大量的请求,从而导致OOM。
  • CachedThreadPool 和 ScheduledThreadPool : 允许创建的线程数量为 Integer.MAX_VALUE ,可能会创建大量线程,从而导致OOM。

总结

  • 平时工作中,在任务请求不多的情况下,可以使用SingleThreadExecutor或者FixedThreadPool线程池。
  • SingleThreadExecutor也可以用于任务的顺序执行。
  • ScheduledThreadPool 可用于定时打印数据,如打印本地缓存情况,使用率,命中率等信息。
  • 其它情况还是推荐自定义线程池

标签:return,threadFactory,Executors,static,线程,new,public,类中
来源: https://www.cnblogs.com/springs018/p/14817566.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有