...
Code Block | ||
---|---|---|
| ||
class InventoryManager { private static final int MIN_INVENTORY = 3; private final AtomicInteger itemsInInventory = new AtomicInteger(100); privatepublic final void removeItem() { for (;;) { int old = itemsInInventory.get(); if (old > MIN_INVENTORY) { int next = old - 1; // Decrement if(itemsInInventory.compareAndSet(old, next)) { break; } } else { throw new IllegalStateException("Under stocked"); } } // end for } // end removeItem() } |
...