In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java thread pool satiety, rejection strategy and exception handling mechanism case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java thread pool satiety, rejection strategy and exception handling mechanism case analysis"!
Public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {...} thread saturation strategy
1. When will it be saturated?
When the core thread corePoolSize is full and the blocking queue is full, it will determine whether the current number of threads is less than the maximum number of threads, and decide whether to create new threads. If the total number of threads created is greater than maximumPoolSize, RejectedExecetionHandler will be triggered.
two。 What are the saturation strategies?
JDK mainly provides four saturation strategies to choose from. All four strategies are implemented in ThreadPoolExcutor as static inner classes.
AbortPolicy abort policy, DiscardPolicy discard policy, DiscardOldestPolicy discard old task policy, CallerRunsPolicy caller run
ThreadPoolExecutor.AbortPolicy: discards the task and throws a 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 execute the task again (repeat this process)
ThreadPoolExecutor.CallerRunsPolicy: the task is handled by the calling thread
Four thread pools encapsulated by 3.JUC
NewCachedThreadPool 、 newFixedThreadPool 、 newScheduledThreadPool 、 newSingleThreadExecutor
Thread exception handling
Let's start with the conclusion:
1. The logic in the run function in runnable must be try...catch
/ / if submit is used, if try catch is not added here, the exception will be "eaten" by PULL_TASK_EXECUTOR.execute (new Runnable () {@ Override public void run () {try {} catch (Exception e) {}).
It means that the biggest difference between the two is exception handling. In execute, if you don't implement a handler, then he uses the default handler to handle the exception. If you implement a handler,
He will use the instantiated handler, but for submit, the exception is bound to Future, but when future.get () is called, these exceptions will be thrown to you, which means that you define the handler
In fact, it is ineffective.
Public static void main (String [] args) {System.out.println ("start"); try {Executors.ASYNC_SERVICE_EXECUTOR.submit (()-> {System.out.println ("task start..."); Object obj = null; String str= obj.toString (); System.out.println ("str=") }); System.out.println ("end");} catch (Exception e) {log.error (e.getMessage ());}}
two。 The thread directly overrides the entire method
/ / in threaded mode Thread t = new Thread (); t.setUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler () {public void uncaughtException (Thread t, Throwable e) {LOGGER.error (t + "throws exception:" + e);}}) / / if it is thread pool mode: ExecutorService threadPool = Executors.newFixedThreadPool (1, r-> {Thread t = new Thread (r); t.setUncaughtExceptionHandler ((T1, e)-> LOGGER.error (T1 + "throws exception:" + e)); return t;}) Thank you for reading, the above is the content of "Java thread pool satiety, rejection strategy and exception handling mechanism case analysis". After the study of this article, I believe you have a deeper understanding of Java thread pool satiety, rejection strategy and exception handling mechanism case analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.