In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the multithreaded scheduler in the Java thread pool architecture is. It is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
If we want to use the default thread pool of java as the scheduler, one option is the combination of Timer and TimerTask. It is clearly stated in the previous article: "the Real principle of Timer and TimerTask & introduction to use": a Timer is a single thread. Although a Timer can schedule multiple TimerTask, it is serial for a Timer. For details, please refer to the corresponding article, the multi-thread scheduler introduced in this article. That is, timing tasks are completed based on multithreading, of course you can use multiple Timer to complete multithreading, but the management of these Timer requires you to complete, not a framework, and ScheduleThreadPoolExecutor provides this function, so the first thing we need to figure out is how to use the scheduler, and secondly, we need to know what its internal principle is, that is, know it, and then know why it is!
First of all, if we want to create a scheduling pool based on java itself, the usual way is:
Executors.newScheduledThreadPool (int); when there are overloaded methods, what we use most is this. Let's take a look at the definition: public static ScheduledExecutorService newScheduledThreadPool (int corePoolSize) {return new ScheduledThreadPoolExecutor (corePoolSize);}
In fact, inside new, an instantiated object comes out and the size is passed, which is tracked to the constructor of ScheduledThreadPoolExecutor:
Public ScheduledThreadPoolExecutor (int corePoolSize) {super (corePoolSize, Integer.MAX_VALUE, 0Med TimeUnit. NANOSECONDS, new DelayedWorkQueue ());}
You will find that super is called, and if you track super, you will find that it is in ThreadPoolExecutor. So what's the difference between ScheduledThreadPoolExecutor and ThreadPoolExecutor? that's the point of this article. First of all, we leave an introduction. You find that when defining a queue, it is no longer the LinkedBlockingQueue mentioned above, but DelayedWorkQueue. Then the next point we will explain in detail is that since they inherit the relationship and actually understand the differences, they understand what they have in common. And most of such relationships should have something in common, different guesses: this is to achieve task scheduling, task scheduling is not immediate, need to be delayed and done on a regular basis, so how is it achieved?
This is what we need to think about. Through the review of the source code, we find that they all have execute methods, but ScheduledThreadPoolExecutor rewrites the source code, and there are the following four scheduler methods:
Public ScheduledFuture schedule (Runnable command, long delay, TimeUnit unit); public ScheduledFuture schedule (Callable callable, long delay, TimeUnit unit); public ScheduledFuture scheduleAtFixedRate (Runnable command, long initialDelay, long period, TimeUnit unit) Public ScheduledFuture scheduleWithFixedDelay (Runnable command, long initialDelay, long delay, TimeUnit unit)
So what's the difference between these four methods? In fact, there is little difference between the first and the second, one is Runnable and the other is Callable, and the effect is the same after internal packaging. Therefore, if the first two methods are almost regarded as a kind of scheduling, the three cases are:
1. Perform a delay schedule: delay delay for such a long time, in units of: a basic unit passed in by TimeUnit, for example: TimeUnit.SECONDS belongs to providing good enumeration information; (suitable for method 1 and method 2).
2. Schedule multiple times, each time according to the last expected scheduling time. For example, if the delay starts in 2s and once in 5s, then it is 2, 7, 12 and 17. If the thread is insufficient for some reason and does not get the scheduling opportunity, then the next calculated time will be counted first, because its ranking will be ranked first, which is somewhat similar to the scheduleAtFixedRate method in Timer. It's just that this is multithreaded, and its method is also called scheduleAtFixedRate, so this is easier to remember (suitable for method 3).
3. Schedule multiple times, and calculate the next time according to the time of the last actual execution. As above, if the schedule is not scheduled in the 7th second but is scheduled in the 9th second, then the next scheduling time will not be 12 seconds, but 9 seconds 5 seconds 14 seconds. If it is delayed again, it will be delayed by more than one cycle, and there will be fewer calls (suitable for method 3).
4. Finally, the supplementary execute method is a schedule, which is expected to be scheduled immediately, and the time is empty:
Public void execute (Runnable command) {if (command = = null) throw new NullPointerException (); schedule (command, 0, TimeUnit.NANOSECONDS);}
A brief look at scheduleAtFixedRate and scheduleWithFixedDelay will be more useful for the following analysis:
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.