当前位置:网站首页>线程池总结
线程池总结
2022-07-23 05:45:00 【wzf6667】
一 使用线程池的好处
池化技术相比大家已经屡见不鲜了,线程池、数据库连接池、Http 连接池等等都是对这个思想的应用。池化技术的思想主要是为了减少每次获取资源的消耗,提高对资源的利用率。
降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的消耗。
提高响应速度。当任务到达时,任务可以不需要的等到线程创建就能立即执行。
提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控。
二 Executor 框架
2.1 简介
Executor 框架是 Java5 之后引进的,在 Java 5 之后,通过 Executor 来启动线程比使用 Thread 的 start 方法更好,除了更易管理,效率更好(用线程池实现,节约开销)外,还有关键的一点:有助于避免 this 逃逸问题。
补充:this 逃逸是指在构造函数返回之前其他线程就持有该对象的引用. 调用尚未构造完全的对象的方法可能引发令人疑惑的错误。
Executor 框架不仅包括了线程池的管理,还提供了线程工厂、队列以及拒绝策略等,Executor 框架让并发编程变得更加简单。
2.2 Executor 框架结构(主要由三大部分组成)
任务(Runnable /Callable)
执行任务需要实现的 Runnable 接口 或 Callable接口。Runnable 接口或 Callable 接口 实现类都可以被 ThreadPoolExecutor 或 ScheduledThreadPoolExecutor 执行。任务的执行(Executor)
包括任务执行机制的核心接口 Executor ,以及继承自 Executor 接口的 ExecutorService 接口。ThreadPoolExecutor 和 ScheduledThreadPoolExecutor 这两个关键类实现了 ExecutorService 接口。
这里提了很多底层的类关系,但是,实际上我们需要更多关注的是 ThreadPoolExecutor 这个类,这个类在我们实际使用线程池的过程中,使用频率还是非常高的。
三 ThreadPoolExecutor 类简单介绍
ThreadPoolExecutor 3 个最重要的参数:
corePoolSize : 核心线程数线程数定义了最小可以同时运行的线程数量。
maximumPoolSize : 当队列中存放的任务达到队列容量的时候,当前可以同时运行的线程数量变为最大线程数。
workQueue: 当新任务来的时候会先判断当前运行的线程数量是否达到核心线程数,如果达到的话,信任就会被存放在队列中。
ThreadPoolExecutor 饱和策略定义:
如果当前同时运行的线程数量达到最大线程数量并且队列也已经被放满了任时,ThreadPoolTaskExecutor 定义一些策略:
ThreadPoolExecutor.AbortPolicy:抛出 RejectedExecutionException来拒绝新任务的处理。
ThreadPoolExecutor.CallerRunsPolicy:调用执行自己的线程运行任务,也就是直接在调用execute方法的线程中运行(run)被拒绝的任务,如果执行程序已关闭,则会丢弃该任务。因此这种策略会降低对于新任务提交速度,影响程序的整体性能。另外,这个策略喜欢增加队列容量。如果您的应用程序可以承受此延迟并且你不能任务丢弃任何一个任务请求的话,你可以选择这个策略。
ThreadPoolExecutor.DiscardPolicy: 不处理新任务,直接丢弃掉。
ThreadPoolExecutor.DiscardOldestPolicy: 此策略将丢弃最早的未处理的任务请求。
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 20, 2, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5));
poolExecutor.execute(new DemoTask(i));
四 生成线程池的方式
java中创建线程池的方式一般有两种:
1. 通过Executors工厂方法创建
FixedThreadPool
SingleThreadExecutor
CachedThreadPool
2. 通过new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue)自定义创建
另外《阿里巴巴 Java 开发手册》中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 构造函数的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险
Executors 返回线程池对象的弊端如下:
FixedThreadPool 和 SingleThreadExecutor : 允许请求的队列长度为 Integer.MAX_VALUE,可能堆积大量的请求,从而导致 OOM。
CachedThreadPool 和 ScheduledThreadPool : 允许创建的线程数量为 Integer.MAX_VALUE ,可能会创建大量线程,从而导致 OOM。
边栏推荐
- Steel structure review questions
- Blog building I: Framework selection
- [introduction to AUTOSAR com 4.com service layer module]
- 嵌入式从入门到精通(入土)——超详细知识点分享2
- [AUTOSAR candrive 2. understand the mapping relationship between communication HOH, canid and pduid]
- 比较临时的修改node_modules内包的源码并编译引用 修改依赖包
- The CUDA version of pytorch installed by anconda is inconsistent with the CUDA version of the system
- 二叉树的实现-c
- 高电压技术考题附答案
- 配置TX1的系统 + 设为固态盘启动
猜你喜欢

二叉树的实现-c

Analysis of 100 questions and answers in Higher Algebra

匿名上位机v7波形显示

Examen des principes fondamentaux de la structure en acier

Questions and answers of basic principles of steel structure

Interpretation of the paper: iterative feature representation method to improve the prediction performance of N7 methylguanosine (m7G) sites

linkerd服务网格调研笔记

表格个人简历

Summary of video coding and decoding related data

【AUTOSAR之FEE(非易失存储器Flash与Eeprom区别)】
随机推荐
快速排序的按区间的三个版本及优化--友友们不一定了解
Steel structure review questions
使用InfluxDB数据库的疑惑
Prometheus Operator使用指南笔记
剑指offer 青蛙跳楼梯
[AUTOSAR candrive 1. learn the function and structure of candrive]
简单实现栈的功能
VS属性配置相关知识
Configure TX1 system + set to solid-state disk startup
AWK 程序设计语言
TeX or LaTeX or MikTeX or TeX Live or CTeX
Anonymous upper computer V7 waveform display
【Autosar 存储Stack NVM】
hot 100深度优先
OBS plug-in Foundation
[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]
[AUTOSAR com 2. Advanced introduction to communication protocol stack]
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 3
博客搭建五:图床选择
C语言数据库:详细的说明用学生管理系统了解数据库的操作,简单易懂。