Use of Delayqueue in Java: Blocking queue, delay queue

Source: Internet
Author: User

The delayqueue is an unbounded blocking queue that extracts elements only when the delay expires. The head of the queue is the delayed element with the longest save time after the delay expires.

Delayqueue blocking queue is also often used in our system development, for example: the design of the cache system, the object in the cache, more than the idle time, need to move out of the cache, task scheduling system, can accurately grasp the task execution time. We may need to process a lot of time through the thread of strict data requirements, if the use of ordinary threads, we need to traverse all the objects, a check to see if the data is out of date, and so on, first of all, the implementation of the efficiency is not too high, followed by the style of this design has greatly affected the accuracy of the data. A task that needs to be performed at 12:00 may not be executed until 12:01, which has a greater disadvantage for systems with high data requirements. Thus we can use Delayqueue.

In order to have the invocation behavior, the elements stored in the Delaydeque must implement the delayed interface. The delayed interface makes an object a lingering object that has an activation date for an object that resides in the Delayqueue class. The interface enforces the following two methods.

CompareTo (Delayed o): The Delayed interface inherits the comparable interface, so this method is used. Getdelay (timeunit Unit): This method returns the time remaining to the activation date, specified by the unit parameter.

implementing the Delayed interface
This code: The execution time of the contract is compared with the current time, to achieve the point of automatic execution of the contract.

thread starts, Delayqueue starts working

Spring boot load, initialize data source into, and turn on single thread

Leave a record!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.