当前位置:网站首页>[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
边栏推荐
- Find the intersection of line segments
- Golang time format sorting
- matlab神经网络所有传递函数(激活函数)公式详解
- 详解sizeof、strlen、指针和数组等组合题
- [cloud native] introduction and use of feign of microservices
- Unity notes 1
- String class
- Introduction to Base64 coding
- Golang string segmentation, substitution and interception
- Some understandings of 3dfiles
猜你喜欢

Notes on understanding applets 2022/7/3

【云原生】微服务之Feign的介绍与使用

VIM learning notes from introduction to silk skating

Get to know unity2 for the first time

单调栈-84. 柱状图中最大的矩形

Chocolate installation

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

Dealing with duplicate data in Excel with xlwings

Vscode, idea, VIM development tool shortcut keys

单调栈-42. 接雨水
随机推荐
LinkList
Cesium service deployment, and import and display local 3dfiles data
梯度下降法求解BP神经网络的简单Demo
ArrayList
Encoding and decoding of golang URL
Clion toolchains are not configured configure disable profile problem solving
Unity Editor Extension - drag and drop
Downward compatibility and upward compatibility
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Go resolve ID card
Exe file running window embedding QT window
Golang 中string和int类型相互转换
Map的实现类的顺序性
单调栈-503. 下一个更大元素 II
Find the intersection of line segments
Three characteristics
Installation of PHP FPM software +openresty cache construction
简易入手《SOM神经网络》的本质与原理
Message pack in C deserializes array objects
【Rust笔记】05-错误处理