The use of JAVA collection DelayQueue (synchronous timeout queue)

https://www.cnblogs.com/hhan/p/10678466.html
Expand article directories [a] Introduction [two] DelayQueue inheritance hierarchy 1 . The core method the Take () PUT (E) the offer (E) poll () PEEK ( ) size () clear () [3] Use DelayQueue to prepare Delayed implementation class 1. Constructor [1] Introduction An unbounded blocking queue from java api Delayed element can only extract elements from it when the delay expires. The head of the queue is the Delayed element with the longest retention time after the delay expires. If none of the delays have expired, the queue has no head and poll will return null . When an element's getDelay (TimeUnit.NANOSECONDS) method returns a value less than or equal to 0, an expiration occurs. Even if you cannot use take or poll to remove unexpired elements, they will not be treated as normal elements. For example, the size method returns the count of both expired and unexpired elements. Null elements are not allowed in this queue . [2] DelayQueue inheritance system public classDelayQueue <E extends Delayed> extends AbstractQueue <E> implements BlockingQueue <E> 1 1. The core method take () gets and removes the head of this queue, and waits (if any) necessary). put (E) inserts the specified element into this delay queue. offer (E) inserts the specified element into this delay queue. poll () gets and removes the head of this queue, or returns null if this queue does not contain an element with an expired delay time . peek () gets but does not remove the head of this queue; if this queue is empty, it returns null . size () returns the number of elements in this collection. clear () automatically removes all elements of this delay queue. [Three] Use DelayQueue to prepare Delayed implementation class DelayQueue <E extends Delayed>Using this timeout queue, according to his generic restriction type, the element he stores must be a subclass of the Delayed interface; therefore we must first construct such a subclass, assuming there is a scheduled task Task, and then from the set time Take out the queue and execute him. Scheduled Task Task / ** * * Scheduled Task * / public class Task { // task id private Integer id; // task name private String name; // execution time private Long time; public Task (Integer id , String name, Long time) { this .id = id; this .name = name; this .time = time; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getTime() { return time; } public void setTime(Long time) { this.time = time; } @Override publicString toString () { return "Task [id =" + id + ", name =" + name + ", time =" + new Date (time) .toString () + "]" ; } } Delayed implementation class: because We don't want to change the structure of Task, so create another implementation class TaskDelayed public class TaskDelayed implements Delayed { // task private Task task; public TaskDelayed (Task task) { super (); this .task = task; } public Task getTask ( ) { return task; } @Override public int compareTo(Delayed o) { return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); } @Override public long getDelay(TimeUnit unit) { long time = task.getTime(); long currentTime = System.currentTimeMillis(); return unit.convert(time - currentTime, TimeUnit.MILLISECONDS); } } 1. 构造方法 BlockingQueue<TaskDelayed> queue = new DelayQueue<>(); 1 put 与 take (一直阻塞直到完成操作为止) public static void main(String[] args) throws InterruptedException { BlockingQueue<TaskDelayed> queue = new DelayQueue<>(); queue.put(new TaskDelayed(new Task(5, "ssss", System.currentTimeMillis() + 9000L))); queue.put(new TaskDelayed(new Task(2, "ssss", System.currentTimeMillis() + 6000L))); queue.put(new TaskDelayed(new Task(3, "ssss", System.currentTimeMillis() + 7000L))); queue.put(new TaskDelayed(new Task(1, "ssss", System.currentTimeMillis() + 5000L))); queue.put(new TaskDelayed(new Task(4, "ssss", System.currentTimeMillis() + 8000L))); for(;;) { System.out.println(queue.take().getTask() + "----" + new Date()); } } 执行结果 Task [id=1, name=ssss, time=Wed Jan 23 14:09:12 CST 2019]----Wed Jan 23 14:09:12 CST 2019 Task [id=2, name=ssss, time=Wed Jan 23 14:09:13 CST 2019]----Wed Jan 23 14:09:13 CST 2019 Task [id= 3, name = ssss, time = Wed Jan 23 14:09:14 CST 2019] ---- Wed Jan 23 14:09:14 CST 2019 Task [id = 4, name = ssss, time = Wed Jan 23 14 : 09: 15 CST 2019] ---- Wed Jan 23 14:09:15 CST 2019 Task [id = 5, name = ssss, time = Wed Jan 23 14:09:16 CST 2019] ---- Wed Jan 23 14:09:16 CST 2019 The time taken here is the same as the current system time, because we specified the time he took it out. The key method is Delayed public interface Delayed extends Comparable <Delayed> { long getDelay (TimeUnit unit); } Specific implementation: Returns the remaining delay time associated with this object, expressed in the given time unit. @Override public long getDelay (TimeUnit unit) { long time = task.getTime (); long currentTime = System.currentTimeMillis (); return unit.convert (time- currentTime, TimeUnit.MILLISECONDS); } ———————————————— Copyright Statement: This article is a program for CSDN bloggers The original article of " Ape- Jojo" follows the CC 4.0 BY- SA copyright agreement. Please attach the original source link and this statement for reprint. Original link: https: // blog.csdn.net/weixin_39554102/article/details/86608830

 

Guess you like

Origin www.cnblogs.com/kelelipeng/p/12721510.html