@Test public void testBackwardsClock() { int numIterations = 10000; Set<Long> idSet = new HashSet<Long>(numIterations); TimeProvider tp = new TimeProvider() { private int counter = 0; @Override public long getTimeInMillis() { if ((counter++ % 109) == 0) { return System.currentTimeMillis() - 5; } return System.currentTimeMillis(); } }; IdGenerator idgen = new IdGenerator(0L, tp); for (int i = 0; i < numIterations; ++i) { assertTrue(idSet.add(idgen.getNextId())); } assertEquals(numIterations, idSet.size()); }
public long getNextId() { synchronized (syncObject) { long timestamp = timeProvider.getTimeInMillis(); if (prevTimestamp < timestamp) { sequence = 0L; prevTimestamp = timestamp; return buildKey(timestamp, workerId, sequence); } timestamp = prevTimestamp; sequence = ((sequence + 1) & sequenceBitMask); if (0L == sequence) { timestamp++; log.info("Cheating, advancing timestamp by 1ms. workerId = " + workerId); prevTimestamp = timestamp; } return buildKey(timestamp, workerId, sequence); } }
IdGenerator { public long getNextId() { synchronized (syncObject) { long timestamp = timeProvider.getTimeInMillis(); if (prevTimestamp < timestamp) { sequence = 0L; prevTimestamp = timestamp; return buildKey(timestamp, workerId, sequence); } timestamp = prevTimestamp; sequence = ((sequence + 1) & sequenceBitMask); if (0L == sequence) { timestamp++; log.info("Cheating, advancing timestamp by 1ms. workerId = " + workerId); prevTimestamp = timestamp; } return buildKey(timestamp, workerId, sequence); } } }
IdGenerator { public long getNextId() { synchronized (syncObject) { long timestamp = timeProvider.getTimeInMillis(); if (prevTimestamp < timestamp) { sequence = 0L; prevTimestamp = timestamp; return buildKey(timestamp, workerId, sequence); } timestamp = prevTimestamp; sequence = ((sequence + 1) & sequenceBitMask); if (0L == sequence) { timestamp++; log.info("Cheating, advancing timestamp by 1ms. workerId = " + workerId); prevTimestamp = timestamp; } return buildKey(timestamp, workerId, sequence); } } IdGenerator(final long workerId);  IdGenerator(final long workerId, final TimeProvider timeProvider);  IdGenerator(final long workerId, final int numBitsWorkerId, final int numBitsSequence);  IdGenerator(final long workerId, final TimeProvider timeProvider,
                       final int numBitsWorkerId, final int numBitsSequence); }
IdGenerator { public long getNextId() { synchronized (syncObject) { long timestamp = timeProvider.getTimeInMillis(); if (prevTimestamp < timestamp) { sequence = 0L; prevTimestamp = timestamp; return buildKey(timestamp, workerId, sequence); } timestamp = prevTimestamp; sequence = ((sequence + 1) & sequenceBitMask); if (0L == sequence) { timestamp++; log.info("Cheating, advancing timestamp by 1ms. workerId = " + workerId); prevTimestamp = timestamp; } return buildKey(timestamp, workerId, sequence); } } IdGenerator(final long workerId);  IdGenerator(final long workerId, final TimeProvider timeProvider);  IdGenerator(final long workerId, final int numBitsWorkerId, final int numBitsSequence);  IdGenerator(final long workerId, final TimeProvider timeProvider,
                       final int numBitsWorkerId, final int numBitsSequence); long getNextId(); String getNextIdHex(); long getWorkerId(); }
IdGenerator { public long getNextId() { synchronized (syncObject) { long timestamp = timeProvider.getTimeInMillis(); if (prevTimestamp < timestamp) { sequence = 0L; prevTimestamp = timestamp; return buildKey(timestamp, workerId, sequence); } timestamp = prevTimestamp; sequence = ((sequence + 1) & sequenceBitMask); if (0L == sequence) { timestamp++; log.info("Cheating, advancing timestamp by 1ms. workerId = " + workerId); prevTimestamp = timestamp; } return buildKey(timestamp, workerId, sequence); } } IdGenerator(final long workerId);  IdGenerator(final long workerId, final TimeProvider timeProvider);  IdGenerator(final long workerId, final int numBitsWorkerId, final int numBitsSequence);  IdGenerator(final long workerId, final TimeProvider timeProvider,
                       final int numBitsWorkerId, final int numBitsSequence); long getNextId(); String getNextIdHex(); long getWorkerId();  }