当前位置:网站首页>线程应用实例
线程应用实例
2022-07-02 18:01:00 【桑英豪】
一.等待超时模式
场景:调用一个方法时等待一段时间,如果该方法能够在给定的时间之内得到结果,将立即返回,反之,如果超时返回默认结果。
二.数据库连接池
连接池的定义:构造函数初始化连接的最大上限,通过一个双向队列来维护连接,调用方需要先调用fetchConnection(long)方法来指定多少毫秒内超时获取连接,当连接使用完成后,需要调用releaseConnection(Connection)方法将连接放回连接池。
三.线程池技术
3.1线程池的作用
面对成千上万的任务递交进服务器时,如果还是采用一个任务一个线程的方式,那么将会创建数以万记的线程,这不是一个好的选择,因为这会使操作系统频繁的进行线程上下文切换,无故增加系统的负载,而线程的创建和消亡都是需要耗费系统资源的,也无疑浪费了系统资源。线程池很好的解决了这个问题,它预先创建了若干的线程,并且不能由用户直接对线程的创建进行控制
3.2线程池的优势
- 降低资源消耗
- 提高响应速度
- 提高线程的可管理性
3.3 线程池中的几个参数
1: 核心线程数(corePoolSize)
核心线程数的设计需要依据任务的处理时间和每秒产生的任务数量来确定,例如:执行一个任务需要0.1秒,系统百分之80的时间每秒都会产生100个任务,那么要想在1秒内处理完这100个任务,就需要10个线程,此时我们就可以设计核心线程数为10;当然实际情况不可能这么平均,所以我们一般按照8020原则设计即可,既按照百分之80的情况设计核心线程数,剩下的百分之20可以利用最大线程数处理;
2:任务队列长度(workQueue)
任务队列长度一般设计为:核心线程数/单个任务执行时间*2即可;例如上面的场景中,核心线程数设计为10,单个任务执行时间为0.1秒,则队列长度可以设计为200;
3:最大线程数(maximumPoolSize)
最大线程数的设计除了需要参照核心线程数的条件外,还需要参照系统每秒产生的最大任务数决定:例如:上述环境中,如果系统每秒最大产生的任务是1000个,那么,最大线程数=(最大任务数-任务队列长度)*单个任务执行时间;既: 最大线程数=(1000-200)*0.1=80个;
4:最大空闲时间(keepAliveTime)
这个参数的设计完全参考系统运行环境和硬件压力设定,没有固定的参考值,用户可以根据经验和系统产生任务的时间间隔合理设置一个值即可;
3.4线程池工作流程
3.5Executor和Executors的区别
Executor 接口对象能执行我们的线程任务;
Executors 工具类的不同方法按照我们的需求创建了不同的线程池,来满足业务的需求。
ExecutorService 接口继承了Executor接口并进行了扩展,提供了更多的方法,我们能够获得任务执行的状态并且可以获取任务的返回值。
3.6你知道怎么创建线程池吗?
ThreadPoolExecutor
边栏推荐
- Fastdfs installation
- 2022 software engineering final exam recall Edition
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- 【测试开发】一文带你了解什么是软件测试
- Stm32g0 USB DFU upgrade verification error -2
- What are the links of the problem
- 论文导读 | 关于将预训练语言模型作为知识库的分析与批评
- 数字滚动带动画
- Learn the knowledge points of eight part essay ~ ~ 1
猜你喜欢

新手必看,點擊兩個按鈕切換至不同的內容

Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比

Kubernetes three open interfaces first sight

How to clean up discarded PVs and their corresponding folders

新手必看,点击两个按钮切换至不同的内容

数据降维——主成分分析

Imitation Jingdong magnifying glass effect (pink teacher version)

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey

电商系统中常见的 9 大坑,你踩过没?

How can retail enterprises open the second growth curve under the full link digital transformation
随机推荐
2022.7.1-----leetcode.241
《病人家属,请来一下》读书笔记
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
Use cheat engine to modify money, life and stars in Kingdom rush
[daily question] the next day
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
PyTorch函数中的__call__和forward函数
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
2022软件工程期末考试 回忆版
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
Deep learning mathematics foundation
[daily question] first day
SQL training 2
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
学习八股文的知识点~~1