DelayQueue源码解析
1. 什么是 DelayQueue?
DelayQueue
是Java并发包 java.util.concurrent
提供的一个无界阻塞队列,元素必须实现 Delayed
接口。它的特点是:
- 元素带有“延迟时间”,只有延迟到期后,元素才能被获取。
- 基于最小堆(优先队列)实现,内部使用
PriorityQueue
来排序。 - 线程安全,适合调度、任务过期处理等场景。
实现类位于
java.util.concurrent.DelayQueue
。
2. 类继承结构
public class DelayQueue<E extends Delayed> extends AbstractQueue<E>implements BlockingQueue<E>
类的继承结构图如下:
继承和实现说明:
-
继承了 AbstractQueue:继承了通用队列行为
-
实现了 BlockingQueue 接口:支持阻塞特性,如