@Test public void testMultiBits() { final int sequenceBits = 8; final int workerBits = 14; final IdGenerator workers[] = new IdGenerator[10]; for (int i = 0; i < workers.length; i++) { workers[i] = new IdGenerator(i * 100, workerBits, sequenceBits); } final Set<Long> ids = new HashSet<Long>(); for (long i = 0; i < (long) (Math.pow(2, sequenceBits) * 10); i++) { for (int j = 0; j < workers.length; j++) { long id = workers[j].getNextId(); if (ids.contains(id)) { fail(id + " is already generated by the IdGenerator"); } ids.add(workers[j].getNextId()); } } }
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();  }