这个要用到线程
代码给你吧
//银行类
public class Bank
{
private final double[] accounts;
public Bank(int n , double initBalance)
{
accounts = new double[n];
for(int i = 0;i
{
accounts[i] = initBalance;
}
}
public synchronized void transfer(int from,int to,double amount)throws InterruptedException
{
if(accounts[from]
{
wait();
}
System.out.println(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("%10.2f from %d to %d",amount,from,to);
accounts[from]+=amount;
System.out.printf("Total Banance:%10.2f%n ",getTotalBalance());
notifyAll();
}
private synchronized Object getTotalBalance()
{
double sum = 0;