package org.springblade.qinzhesync.utils;
|
|
public class SnowflakeIdGenerator {
|
private final long workerId;
|
private final long datacenterId;
|
private long sequence = 0L;
|
private long lastTimestamp = -1L;
|
|
public SnowflakeIdGenerator(long workerId, long datacenterId) {
|
this.workerId = workerId;
|
this.datacenterId = datacenterId;
|
}
|
|
public synchronized long nextId() {
|
long timestamp = System.currentTimeMillis();
|
|
if (timestamp < lastTimestamp) {
|
throw new RuntimeException("Clock moved backwards!");
|
}
|
|
if (lastTimestamp == timestamp) {
|
sequence = (sequence + 1) & 0xFFF;
|
if (sequence == 0) {
|
timestamp = tilNextMillis(lastTimestamp);
|
}
|
} else {
|
sequence = 0L;
|
}
|
|
lastTimestamp = timestamp;
|
|
return ((timestamp - 1288834974657L) << 22) |
|
(datacenterId << 17) |
|
(workerId << 12) |
|
sequence;
|
}
|
|
private long tilNextMillis(long lastTimestamp) {
|
long timestamp = System.currentTimeMillis();
|
while (timestamp <= lastTimestamp) {
|
timestamp = System.currentTimeMillis();
|
}
|
return timestamp;
|
}
|
}
|