Code example for AtomicInteger

Methods: addAndGet

0
    public BankAccount(int balance) {
        this.balance.set(balance);
    } 
 
    public void deposit(int amount) {
        balance.addAndGet(amount);
    } 
 
    public void withdraw(int amount) {
        deposit(-amount);
    } 
 
    public boolean transferTo(BankAccount other, int amount) {
        while (true) { 
            int currentBalance = balance.get();
            if (currentBalance < amount) return false;
            int newBalance = currentBalance - amount;
            if (balance.compareAndSet(currentBalance, newBalance)) {
                other.deposit(amount);
                return true; 
            }