In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to carry out ScheduledThreadPoolExecutor analysis and thread pool pit prevention, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Improper use of Java thread pools can lead to many online problems, including:
The thread pool uses unbounded queues by default. When there are too many tasks, JVM OOM
If the maximum number of threads is not set, the number of threads will soar.
Regular execution, the execution of a task takes too long or even blocks all the time, which can not achieve the purpose of regular execution.
The thread in the thread pool is a non-daemon thread, there is a problem when stopping JVM
Using the submit method, no call to future.get () causes the exception to be swallowed, and execute will not have the problem of exception being swallowed.
Deadlock problems are not common, but sometimes complex writing can occur. In a word, when using thread pools and higher-level abstract tools such as blocking queues, try not to use low-level classes, such as lock,wait (), notify (), etc., which can lead to problems that are difficult to troubleshoot.
ScheduledExecutorService analysis
ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool (4); scheduledService.scheduleAtFixedRate (()-> {try {System.out.println ("pool thread:" + Thread.currentThread (). IsDaemon ()); System.out.println ("start at:" + new Date ()); Thread.sleep (10000); System.out.println ("end at:" + new Date ()) } catch (InterruptedException e) {e.printStackTrace ();}, 0,5, TimeUnit.SECONDS)
The above code block is intended to execute a task every 5 seconds, but it takes 10 seconds (sleep) to execute this task, so it certainly won't achieve the effect of 5 seconds. Analyze the reason.
The implementation class of the timed execution function is ScheduledThreadPoolExecutor, which is implemented by a thread pool plus a delay queue, which is implemented using a heap, that is, the maximum or minimum value of the root element. In the scenario of scheduled task execution, the root element is the next task to be executed, and then there is a waiting time.
Public ScheduledFuture scheduleAtFixedRate (Runnable command, long initialDelay, long period, TimeUnit unit) {if (command = = null | | unit = = null) throw new NullPointerException (); if (period
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.