IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Java并发编程系列23:Executors框架和ThreadPoolExecutor实现类了解 -> 正文阅读

[Java知识库]Java并发编程系列23:Executors框架和ThreadPoolExecutor实现类了解

Executors框架成员:

ThreadPoolExecutor实现类、ScheduledThreadPoolExecutor实现类、Future接口、Runnable接口、Callable接口和Executors

java.util.concurrent.Executor : 负责线程的使用与调度的根接口?
 |–ExecutorService:Executor的子接口,线程池的主要接口?
   |–ThreadPoolExecutor:ExecutorService的实现类?
   |–ScheduledExecutorService:ExecutorService的子接口,负责线程的调度?
     |–ScheduledThreadPoolExecutor:既继承了ThreadPoolExecutor,同时实现了ScheduledExecutorService

ThreadPoolExecutor实现类了解:

构造方法:

????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);

????}

参数介绍:

  1. int corePoolSize, 核心线程大小
  2. int maximumPoolSize,最大线程大小
  3. long keepAliveTime, 超过corePoolSize的线程多久不活动被销毁时间
  4. TimeUnit unit,时间单位
  5. BlockingQueue<Runnable> workQueue 任务队列
  6. ThreadFactory threadFactory 线程池工厂
  7. RejectedExecutionHandler handler 拒绝策略

以下介绍三种类型的ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool和CachedThreadPool。

这3种线程池都是创建了ThreadPollExecutor对象,只是传递的参数不一样,传入的workQueue 都是默认,即最大可添加Integer.MAX_VALUE个任务,这也就是阿里巴巴java开发规范禁止直接使用java提供的默认线程池的原因。

参考:https://blog.csdn.net/weixin_41888813/article/details/90769126

1、FixedThreadPool

线程数量固定的线程池(定长线程池)可控制线程最大并发数,超出的线程会在队列中等待。

适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。

????public static ExecutorService newFixedThreadPool(int nThreads) {

????????return new ThreadPoolExecutor(nThreads, nThreads,

??????????????????????????????????????0L, TimeUnit.MILLISECONDS,

??????????????????????????????????????new LinkedBlockingQueue<Runnable>());

}

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {

????????return new ThreadPoolExecutor(nThreads, nThreads,

??????????????????????????????????????0L, TimeUnit.MILLISECONDS,

??????????????????????????????????????new LinkedBlockingQueue<Runnable>(),

??????????????????????????????????????threadFactory);

}

  1. 初始线程数为:nThreads
  2. 最大线程数为:nThreads
  3. 超时时间为:0毫秒
  4. 阻塞队列采用的是:LinkedBlockingQueue

线程池最大线程数量maxmumPoolSize和核心线程池的数量corePoolSize设置为相等,使用LinkedBlockingQueue作为阻塞队列

2、SingleThreadExecutor

只有一个线程的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。

????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. 初始线程数为:1
  2. 最大线程数为:1
  3. 超时时间为:0毫秒
  4. 阻塞队列采用的是:LinkedBlockingQueue

3、newCachedThreadPool

一个可缓存线程池如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

它适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。

????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);

????}

  1. 初始线程数为:0
  2. 最大线程数为:?Integer.MAX_VALUE(int的最大值)
  3. 超时时间为:1分钟
  4. 阻塞队列采用的是:SynchronousQueue

参考:https://www.cnblogs.com/xiondun/p/14764096.html

参考:https://blog.csdn.net/tongdanping/article/details/79625109

参考:https://blog.csdn.net/qq_43040688/category_9910156.html

参考:https://blog.csdn.net/qq_43040688/article/details/106046629

参考:关于阿里规范禁止使用Executors创建线程池的分析

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-02-09 20:33:44  更:2022-02-09 20:35:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/3 7:44:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码