DelayQueue is the implementation class of the BlockingQueue interface that determines the processing priority of the elements in the queue based on “latency” (that is, sorting the queue elements by their “latency”). Another implication is that only those elements that exceed the “delay time” can be removed from the queue for processing.

  • The DelayQueue queue will prevent its element object from being fetched from the queue until the delay time set for the element object is reached. DelayQueue stores the most recently expired elements at the head of the queue. If there are no expired elements in the queue, retrieving the elements in the queue using the poll() method returns NULL.
  • The DelayQueue class and its iterator are implementedCollectionandIteratorAll optional methods of the interface, but iterator methodsiterator()There is no guarantee that the elements of the DelayQueue will be traversed in a particular order.

  • DelayQueue don’t accept null elements, DelayQueue only accept that implement the Java. Util. Concurrent. The interface of object, and put it in the queue. DelayQueue gets the remaining “delay time” of the element by calling the element object’s getDelay(TimeUnit) method. The TimeUnit of getDelay() is an enumerated type: DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS
public interface Delayed extends Comparable{
   long getDelay(TimeUnit unit);
}
Copy the code

Let’s write a Java Class that implements the Delayed interface, and only those classes that implement the Delayed interface can be added to the DelayQueue. Because the Delayed interface inherits from the Comparable interface, we must implement the getDelay method and the compareTo method.

class DelayObject implements Delayed { private String name; private long time; Public DelayObject(String name, long delayTime) {this.name = name; this.time = System.currentTimeMillis() + delayTime; } @Override public long getDelay(TimeUnit unit) { long diff = time - System.currentTimeMillis(); return unit.convert(diff, TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed obj) { if (this.time < ((DelayObject)obj).time) { return -1; } if (this.time > ((DelayObject)obj).time) { return 1; } return 0; } @Override public String toString() { Date date = new Date(time); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return "\nDelayObject:{" + "name=" + name + ", time=" + sd.format(date) + "}"; }}Copy the code

Test the effect of DelayQueue

Public class DelayQueueTest {@test void testDelayObject() throws InterruptedException {// Instantiate a DelayQueue BlockingQueue<DelayObject> DQ = new DelayQueue<>(); Dq. add(new DelayObject("A", 1000 * 10)); Dq. add(new DelayObject("B", 4000 * 10)); Dq. add(new DelayObject("C", 3000 * 10)); Dq. add(new DelayObject("D", 2000 * 10)); SimpleDateFormat sd = new SimpleDateFormat(" YYYY-MM-DD HH: MM :ss"); SimpleDateFormat sd = new SimpleDateFormat(" YYYY-MM-DD HH: MM :ss"); System.out.println(dq.take ())); A system.out.println (dq.take ()); D system.out.println (dq.take ()); C system.out.println (dq.take ()); // select * from B}}Copy the code

This can be seen from the printout below and the code above

  • The elements in the queue are put in order A, B, C, D, and taken out in order A, D, C, and B, because the elements in the queue are sorted by delay time.
  • In addition, we can see that an element can only be removed from the queue every 10 seconds, because only elements that exceed the “delay time” can be removed from the queue. And we set the delay time is 10s, 20s, 30s, 40s.
DelayObject:{name=A, time=2021-03-23 14:14:20}

DelayObject:{name=D, time=2021-03-23 14:14:30}

DelayObject:{name=C, time=2021-03-23 14:14:40}

DelayObject:{name=B, time=2021-03-23 14:14:50}
Copy the code

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series