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>

public final class LockFreeQueue<E> extends AbstractQueue<E> implements Serializable
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 Details

    • create

      public static <E> LockFreeQueue<E> create()
      Create an empty LockFreeQueue. 创建空 LockFreeQueue。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new empty LockFreeQueue | 新空 LockFreeQueue
    • create

      public static <E> LockFreeQueue<E> create(Collection<? extends E> collection)
      Create a LockFreeQueue from collection. 从集合创建 LockFreeQueue。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      collection - the collection | 集合
      Returns:
      new LockFreeQueue | 新 LockFreeQueue
    • offer

      public boolean offer(E e)
      Specified by:
      offer in interface Queue<E>
    • poll

      public E poll()
      Specified by:
      poll in interface Queue<E>
    • peek

      public E peek()
      Specified by:
      peek in interface Queue<E>
    • 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 calling size() 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, prefer isEmpty() 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:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Overrides:
      clear in class AbstractQueue<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Overrides:
      contains in class AbstractCollection<E>