Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: getAndDecrement() vs compareAndSet()

...

It is necessary to use a local variable temp because there is a time-of-check to time-of-use (TOCTOU) condition between checking whether the inventory count is less than Integer.MAX_VALUE and using getAndIncrement() to increment it. Notably, this functionality could also be implemented by using the compareAndSet() method. The getAndIncrement() alternative is useful when control over setting the returned value must lie in the hands of the caller instead of the invoked method (returnItem()).

Compliant Solution (method synchronization)

...