@GenerateMicroBenchmark @Group("Stamped") @GroupThreads(1) public long stWrites() { long stamp; long newValue; stamp = stampedLock.readLock(); try { BlackHole.consumeCPU(consumedCPU); long currentValue = stampedAdder; newValue = currentValue + 1; long writeStamp = stampedLock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; stampedAdder = newValue; } else { stampedLock.unlockRead(stamp); stamp = stampedLock.writeLock(); BlackHole.consumeCPU(consumedCPU); currentValue = stampedAdder; newValue = currentValue + 1; stampedAdder = newValue; } } finally { stampedLock.unlock(stamp); } BlackHole.consumeCPU(consumedCPU); return newValue; }
@Override public void increment() { long stamp = stampedLock.writeLock(); // blocking lock, returns a stamp try { counter++; } finally { stampedLock.unlockWrite(stamp); // release the lock in the same block } }