コード例 #1
0
  @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;
  }