In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to customize Java multithreading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What does a custom thread do?
Public ThreadPoolExecutor (int corePoolSize
Int maximumPoolSize
Long keepAliveTime
TimeUnit unit
BlockingQueue workQueue
ThreadFactory threadFactory
RejectedExecutionHandler handler) {
}
CorePoolSize: number of core threads
MaximumPoolSize: maximum number of threads
KeepAliveTime + unit: thread recovery time
WorkQueue: temporarily save to the queue when there are many tasks
ThreadFactory: the factory used by the executor to create a new thread
Handler: strategy for rejecting tasks after exceeding thread pool capacity and queue length
How thread pools work:
When a task is submitted, the number of threads in the thread pool can be increased to corePoolSize, and then the submitted task will be temporarily stored in the queue. If the queue is full, we will see if we can continue to increase the number of threads to maximumPoolSize. When the number of threads is exceeded, the rejection policy will be processed. Obviously, if you use unbounded queues, then maximumPoolSize will fail, and there will be at most corePoolSize threads working in the thread pool.
KeepAliveTime + unit
This involves thread pool recycling threads. To put it simply, a bounded queue is used, resulting in corePoolSize full, queue full, and the number of threads in the thread pool growing to maximumPoolSize. After the task is processed, the part of the thread pool with more corePoolSize should be recycled. The question of how long to wait and how long to reclaim after no task is determined by the above parameters.
Fixed thread pool
Features:
Unbounded queue, resulting in keepAliveTime/unit/maximumPoolSize invalidation, no rejection
As the task grows, the number of threads will be fixed at corePoolSize.
Single thread pool
Features:
Unbounded queue + core, maximum number of threads is 1.
To make a simple analogy, the task comes, no matter how many, so orderly, there is only one worker, then deal with the task in order. Is a single-threaded sequential processing of tasks.
Cache thread pool
Features:
A special queue: SynchronousQueue, there is no capacity to speak of, and submitting a task means that the thread that has been blocking the waiting for the task gets the task to execute immediately. To put it bluntly, do not temporarily store in the queue, the task is submitted directly to the thread for execution. Because the task cannot be temporarily saved, the cache thread pool will grow according to the actual situation of the task until maximumPoolSize (Integer.MAX_VALUE).
Notice that keepAliveTime is set to 60s, which means that if a lot of tasks come, the cache thread pool creates a lot of threads to deal with them, and the task is almost done, so after waiting for 60s, there is no task to deal with, then thread recycling.
Timing thread pool
Features:
Use the delay queue DelayedQueue (Unbounded queue) to complete the thread pool to achieve the need for timing and periodic execution of tasks.
Rejection strategy
When the task cache queue of the thread pool is full and the number of threads in the thread pool reaches maximumPoolSize, a task rejection policy will be adopted if any more tasks arrive. There are usually the following four strategies:
ThreadPoolExecutor.AbortPolicy: discard the task and throw
RejectedExecutionException exception.
ThreadPoolExecutor.DiscardPolicy: also discards the task, but does not throw an exception.
ThreadPoolExecutor.DiscardOldestPolicy: discard the first task in the queue, and then
Try to perform the task again (repeat this process)
ThreadPoolExecutor.CallerRunsPolicy: call another thread to handle the task
This is the end of the content of "how to customize Java multithreading". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.