@Test public void testId() { RequestIdGenerator.offset.set(0); for (long i = 1L; i < RequestIdGenerator.MAX_COUNT_PER_MILLIS; i++) { long id = RequestIdGenerator.getRequestId(); Assert.assertEquals(i, id & (RequestIdGenerator.MAX_COUNT_PER_MILLIS - 1)); } Assert.assertEquals(1L, RequestIdGenerator.getRequestId() & (RequestIdGenerator.MAX_COUNT_PER_MILLIS - 1)); Assert.assertEquals(2L, RequestIdGenerator.getRequestId() & (RequestIdGenerator.MAX_COUNT_PER_MILLIS - 1)); }
public static long getRequestId() { long currentTime = System.currentTimeMillis(); long count = offset.incrementAndGet(); while(count >= MAX_COUNT_PER_MILLIS){ synchronized (RequestIdGenerator.class){ if(offset.get() >= MAX_COUNT_PER_MILLIS){ offset.set(0); } } count = offset.incrementAndGet(); } return (currentTime << BITS) + count; }
RequestIdGenerator { public static long getRequestId() { long currentTime = System.currentTimeMillis(); long count = offset.incrementAndGet(); while(count >= MAX_COUNT_PER_MILLIS){ synchronized (RequestIdGenerator.class){ if(offset.get() >= MAX_COUNT_PER_MILLIS){ offset.set(0); } } count = offset.incrementAndGet(); } return (currentTime << BITS) + count; } }
RequestIdGenerator { public static long getRequestId() { long currentTime = System.currentTimeMillis(); long count = offset.incrementAndGet(); while(count >= MAX_COUNT_PER_MILLIS){ synchronized (RequestIdGenerator.class){ if(offset.get() >= MAX_COUNT_PER_MILLIS){ offset.set(0); } } count = offset.incrementAndGet(); } return (currentTime << BITS) + count; }  }
RequestIdGenerator { public static long getRequestId() { long currentTime = System.currentTimeMillis(); long count = offset.incrementAndGet(); while(count >= MAX_COUNT_PER_MILLIS){ synchronized (RequestIdGenerator.class){ if(offset.get() >= MAX_COUNT_PER_MILLIS){ offset.set(0); } } count = offset.incrementAndGet(); } return (currentTime << BITS) + count; }  static long getRequestId(); static long getRequestIdFromClient(); }
RequestIdGenerator { public static long getRequestId() { long currentTime = System.currentTimeMillis(); long count = offset.incrementAndGet(); while(count >= MAX_COUNT_PER_MILLIS){ synchronized (RequestIdGenerator.class){ if(offset.get() >= MAX_COUNT_PER_MILLIS){ offset.set(0); } } count = offset.incrementAndGet(); } return (currentTime << BITS) + count; }  static long getRequestId(); static long getRequestIdFromClient();  }