Class AtomicIdGenerator

java.lang.Object
cloud.opencode.base.id.simple.AtomicIdGenerator
All Implemented Interfaces:
IdGenerator<Long>

public final class AtomicIdGenerator extends Object implements IdGenerator<Long>
Atomic Increment ID Generator 原子自增ID生成器

Simple thread-safe ID generator using atomic increment. Suitable for single-node or testing scenarios.

使用原子递增的简单线程安全ID生成器。 适用于单节点或测试场景。

Features | 主要功能:

  • Thread-safe atomic increment - 线程安全原子递增
  • Configurable start value - 可配置起始值
  • Reset capability - 重置功能

Usage Examples | 使用示例:

AtomicIdGenerator gen = AtomicIdGenerator.create();
long id1 = gen.generate(); // 1
long id2 = gen.generate(); // 2

// With start value
AtomicIdGenerator gen2 = AtomicIdGenerator.create(1000);
long id = gen2.generate(); // 1000

Performance | 性能特性:

  • ~50M ops/sec single thread - 单线程约50M次/秒
  • ~200M ops/sec with 8 threads - 8线程约200M次/秒

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-id V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static AtomicIdGenerator create()
      Creates a generator starting from 1 创建从1开始的生成器
      Returns:
      generator | 生成器
    • create

      public static AtomicIdGenerator create(long startValue)
      Creates a generator with specific start value 使用指定起始值创建生成器
      Parameters:
      startValue - the start value | 起始值
      Returns:
      generator | 生成器
    • generate

      public Long generate()
      Description copied from interface: IdGenerator
      Generates the next ID 生成下一个ID
      Specified by:
      generate in interface IdGenerator<Long>
      Returns:
      generated ID | 生成的ID
    • getCurrentValue

      public long getCurrentValue()
      Gets the current value without incrementing 获取当前值但不递增
      Returns:
      current value | 当前值
    • reset

      public void reset(long value)
      Resets the counter to a new value 将计数器重置为新值
      Parameters:
      value - the new value | 新值
    • getType

      public String getType()
      Description copied from interface: IdGenerator
      Gets the generator type name 获取生成器类型名称
      Specified by:
      getType in interface IdGenerator<Long>
      Returns:
      type name | 类型名称