mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge pull request #874 from jptx1234/v5-dev
[bug修复] Snowflake 循环等待下一个时间时避免长时间循环,加入对时钟倒退的判断
This commit is contained in:
@@ -177,9 +177,15 @@ public class Snowflake implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private long tilNextMillis(long lastTimestamp) {
|
private long tilNextMillis(long lastTimestamp) {
|
||||||
long timestamp = genTime();
|
long timestamp = genTime();
|
||||||
while (timestamp <= lastTimestamp) {
|
// 循环直到操作系统时间戳变化
|
||||||
|
while (timestamp == lastTimestamp) {
|
||||||
timestamp = genTime();
|
timestamp = genTime();
|
||||||
}
|
}
|
||||||
|
if (timestamp < lastTimestamp) {
|
||||||
|
// 如果发现新的时间戳比上次记录的时间戳数值小,说明操作系统时间发生了倒退,报错
|
||||||
|
throw new IllegalStateException(
|
||||||
|
StrUtil.format("Clock moved backwards. Refusing to generate id for {}ms", lastTimestamp - timestamp));
|
||||||
|
}
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user