铁匠 铁匠
首页
收藏
java
架构之路
常用算法
  • Java
  • nginx
  • 系统运维
  • 系统安全
  • mysql
  • redis
参考文档
关于
链接
  • 分类
  • 标签
  • 归档

专注、不予评判地关注当下
首页
收藏
java
架构之路
常用算法
  • Java
  • nginx
  • 系统运维
  • 系统安全
  • mysql
  • redis
参考文档
关于
链接
  • 分类
  • 标签
  • 归档
  • java api 文档
  • 集合

  • 版本特性

  • jvm

  • 网络编程

  • 并发编程

    • 并发编程三大特性
    • 线程的基本概念
    • 如何正确停止一个线程
    • java线程池
      • jdk提供的常用线程池
        • 参数含义
        • 队列类型
        • JDK 提供的四种拒绝策略
      • 线程池任务执行流程
      • 参考
    • 线程池大小设置多少合适
    • java 常用队列
    • 使用 Semaphore 实现一个简单限流器
  • java
  • 并发编程
FengJianxin
2019-03-10
目录

java线程池

# jdk提供的常用线程池

注意

在高并发场景下不要使用Executors创建线程池,需要根据实际场景定制参数。

使用Executors静态方法可以创建5种不同配置和类型的线程池

  • newCachedThreadPool() 用来创建可缓存的线程,当没有缓存线程可用时,就创建新的工作线程;如果线程闲置的时间超过60秒,将会被移出缓存,内部使用SynchronousQueue作为工作队列。一般用来处理大量短时间任务。

    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }
    
    1
    2
    3
    4
    5
    6
  • newFixedThreadPool(int nThreads) 创建固定大小为nThreads的线程池,当活动任务数量超过nThreads,新的任务将在工作队列中等待。具有固定的线程开销,当任务过多时任务需要等待,任务数少时则会有线程浪费。

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
    
    1
    2
    3
    4
    5
  • newSingleThreadExecutor() 永远只有1个工作线程,保证所有任务按顺序执行。使用FinalizableDelegatedExecutorService包装,屏蔽了很多方法,限制了线程池实例无法修改,例如改变线程池数量。

    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
    
    1
    2
    3
    4
    5
    6
  • newSingleThreadScheduledExecutor / newScheduledThreadPool(int corePoolSize) 返回ScheduledExecutorService对象,用于执行定时或者周期性任务,区别在与工作线程是1个还是多个。

    public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }
    
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
  • newWorkStealingPool(int parallelism) jdk8加入的api,返回ForkJoinPool对象。

    public static ExecutorService newWorkStealingPool(int parallelism) {
        return new ForkJoinPool
            (parallelism,
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }
    
    1
    2
    3
    4
    5
    6

ThreadPoolExecutor构造函数

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
1
2
3
4
5
6
7

# 参数含义

参数 含义
corePoolSize 核心线程数
maximumPoolSize 最大线程数
keepAliveTime 空闲线程的存活时间,当线程数大于 corePoolSize 时,超过keepAliveTime没有执行任务的线程讲会被回收
unit keepAliveTime 时间单位
workQueue 任务队列
threadFactory 线程工厂类,用于创建线程,jdk 提供有默认实现
handler 拒绝策略,当被拒绝时候调用

# 队列类型

(图片来自网络)

队列类型

# JDK 提供的四种拒绝策略

(图片来自网络)

拒绝策略

# 线程池任务执行流程

(图片来自网络)

执行流程

# 参考

  • Java线程池实现原理及其在美团业务中的实践 (opens new window)
#并发编程
如何正确停止一个线程
线程池大小设置多少合适

← 如何正确停止一个线程 线程池大小设置多少合适→

最近更新
01
策略模式
01-09
02
模板方法
01-06
03
观察者模式
01-06
更多文章>
Theme by Vdoing | Copyright © 2016-2023 铁匠 | 粤ICP备15021633号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式