当前位置:网站首页>Thread application instance
Thread application instance
2022-07-02 19:16:00 【Sang Yinghao】
One . Wait timeout mode
scene : Wait for a period of time when calling a method , If this method can get results within a given time , Will return immediately , conversely , If timeout, return the default result .
Two . Database connection pool
Definition of connection pool : Constructor initializes the maximum limit of the connection , Maintain connections through a two-way queue , The caller needs to call fetchConnection(long) Method to specify how many milliseconds to timeout to get a connection , When the connection is used , Need to call releaseConnection(Connection) Method to put the connection back into the connection pool .
3、 ... and . Thread pool technology
3.1 Role of thread pool
face Thousands of people Service delivery hand over Into the clothing service device when , If you still use one service One Line The way of Cheng , Then it will gen Build tens of thousands remember Of Line cheng , this It's not a good choice , Because this will make the operating system switch thread context frequently , Increase the load of the system for no reason , The creation and extinction of threads require system resources , It also undoubtedly wastes system resources .Thread pool solves this problem well , It creates several threads in advance , And it can't be directly controlled by users to create threads
3.2 Advantages of thread pool
- Reduce resource consumption
- Improve response time
- Improve the manageability of threads
3.3 Several parameters in the thread pool
1: Number of core threads (corePoolSize)
The design of the number of core threads needs to be determined according to the processing time of the task and the number of tasks generated per second , for example : Performing a task requires 0.1 second , The system is... Percent 80 Every second of the time will produce 100 A mission , So if you want to be in 1 Finish this in seconds 100 A mission , Need 10 Threads , At this point, we can design the number of core threads as 10; Of course, the actual situation cannot be so average , So we usually follow 8020 Principle design is enough , Both according to percent 80 Design the number of core threads in the case of , The remaining percent 20 The maximum number of threads can be used to process ;
2: Task queue length (workQueue)
The length of task queue is generally designed as : Number of core threads / Execution time of single task *2 that will do ; For example, in the above scene , The number of core threads is designed as 10, The execution time of a single task is 0.1 second , The queue length can be designed as 200;
3: Maximum number of threads (maximumPoolSize)
The design of the maximum number of threads needs to refer to the conditions of the number of core threads , You also need to refer to the maximum number of tasks generated by the system per second : for example : In the above environment , If the maximum task generated by the system per second is 1000 individual , that , Maximum number of threads =( Maximum number of tasks - Task queue length )* Execution time of single task ; Both : Maximum number of threads =(1000-200)*0.1=80 individual ;
4: Maximum free time (keepAliveTime)
The design of this parameter completely refers to the system operating environment and hardware pressure setting , There is no fixed reference value , The user can set a reasonable value according to experience and the time interval of the task generated by the system ;
3.4 Thread pool workflow
3.5Executor and Executors The difference between
Executor Interface objects can perform our threading tasks ;
Executors Different methods of tool classes create different Thread pool , To meet the needs of the business .
ExecutorService Interface inherited Executor Interface and extended , More ways , We can get the execution status of the task and the return value of the task .
3.6 Do you know how to create a thread pool ?
ThreadPoolExecutor
边栏推荐
- 论文导读 | 关于将预训练语言模型作为知识库的分析与批评
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- ORA-01455: converting column overflows integer datatype
- Markdown basic grammar
- 【JVM调优实战100例】03——JVM堆调优四例
- UML class diagram
- 全志A33使用主线U-Boot
- High frequency interview questions
- In pytorch function__ call__ And forward functions
- 【测试开发】软件测试—概念篇
猜你喜欢
High frequency interview questions
仿京东放大镜效果(pink老师版)
Stm32g0 USB DFU upgrade verification error -2
Tips for material UV masking
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Imitation Jingdong magnifying glass effect (pink teacher version)
消息队列消息丢失和消息重复发送的处理策略
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
Installation of thingsboard, an open source IOT platform
随机推荐
【ERP软件】ERP体系二次开发有哪些危险?
新手必看,點擊兩個按鈕切換至不同的內容
metric_ Logger urination
Deep learning mathematics foundation
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Tips for material UV masking
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
页面标题组件
yolov3 训练自己的数据集之生成train.txt
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
2022编译原理期末考试 回忆版
How to copy and paste interlaced in Excel
How performance testing creates business value
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Progress progress bar
思维意识转变是施工企业数字化转型成败的关键
Emmet basic syntax
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
[test development] takes you to know what software testing is
2022.7.1-----leetcode.241