# DelayQueue **Repository Path**: zclget/delay-queue ## Basic Information - **Project Name**: DelayQueue - **Description**: 简介: DelayQueue是一个无界阻塞队列,只有在延迟期满时,才能从中提取元素。 队列的头部,是延迟期满后保存时间最长的delay元素。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-25 - **Last Updated**: 2021-06-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: queue ## README # DelayQueue 1. Order实现Delayed类 2. 重写getDelay和compareTo方法 3. new Thread执行 4. 源码解析 ```java public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (;;) { E first = q.peek(); if (first == null) available.await(); else { // 获取自实现的getDelay延迟时间 long delay = first.getDelay(NANOSECONDS); if (delay <= 0) return q.poll(); first = null; // don't retain ref while waiting if (leader != null) available.await(); else { Thread thisThread = Thread.currentThread(); leader = thisThread; try { // 等待 available.awaitNanos(delay); } finally { if (leader == thisThread) leader = null; } } } } } finally { if (leader == null && q.peek() != null) available.signal(); lock.unlock(); } } ```