@Test public void testNextId(){ SnowFlakeIdGenerator idGenerator = new SnowFlakeIdGenerator(1L,2L,0L); for (int i = 0; i < 100; i++) { long l = idGenerator.nextId(); System.out.println(l); } }
public long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = blockUtilNextMillis(); } } else { sequence = 0; } lastTimestamp = timestamp; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; }
SnowFlakeIdGenerator { public long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = blockUtilNextMillis(); } } else { sequence = 0; } lastTimestamp = timestamp; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } }
SnowFlakeIdGenerator { public long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = blockUtilNextMillis(); } } else { sequence = 0; } lastTimestamp = timestamp; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(long workerId, long dataCenterId, long sequence); }
SnowFlakeIdGenerator { public long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = blockUtilNextMillis(); } } else { sequence = 0; } lastTimestamp = timestamp; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(long workerId, long dataCenterId, long sequence); long nextId(); }
SnowFlakeIdGenerator { public long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = blockUtilNextMillis(); } } else { sequence = 0; } lastTimestamp = timestamp; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(long workerId, long dataCenterId, long sequence); long nextId();  }