import java.util.Arrays;
class Bank {
private int[] accounts;
public Bank(int n, int initialBalance) {
accounts = new int[n];
Arrays.fill(accounts, initialBalance);
}
public void transfer(int from, int to, int amount) {
if (accounts[from] < amount) {
return;
}
accounts[from] -= amount;
accounts[to] += amount;
System.out.printf("%s. %d from %d to %d. Total Balance: %d\n",
Thread.currentThread(), amount, from, to, getTotalBalance());
}
public int getTotalBalance() {
int sum = 0;
for (int account : accounts) {
sum += account;
}
return sum;
}
public int size() {
return accounts.length;
}
}
class UnSyncBankTest {
public static final int N_ACCOUNTS = 100;
public static final int INITIAL_BALANCE = 1000;
public static final int MAX_AMOUNT = 1000;
public static final int DELAY = 10;
public static void test() {
Bank bank = new Bank(N_ACCOUNTS, INITIAL_BALANCE);
for (int i = 0; i < N_ACCOUNTS; i++) {
int fromAccount = i;
Runnable r = () -> {
try {
while (true) {
int toAccount = (int) (bank.size() * Math.random());
int amount = (int) (MAX_AMOUNT * Math.random());
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int) (DELAY * Math.random()));
}
} catch (InterruptedException e) {
}
};
Thread t = new Thread(r);
t.start();
}
}
}
public class Main {
public static void main(String[] args) {
UnSyncBankTest.test();
}
}
import java.util.Arrays;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Bank {
private final int[] accounts;
private Lock bankLock;
private Condition sufficientFunds;
public Bank(int n, int initialBalance) {
accounts = new int[n];
Arrays.fill(accounts, initialBalance);
bankLock = new ReentrantLock();
sufficientFunds = bankLock.newCondition();
}
public void transfer(int from, int to, int amount) throws InterruptedException {
bankLock.lock();
try {
while (accounts[from] < amount) {
sufficientFunds.await();
}
accounts[from] -= amount;
accounts[to] += amount;
System.out.printf("%s %d from %d to %d Total Balance: %d\n",
Thread.currentThread(),
amount, from, to, getTotalBalance());
sufficientFunds.signalAll();
} finally {
bankLock.unlock();
}
}
public int getTotalBalance() {
bankLock.lock();
try {
int sum = 0;
for (int a : accounts) {
sum += a;
}
return sum;
} finally {
bankLock.unlock();
}
}
public int size() {
return accounts.length;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
final int n = 100;
final int initialBalance = 1000;
final int maxAmount = 1000;
final int delay = 10;
Bank bank = new Bank(n, initialBalance);
for (int i = 0; i < bank.size(); i++) {
int fromAccount = i;
Runnable r = () -> {
try {
while (true) {
int toAccount = (int) (bank.size() * Math.random());
int amount = (int) (maxAmount * Math.random());
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int) (delay * Math.random()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread t = new Thread(r);
t.start();
}
}
}