延遲隊列
與時間相關場景的應用,經常用于延后多少時間執行什么任務。
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