2019.4.24

2019.4.24

class AnotherBank{
    private double[] accounts;
    // private LongAccumulator adder = new LongAccumulator(Long:sum, 0);
    public static final ThreadLocal<SimpleDateFormat> dataFormat =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    public AnotherBank(int n, double initialBalance){
        accounts = new double[n];
        Arrays.fill(accounts, initialBalance);
    }

    public synchronized void transfer(int from, int to, int amount) throws InterruptedException{
        while(accounts[from] < amount)
            wait();
        accounts[from] -= amount;
        accounts[to] += amount;
        notifyAll();
    }

    public synchronized double getTotalBalance(){
        double sum = 0;
        for(double a: accounts)
            sum += a;
        return sum;
    }

    public int size(){
        return accounts.length;
    }
}

2019.4.24_第1张图片

你可能感兴趣的:(琉璃神社)