@Test (timeout=500) public void testMicroBenchmark() { IdGenerator idgen = new IdGenerator(0L); int numIterations = 10000; long start = System.currentTimeMillis(); for (int i = 0; i < numIterations; ++i) { idgen.getNextId(); } long duration = System.currentTimeMillis() - start; log.info("Microbenchmark: iterations = " + numIterations + ", time = " + duration + "ms"); }
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();  }