当前位置:网站首页>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
边栏推荐
- Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
- Tips for material UV masking
- 数据降维——因子分析
- Emmet基础语法
- 新手必看,点击两个按钮切换至不同的内容
- C file input operation
- What is 9D movie like? (+ common sense of dimension space)
- Crypto usage in nodejs
- Yunna | why use the fixed asset management system and how to enable it
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
猜你喜欢

Processing strategy of message queue message loss and repeated message sending

机器学习笔记 - 时间序列预测研究:法国香槟的月销量
![[fluent] dart data type (VaR data type | object data type)](/img/1b/fe2529af5f6663fad1fb7861f14ab5.jpg)
[fluent] dart data type (VaR data type | object data type)

PyTorch函数中的__call__和forward函数

线程应用实例

Hospital online inquiry source code hospital video inquiry source code hospital applet source code
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises

Mini Golf Course: a good place for leisure and tourism in London

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
随机推荐
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
拦截器与过滤器的区别
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Crypto usage in nodejs
消息队列消息丢失和消息重复发送的处理策略
Golang并发编程——goroutine、channel、sync
Preprocessing and preprocessing macros
Markdown基础语法
The difference between interceptor and filter
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
UML class diagram
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
Progress-进度条
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
页面标题组件
电脑使用哪个录制视频软件比较好
Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey