@Override public long getResult() { long stamp = stampedLock.tryOptimisticRead(); // non blocking long result = counter; if (!stampedLock.validate(stamp)) { // if a write occurred, try again with a read lock stamp = stampedLock.readLock(); try { return counter; } finally { stampedLock.unlockRead(stamp); } } return result; }
@GenerateMicroBenchmark @Group("Stamped") @GroupThreads(1) public long stReads() { long stamp; long currentValue; stamp = stampedLock.tryOptimisticRead(); BlackHole.consumeCPU(consumedCPU); currentValue = stampedAdder; if (!stampedLock.validate(stamp)) { stamp = stampedLock.readLock(); try { BlackHole.consumeCPU(consumedCPU); currentValue = stampedAdder; } finally { stampedLock.unlockRead(stamp); } } return currentValue; }