Java并发

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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值