延迟队列
与时间相关场景的应用,经常用于延后多少时间执行什么任务。
java 自带延迟队列
class Solution { public static void main(String[] args) throws InterruptedException { DelayQueue<DelayMealTask> queue = new DelayQueue<>(); DelayMealTask task = new DelayMealTask(System.nanoTime() + ThreadLocalRandom.current().nextLong(100000000L, 300000000L)); queue.add(task); StopWatch stopWatch = new StopWatch(); stopWatch.start(); System.out.println("begin to take task"); DelayMealTask take = queue.take(); System.out.println("get task complete id :" + take.getTaskId()); stopWatch.stop(); System.out.println("cost time : " + stopWatch.getTotalTimeMillis()); } } // output ~ //begin to take task //get task complete id :1 //cost time : 110