当前位置:网站首页>线程应用实例
线程应用实例
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
边栏推荐
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
- Tips for material UV masking
- "Patient's family, please come here" reading notes
- Date tool class (updated from time to time)
- R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
- 二进制操作
- How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
- Fastdfs installation
- Mini Golf Course: a good place for leisure and tourism in London
猜你喜欢

全志A33使用主线U-Boot

Excel finds the same value in a column, deletes the row or replaces it with a blank value

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
![[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction

Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

STM32G0 USB DFU 升级校验出错-2

材质UV遮罩的技巧

开源物联网平台ThingsBoard的安装

在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
随机推荐
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
高频面试题
Masa framework - DDD design (1)
Date tool class (updated from time to time)
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
论文导读 | 机器学习在数据库基数估计中的应用
Yolov3 trains its own data set to generate train txt
codeforces每日5题(均1700)-第四天
What are the links of the problem
[test development] software testing - concept
学习八股文的知识点~~1
全志A33使用主线U-Boot
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Progress progress bar
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
二进制操作
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
[fluent] dart data type (VaR data type | object data type)
预处理和预处理宏