mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Snowflake 循环等待下一个时间时加入对时钟倒退的判断
Snowflake 在循环等待下一个时间时,判断时间未发生变化的方式由<=改为==,避免了时钟突然倒退导致的长时间循环,同时加入时钟倒退检测,避免生成重复ID
This commit is contained in:
parent
9440512d54
commit
a3eca85e12
@ -177,9 +177,15 @@ public class Snowflake implements Serializable {
|
||||
*/
|
||||
private long tilNextMillis(long lastTimestamp) {
|
||||
long timestamp = genTime();
|
||||
while (timestamp <= lastTimestamp) {
|
||||
// 循环直到操作系统时间戳变化
|
||||
while (timestamp == lastTimestamp) {
|
||||
timestamp = genTime();
|
||||
}
|
||||
if (timestamp < lastTimestamp) {
|
||||
// 如果发现新的时间戳比上次记录的时间戳数值小,说明操作系统时间发生了倒退,报错
|
||||
throw new IllegalStateException(
|
||||
StrUtil.format("Clock moved backwards. Refusing to generate id for {}ms", lastTimestamp - timestamp));
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user