代码如下:
package com.example.base.concurrent; import java.util.Date; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; import com.example.spring.MyLog; public class MyDelayedQueue { public static void main(String[] args) throws InterruptedException { test(); } private static void test() throws InterruptedException { DelayQueue<DelayedTask> queue = new DelayQueue<>(); producer(queue); MyLog.info("start take task from queue"); //循环从队列中取数据 while(!queue.isEmpty()) { //只有到期的数据才能取的出来,否则就阻塞等待 MyLog.info(queue.take()); } } private static void producer(DelayQueue<DelayedTask> queue) { for (int i = 0; i < 10; i++) { queue.put(new DelayedTask((10 - i) * 1000, "delayed taask"+i)); } } public static void info(Object object) { System.out.println(new Date() + " [" + Thread.currentThread().getName() + "] " + object); } private static class DelayedTask implements Delayed { private long delay; private long expire; private String msg; public DelayedTask(long delay, String msg) { this.delay = delay; this.msg = msg; this.expire = System.currentTimeMillis() + delay; } /** * 用于延迟队列内部进行排序,将最先到期的放在队首,保证take出来的是到期的那个 */ @Override public int compareTo(Delayed o) { return (int)(this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); } /** * 指定到期时间计算规则 */ @Override public long getDelay(TimeUnit unit) { return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override public String toString() { return "DelayedTask [delay=" + delay + ", expire=" + expire + ", msg=" + msg + "]"; } } }
执行结果如下:
2019-07-14T11:57:36.440+08:00 INFO [main] [[email protected]:17] -start take task from queue 2019-07-14T11:57:37.186+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=1000, expire=1563076657181, msg=delayed taask9] 2019-07-14T11:57:38.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=2000, expire=1563076658181, msg=delayed taask8] 2019-07-14T11:57:39.181+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=3000, expire=1563076659181, msg=delayed taask7] 2019-07-14T11:57:40.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=4000, expire=1563076660181, msg=delayed taask6] 2019-07-14T11:57:41.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=5000, expire=1563076661181, msg=delayed taask5] 2019-07-14T11:57:42.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=6000, expire=1563076662181, msg=delayed taask4] 2019-07-14T11:57:43.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=7000, expire=1563076663181, msg=delayed taask3] 2019-07-14T11:57:44.181+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=8000, expire=1563076664181, msg=delayed taask2] 2019-07-14T11:57:45.181+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=9000, expire=1563076665180, msg=delayed taask1] 2019-07-14T11:57:46.182+08:00 INFO [main] [[email protected]:19] -DelayedTask [delay=10000, expire=1563076666180, msg=delayed taask0]
原文地址:https://www.cnblogs.com/gc65/p/11183771.html
时间: 2024-11-12 19:07:25