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!