complete
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package dev.banking;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AtomicDemo {
|
||||
private static int normalCounter = 0;
|
||||
private static final AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
int threads = 10;
|
||||
int increments = 1000;
|
||||
|
||||
Thread[] threadList = new Thread[threads * 2];
|
||||
|
||||
for (int i = 0; i < threads; i++) {
|
||||
threadList[i * 2] = new Thread(() -> {
|
||||
for (int j = 0; j < increments; j++) {
|
||||
normalCounter++;
|
||||
}
|
||||
});
|
||||
|
||||
threadList[i * 2 + 1] = new Thread(() -> {
|
||||
for (int j = 0; j < increments; j++) {
|
||||
atomicCounter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
|
||||
threadList[i * 2].start();
|
||||
threadList[i * 2 + 1].start();
|
||||
}
|
||||
|
||||
for (Thread t : threadList) {
|
||||
t.join();
|
||||
}
|
||||
int expected = threads * increments;
|
||||
System.out.println("Expected value: " + expected);
|
||||
System.out.println("Normal int: " + normalCounter);
|
||||
System.out.println("AtomicInteger: " + atomicCounter.get());
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,6 @@ public class BankAccount {
|
||||
private final int accountId;
|
||||
private long balance;
|
||||
|
||||
/*
|
||||
* Students may introduce additional fields
|
||||
* such as:
|
||||
* - Lock / ReentrantLock
|
||||
* - ReadWriteLock
|
||||
* - Object monitor
|
||||
* - etc.
|
||||
*/
|
||||
|
||||
public BankAccount(int accountId, long initialBalance) {
|
||||
this.accountId = accountId;
|
||||
this.balance = initialBalance;
|
||||
@@ -23,56 +14,46 @@ public class BankAccount {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Return the current balance in a thread-safe way.
|
||||
*
|
||||
* Requirements:
|
||||
* - Must be safe under concurrent reads/writes
|
||||
* - Should not block unnecessarily if using read/write locks
|
||||
*/
|
||||
public long getBalance() {
|
||||
throw new UnsupportedOperationException("TODO: implement thread-safe balance read");
|
||||
public synchronized long getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Increase balance atomically.
|
||||
*
|
||||
* Requirements:
|
||||
* - Must not lose updates under concurrency
|
||||
*/
|
||||
public void deposit(long amount) {
|
||||
throw new UnsupportedOperationException("TODO: implement thread-safe deposit");
|
||||
public synchronized void deposit(long amount) {
|
||||
balance += amount;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Decrease balance atomically.
|
||||
*
|
||||
* Requirements:
|
||||
* - Must not cause race conditions
|
||||
* - Negative balance handling is NOT required unless you decide
|
||||
* to extend the system (optional)
|
||||
*/
|
||||
public void withdraw(long amount) {
|
||||
throw new UnsupportedOperationException("TODO: implement thread-safe withdraw");
|
||||
public synchronized void withdraw(long amount) {
|
||||
while (balance < amount) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Transfer money between two accounts atomically.
|
||||
*
|
||||
* IMPORTANT REQUIREMENTS:
|
||||
* - Must be atomic (no partial transfer)
|
||||
* - Must be deadlock-free
|
||||
* - Must protect both source and target accounts
|
||||
*
|
||||
* HINT:
|
||||
* - Consider global lock ordering using accountId
|
||||
* - Or tryLock with retry strategy
|
||||
*/
|
||||
public void transfer(BankAccount target, long amount) {
|
||||
throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer");
|
||||
if (this == target) {
|
||||
return;
|
||||
}
|
||||
|
||||
BankAccount first = (this.accountId < target.accountId) ? this : target;
|
||||
BankAccount second = (this.accountId < target.accountId) ? target : this;
|
||||
|
||||
synchronized (first) {
|
||||
synchronized (second) {
|
||||
if (this.balance >= amount) {
|
||||
this.balance -= amount;
|
||||
target.balance += amount;
|
||||
target.notifyAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.withdraw(amount);
|
||||
target.deposit(amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user