...
Code Block | ||
---|---|---|
| ||
class InventoryManager {
private static final int MIN_INVENTORY = 3;
private int itemsInInventory = 100;
private final Lock lock = new ReentrantLock();
public final synchronized void removeItem() {
if(lock.tryLock()) {
try {
if (itemsInInventory <= MIN_INVENTORY) {
throw new IllegalStateException("Under stocked");
}
itemsInInventory--;
} finally {
lock.unlock();
}
}
} // end removeItem()
}
|
...