There is such synchronized function:
private int balance;
//...
public synchronized void executeRequest(Request request) {
switch (request.getType()) {
case CREDIT:
if(balance >= request.getAmount()) {
balance -= request.getAmount();
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
} else {
System.out.printf(FAILURE,
request,
Thread.currentThread().getName(),
balance);
}
break;
case REPAYMENT:
balance += request.getAmount();
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
break;
}
}
I need to rewrite it by using AtomicInteger for balance parameter.
I can't come up with how to do two actions atomically:
- balance >= request.getAmount()
- balance -= request.getAmount();
I try to rewrite CREDIT operation like that:
private void credit(int amount, String request) {
int oldValue;
int newValue;
do {
oldValue = balance.get();
newValue = oldValue - amount;
if (oldValue >= amount) {
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
} else {
System.out.printf(FAILURE,
request,
Thread.currentThread().getName(),
balance);
}
} while (!balance.compareAndSet(oldValue, newValue));
}
But it won't work, because there's no guarantee that oldValue >= amount
will be true when we try to compute !balance.compareAndSet(oldValue, newValue)
again.
Have you any idea how to rewrite the first method with AtomicInteger?