Class LockFreeQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
cloud.opencode.base.collections.concurrent.LockFreeQueue<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>
LockFreeQueue - Lock-free Queue Implementation
LockFreeQueue - 无锁队列实现
A lock-free queue implementation using atomic operations.
使用原子操作的无锁队列实现。
Features | 主要功能:
- Lock-free operations - 无锁操作
- Thread-safe - 线程安全
- Non-blocking - 非阻塞
- Wait-free for single producer - 单生产者无等待
Usage Examples | 使用示例:
LockFreeQueue<String> queue = LockFreeQueue.create();
// Offer (enqueue) - 入队
queue.offer("item1");
queue.offer("item2");
// Poll (dequeue) - 出队
String item = queue.poll(); // "item1"
// Can be used from multiple threads - 可从多线程使用
Performance | 性能特性:
- offer: O(1) - offer: O(1)
- poll: O(1) - poll: O(1)
- peek: O(1) - peek: O(1)
Security | 安全性:
- Thread-safe: Yes (lock-free) - 线程安全: 是(无锁)
- Null-safe: No (nulls not allowed) - 空值安全: 否(不允许空值)
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleanstatic <E> LockFreeQueue<E> create()Create an empty LockFreeQueue.static <E> LockFreeQueue<E> create(Collection<? extends E> collection) Create a LockFreeQueue from collection.booleanisEmpty()iterator()booleanpeek()poll()intsize()Returns the current size of the queue.Methods inherited from class AbstractQueue
add, addAll, element, removeMethods inherited from class AbstractCollection
containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Method Details
-
create
Create an empty LockFreeQueue. 创建空 LockFreeQueue。- Type Parameters:
E- element type | 元素类型- Returns:
- new empty LockFreeQueue | 新空 LockFreeQueue
-
create
Create a LockFreeQueue from collection. 从集合创建 LockFreeQueue。- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- new LockFreeQueue | 新 LockFreeQueue
-
offer
-
poll
-
peek
-
size
public int size()Returns the current size of the queue.V1.0.4 sec round-7 P1 — performance note: this is an O(n) operation that traverses the entire linked list, NOT the
返回队列当前大小。O(1)the class-level Javadoc previously implied. Avoid callingsize()in hot paths or on every iteration of a loop — under high concurrency that produces O(n²) work patterns. For "is the queue empty" checks, preferisEmpty()which is genuinely O(1).V1.0.4 sec round-7 P1 性能说明:此操作 O(n), 遍历整个链表,不是类级 Javadoc 此前暗示的
O(1)。 避免在热路径或循环每次迭代调用size()—— 高并发下产生 O(n²) 工作模式。 检查"队列是否为空"请用isEmpty()(真正的 O(1))。- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
iterator
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>
-