Class Wait

java.lang.Object
cloud.opencode.base.id.snowflake.Wait
All Implemented Interfaces:
ClockBackwardStrategy

public final class Wait extends Object implements ClockBackwardStrategy
Wait Strategy for Clock Backward Handling 时钟回拨等待策略

Waits for the clock to catch up when clock backward is detected. If the wait time exceeds the maximum allowed, throws an exception.

检测到时钟回拨时等待时钟追上。如果等待时间超过最大允许值,则抛出异常。

Features | 主要功能:

  • Configurable wait timeout - 可配置等待超时
  • Graceful handling for small backward - 小幅回拨优雅处理
  • Fail-fast for large backward - 大幅回拨快速失败

Usage Examples | 使用示例:

// Wait up to 5 seconds
Wait wait = Wait.ofSeconds(5);

// Wait up to 100 milliseconds
Wait waitMs = Wait.ofMillis(100);

Security | 安全性:

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

    Nested classes/interfaces inherited from interface ClockBackwardStrategy

    ClockBackwardStrategy.Custom
  • Constructor Summary

    Constructors
    Constructor
    Description
    Wait(long maxWaitMillis)
    Creates a wait strategy with maximum wait time 使用最大等待时间创建等待策略
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    handle(long lastTimestamp, long currentTimestamp)
    Handles clock backward movement 处理时钟回拨
    long
    Gets the maximum wait time in milliseconds 获取最大等待时间(毫秒)
    static Wait
    ofMillis(long millis)
    Creates a wait strategy with milliseconds timeout 使用毫秒数超时创建等待策略
    static Wait
    ofSeconds(int seconds)
    Creates a wait strategy with seconds timeout 使用秒数超时创建等待策略
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Wait

      public Wait(long maxWaitMillis)
      Creates a wait strategy with maximum wait time 使用最大等待时间创建等待策略
      Parameters:
      maxWaitMillis - the maximum wait time in milliseconds | 最大等待时间(毫秒)
  • Method Details

    • ofSeconds

      public static Wait ofSeconds(int seconds)
      Creates a wait strategy with seconds timeout 使用秒数超时创建等待策略
      Parameters:
      seconds - the timeout in seconds | 超时秒数
      Returns:
      wait strategy | 等待策略
    • ofMillis

      public static Wait ofMillis(long millis)
      Creates a wait strategy with milliseconds timeout 使用毫秒数超时创建等待策略
      Parameters:
      millis - the timeout in milliseconds | 超时毫秒数
      Returns:
      wait strategy | 等待策略
    • maxWaitMillis

      public long maxWaitMillis()
      Gets the maximum wait time in milliseconds 获取最大等待时间(毫秒)
      Returns:
      maximum wait time | 最大等待时间
    • handle

      public long handle(long lastTimestamp, long currentTimestamp)
      Description copied from interface: ClockBackwardStrategy
      Handles clock backward movement 处理时钟回拨
      Specified by:
      handle in interface ClockBackwardStrategy
      Parameters:
      lastTimestamp - the last timestamp used | 上次使用的时间戳
      currentTimestamp - the current (backward) timestamp | 当前(回拨的)时间戳
      Returns:
      the timestamp to use | 要使用的时间戳
    • toString

      public String toString()
      Overrides:
      toString in class Object