package com.ruoyi.project.common; public class IdGeneratorUtils { // 时间戳左移的位数(22位给计数器) private static final long TIMESTAMP_SHIFT = 22; private static final long COUNTER_BITS = 22L; private static final long MAX_COUNTER = (1L << COUNTER_BITS) - 1; private static long lastTimestamp = -1L; private static long counter = 0L; public synchronized static long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new RuntimeException("Clock moved backwards"); } if (timestamp == lastTimestamp) { counter = (counter + 1) & MAX_COUNTER; if (counter == 0) { // 同一毫秒内计数器用完,等待下一毫秒 timestamp = waitNextMillis(lastTimestamp); } } else { counter = 0L; } lastTimestamp = timestamp; return ((timestamp) << TIMESTAMP_SHIFT) | counter; } private static long waitNextMillis(long lastTimestamp) { long timestamp = System.currentTimeMillis(); while (timestamp <= lastTimestamp) { timestamp = System.currentTimeMillis(); } return timestamp; } }