@Test public void testNext() throws Exception { final AtomicBoolean stopped = new AtomicBoolean(false); final ConcurrentMap<Long, Integer> values = new ConcurrentHashMap<Long, Integer>(); final AtomicLong errors = new AtomicLong(); final AtomicLong totalRuns = new AtomicLong(); Thread[] threads = new Thread[THREADS]; for(int i =0; i < THREADS; i++) { threads[i] = new Thread() { long runs; @Override public void run() { while (!stopped.get()) { Timestamper.next(); ++runs; } totalRuns.addAndGet(runs); } }; } for (Thread thread : threads) { thread.start(); } Thread.sleep(TimeUnit.SECONDS.toMillis(DURATION)); stopped.set(true); long start = System.nanoTime(); for (Thread thread : threads) { thread.join(); } assertThat("Shouldn't wait that long for all threads to join!", TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start), is(0L)); System.out.println(totalRuns.get() / DURATION / THREADS + " tps per thread" ); assertThat(errors.get(), is(0L)); }
public static long next() { int runs = 0; while (true) { long base = SlewClock.timeMillis() << BIN_DIGITS; long maxValue = base + ONE_MS - 1; for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue; current = VALUE.get(), update = Math.max(base, current + 1)) { if (VALUE.compareAndSet(current, update)) { if (runs > 1) { log(base, "Thread spin-waits on time to pass. Looped " + "{} times, you might want to increase -Dnet.sf.ehcache.util.Timestamper.shift", runs); } return update; } } ++runs; } }
Timestamper { public static long next() { int runs = 0; while (true) { long base = SlewClock.timeMillis() << BIN_DIGITS; long maxValue = base + ONE_MS - 1; for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue; current = VALUE.get(), update = Math.max(base, current + 1)) { if (VALUE.compareAndSet(current, update)) { if (runs > 1) { log(base, "Thread spin-waits on time to pass. Looped " + "{} times, you might want to increase -Dnet.sf.ehcache.util.Timestamper.shift", runs); } return update; } } ++runs; } } }
Timestamper { public static long next() { int runs = 0; while (true) { long base = SlewClock.timeMillis() << BIN_DIGITS; long maxValue = base + ONE_MS - 1; for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue; current = VALUE.get(), update = Math.max(base, current + 1)) { if (VALUE.compareAndSet(current, update)) { if (runs > 1) { log(base, "Thread spin-waits on time to pass. Looped " + "{} times, you might want to increase -Dnet.sf.ehcache.util.Timestamper.shift", runs); } return update; } } ++runs; } } private  Timestamper(); }
Timestamper { public static long next() { int runs = 0; while (true) { long base = SlewClock.timeMillis() << BIN_DIGITS; long maxValue = base + ONE_MS - 1; for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue; current = VALUE.get(), update = Math.max(base, current + 1)) { if (VALUE.compareAndSet(current, update)) { if (runs > 1) { log(base, "Thread spin-waits on time to pass. Looped " + "{} times, you might want to increase -Dnet.sf.ehcache.util.Timestamper.shift", runs); } return update; } } ++runs; } } private  Timestamper(); static long next(); }
Timestamper { public static long next() { int runs = 0; while (true) { long base = SlewClock.timeMillis() << BIN_DIGITS; long maxValue = base + ONE_MS - 1; for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue; current = VALUE.get(), update = Math.max(base, current + 1)) { if (VALUE.compareAndSet(current, update)) { if (runs > 1) { log(base, "Thread spin-waits on time to pass. Looped " + "{} times, you might want to increase -Dnet.sf.ehcache.util.Timestamper.shift", runs); } return update; } } ++runs; } } private  Timestamper(); static long next(); static final int BIN_DIGITS; static final int ONE_MS; }