当前位置:网站首页>[concurrent programming] working mechanism and type of thread pool
[concurrent programming] working mechanism and type of thread pool
2022-07-03 08:32:00 【keeper42】
JDK Thread pool and working mechanism in
Creation of thread pool
ThreadPoolExecutor,jdk Parent class of all thread pool implementations
Meaning of each parameter
int corePoolSize : Number of core threads in the thread pool ,< corePoolSize , New threads will be created ,= corePoolSize , This task will be saved to BlockingQueue, If the prestartAllCoreThreads() The method will start at once corePoolSize Number of threads .
int maximumPoolSize, Maximum number of threads allowed ,BlockingQueue It's full, too ,< maximumPoolSize It will create a new thread again
long keepAliveTime, When the thread is idle , Time to live , This parameter is only in > corePoolSize Only useful
TimeUnit unit, Unit value of survival time
BlockingQueue<Runnable> workQueue, Save the blocking queue for the task
ThreadFactory threadFactory, A factory for creating threads , Give the new thread a name
RejectedExecutionHandler handler : Saturated strategy
AbortPolicy : Throw an exception directly , Default ;
CallerRunsPolicy: Use the thread of the caller to execute the task
DiscardOldestPolicy: Discard the oldest task in the blocking queue , The top task in the queue
DiscardPolicy : The current task is discarded directly
Implement your own saturation strategy , Realization RejectedExecutionHandler Interface can
Submit tasks
execute(Runnable command) No need to return
Future<T> submit(Callable<T> task) Need to return
Close thread pool
shutdown(),shutdownNow();
shutdownNow(): Set the state of thread pool , It also tries to stop the thread that is running or suspending the task
shutdown() Set the state of thread pool , It will only interrupt all threads that do not execute tasks
Working mechanism ( Interview questions )
Reasonable configuration of thread pool ( Interview questions )
According to the nature of the task : Computationally intensive (CPU),IO intensive , mixed type
Computationally intensive : encryption , Large number decomposition , Regular ……., The number of threads should be a little smaller , Best recommendation : Mechanical Cpu The core number +1, Why? +1, Prevent missing pages ,( Mechanical Cpu The core =Runtime.getRuntime().availableProcessors();)
IO intensive : Read the file , Database connection , Network communication , The number of threads should be a little larger , Mechanical Cpu The core number *2,
mixed type : Try to split ,IO intensive >> Computationally intensive , It doesn't mean much to break up ,IO intensive ~ Computationally intensive
The choice of the queue , Should use bounded , Unbounded queues can cause memory overflows ,OOM
Predefined thread pools
FixedThreadPool
Create a fixed number of threads , It is suitable for servers with heavy load , Unbounded queue is used
SingleThreadExecutor
Create a single thread , It is necessary to ensure the execution of tasks in sequence , There won't be multiple threads active , Unbounded queue is used
CachedThreadPool
New threads will be created as needed , Programs that perform many short-term asynchronous tasks , Used SynchronousQueue
WorkStealingPool(JDK7 in the future )
be based on ForkJoinPool Realization
ScheduledThreadPoolExecutor
Periodic tasks need to be performed on a regular basis ,Timer Not recommended .
newSingleThreadScheduledExecutor: Contains only one thread , Only a single thread is required to perform periodic tasks , Ensure that the tasks are performed in sequence
newScheduledThreadPool Can contain multiple threads , Threads perform periodic tasks , When controlling the number of threads in the background appropriately
边栏推荐
- 【Rust笔记】05-错误处理
- 了解小程序的笔记 2022/7/3
- KunlunBase MeetUP 等您来!
- Clion toolchains are not configured configure disable profile problem solving
- Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
- 数据库原理期末复习
- Osgearth target selection
- Dotween plug-in
- About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
- Classes and objects
猜你喜欢
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
Ue5 opencv plug-in use
VIM learning notes from introduction to silk skating
数据库原理期末复习
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
了解小程序的笔记 2022/7/3
【云原生】微服务之Feign的介绍与使用
matlab神经网络所有传递函数(激活函数)公式详解
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Abstract classes and interfaces
随机推荐
Advanced OSG collision detection
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Kunlunbase meetup is waiting for you!
Classes and objects
About Wireshark's unsuccessful installation of npcap
Introduction to Base64 coding
MySQL containerization (1) docker installation MySQL
【Rust 笔记】09-特型与泛型
Development experience and experience
十六进制编码简介
Cesium service deployment, and import and display local 3dfiles data
redis集群系列四
Gradle's method of dynamically modifying APK package name
Unity change default editor
Animation_ IK overview
Image processing 8-cnn image classification
Golang 字符串分割,替换和截取
Cesium for unreal quick start - simple scenario configuration
【K&R】中文第二版 个人题解 Chapter1
【Rust笔记】05-错误处理