@Test public void testNextId() throws Exception { Thread thread1 = new Thread(new Task()); thread1.start(); Thread thread2 = new Thread(new Task()); thread2.start(); thread1.join(); thread2.join(); }
public long nextId() { if (!nodeIdentityProvider.isStarted()) { throw new IllegalStateException("Start id-generator is necessary before generating ids"); } 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 - startEpochMs) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; }
SnowFlakeIdGenerator { public long nextId() { if (!nodeIdentityProvider.isStarted()) { throw new IllegalStateException("Start id-generator is necessary before generating ids"); } 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 - startEpochMs) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } }
SnowFlakeIdGenerator { public long nextId() { if (!nodeIdentityProvider.isStarted()) { throw new IllegalStateException("Start id-generator is necessary before generating ids"); } 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 - startEpochMs) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider);  SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider, long startEpochMs); }
SnowFlakeIdGenerator { public long nextId() { if (!nodeIdentityProvider.isStarted()) { throw new IllegalStateException("Start id-generator is necessary before generating ids"); } 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 - startEpochMs) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider);  SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider, long startEpochMs); void start(); void stop(); long nextId(); }
SnowFlakeIdGenerator { public long nextId() { if (!nodeIdentityProvider.isStarted()) { throw new IllegalStateException("Start id-generator is necessary before generating ids"); } 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 - startEpochMs) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence; } SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider);  SnowFlakeIdGenerator(NodeIdentityProvider nodeIdentityProvider, long startEpochMs); void start(); void stop(); long nextId();  }