ConcurrentLinkedQueue
是基于链表的无界队列,队列中数据遵循FIFO,不允许使用null值。从ConcurrentLinkedQueue
的类继承关系如下图:
可看出其实现了Queue
接口但没有实现BlockingQueue
故ConcurrentLinkedQueue
不是阻塞队列,不能用于线程池中。
主要属性
1 | public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> |
构造器
1 | public ConcurrentLinkedQueue() { |
入队
1 | public boolean add(E e) { |
出队
1 | public E remove() { |
总结
ConcurrentLinkedQueue
使用CAS + spin更新头尾节点来完成出队、入队操作。
特性 | ConcurrentLinkedQueue | LinkedBlockingQueue |
---|---|---|
线程安全 | 是 | 是 |
取元素 | 队列为空获取元素直接返回null | poll()方法可以实现此功能 |
锁 | 无锁 通过CAS+Spin实现 | 使用重入锁 |
效率 | 高 | 较低 |
阻塞 | 非阻塞 | 阻塞 |
是否可在线程池使用 | 否 | 是 |