In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "what is Java thread pool rejection strategy". 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!
1. CallerRunsPolicy (caller running policy)
It is generally used in scenarios where failures are not allowed, performance requirements are low, and concurrency is small, because thread pools are not normally closed, that is, submitted tasks must be run, but since the tasks are executed by the caller thread itself, when tasks are submitted multiple times, subsequent task execution will be blocked, and performance and efficiency will naturally slow down. When a deny policy is triggered, it is handled by the current thread that submits the task as long as the thread pool is not closed.
2. AbortPolicy (abort strategy)
When a reject policy is triggered, it directly throws an exception that rejects execution, and the abort policy directly interrupts the currently executed process. It has no special usage scenario, but one thing is to handle the thrown exception correctly. The default policy in ThreadPoolExecutor is the series ThreadPoolExecutor of the AbortPolicy,ExecutorService interface, because there is no rejection policy displayed, so the default is this. Note, however, that the thread pool instance queues in ExecutorService are unbounded, meaning that bursting memory does not trigger a reject policy. When you customize a thread pool instance, you must use this policy to handle the exception that is thrown when the policy is triggered, because it will interrupt the current execution process.
3. DiscardPolicy (discard policy)
If the task you submit is irrelevant, you can use it. Because it is an empty realization, will quietly engulf your task. It just discards the task directly and quietly without triggering any action. So this strategy is basically out of use.
4. DiscardOldestPolicy (abandoning the old strategy)
This strategy still discards tasks and discards them silently, but it discards old unexecuted tasks and tasks with a higher priority to be executed. Based on this feature, the only scenario I can think of is to publish the message and modify the message. When the message is released and has not been executed, the updated message comes again, and the version of the unexecuted message is lower than it is now and can be discarded. Because there may also be messages with older versions in the queue that will be queued for execution, be sure to make a good message version comparison when actually processing the message. This rejection strategy is a rejection strategy that likes the new and hates the old. Whether to adopt this rejection strategy or not has to be carefully measured according to whether the actual business allows old tasks to be discarded.
The rejection strategy realized by the third party
Thread rejection Strategy in dubbo
As you can see, when the worker thread of dubbo triggers the thread reject, it mainly does three things, and the principle is to let the user know the real reason for triggering the thread rejection policy as much as possible.
A warning-level log is output with detailed setting parameters for the thread pool, as well as the current state of the thread pool, as well as some details about the current deny task. It can be said that this log is more or less seen by those who have experience in production, operation and maintenance using dubbo. This log is simply a model of log printing, and other examples of log printing are spring. Thanks to such a detailed log, it is easy to locate the problem.
Thread Pool rejection Policy in Netty
The implementation in Netty is very similar to CallerRunsPolicy in JDK. The difference is that the caller runs the policy as a task that is executed directly on the caller thread. Netty has created a new thread to process it. Therefore, the use of Netty can be extended to support high-efficiency and high-performance scenarios. Note, however, that in the implementation of Netty, no judgment constraints are made when the thread is created.
Thread Pool rejection Policy in pinpoint
The implementation of pinpoint's rejection strategy is very distinctive and different. He defines a reject policy chain and wraps a list of reject policies. When a reject policy is triggered, the rejectedExecution in the policy chain is executed in turn.
This is the end of the content of "what is the Java thread pool rejection strategy". 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.