In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is the key DelayQueue delay queue for the implementation of timing tasks? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Today's learning is a delay queue (DelayQueue) provided by concurrent packages.
Delay queue description
The function provided by the delay queue is to obtain the function of the queue element at a specified point in time, and the first element of the queue is the first element to be executed.
Enumerating the usage scenarios may be easier to understand, such as the design of the cache system, the objects in the cache specify the expiration time, and the expiration time needs to be removed from the cache; for example, in the task scheduling system, execute the task exactly at the time specified by the task. In these scenarios, if we don't use delay queues, we have to traverse all caches and tasks differently and then determine whether we need to remove the cache and execute the task.
On the other hand, the delay queue does not need to scan the cache and tasks constantly, it can be implemented to execute the task at the exact point in time.
Next, let's sort out the implementation of just-in-time cache removal. first of all, we know the expiration time of each cache, so we can calculate the timestamp of each cache expiration. We first put it in the priority queue (the priority queue introduced in the previous article) according to the expiration timestamp, and then get the cache from the priority queue. We must get the cache that needs to expire first. Determine whether the cache has expired. If not, block the thread (the difference between the expiration timestamp and the current timestamp). After a certain period of time, the thread wakes up automatically. Once again, it is verified that the cache has just expired and the cache can be removed.
Similarly, the punctual execution of scheduled tasks is the same, except that the timestamp of the cache expiration is replaced with the next execution timestamp of the scheduled task as a basis for comparison.
Through the analysis, it is found that a priority queue is needed to achieve this function, and the saved element should specify the timestamp of the queue to be removed.
Introduction to DelayQueue attribute
From the analysis in the previous step, it is concluded that the delay queue must have the function of priority queue, and the saved elements must have a certain time to remove the queue. Let's take a look at the specific implementation of DelayQueue. The basic attribute source code is shown below:
You can see that it uses priority queue Q to store data, so it has all the functions of priority queue.
Then look at all the elements it can store must inherit Delayed, look at the Delayed source code and find that it inherits the Comparable interface and declares the method "long getDelay (TimeUnit unit);". The description of this method translates as: returns the remaining delay associated with this object in a given unit of time. It can directly calculate the remaining delay of the object, just like the remaining time of the cache, it can execute to get the thread blocking time of the object.
Then the object inheriting to Delayed has both the Comparable implementation required by the priority queue and the remaining delayed execution time of the object.
Key implementation of DelayQueue
Take a look at the implementation of take method directly. The source code is shown below:
As long as the previous article understood the priority queue, it's easy to look at the code for the delay queue. First, the priority queue is used to get the element, and then the getDelay (declared by the Delayed interface) method is called to determine whether or not to block and the blocking time.
You can see that if the first thread comes in, if the node is found to be null, it will block for unlimited blocking time, and if the later thread finds that the leader is not null, it will also block directly, and the later thread can wake up after execution by the leader thread. When will the leader thread wake up?
The reason for unlimited blocking of leader is that there is no data in the queue, so waking up must be the place where the data is added. After the offer method saves the element successfully, it will verify whether the element just saved in front of the queue. If so, it will call available.signal (); wake up the thread, the code is relatively simple and will not post it.
Delay queuing Best practic
When it comes to the best practice of delay queue, we need to talk about the previously submitted timed task. The thread pool for analyzing timed task ScheduledThreadPoolExecutor mentioned earlier, so let's directly look at the implementation in ScheduledThreadPoolExecutor. The key source code is shown below:
Each task will set the next execution time time before it is placed in the task queue. For example, the above figure implements the getDelay and compareTo methods through time, so that multiple scheduled tasks can be saved in a thread pool. After execution, each task will reset time and continue to be placed in the priority queue of the thread pool. It is so simple to implement scheduled tasks.
This is the answer to the question about what is the key DelayQueue delay queue for the implementation of scheduled tasks. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.