展开全部
这个要用到线程
代码给你吧
//银行类
public class Bank
{
private final double[] accounts;
public Bank(int n , double initBalance)
{
accounts = new double[n];
for(int i = 0;i<accounts.length;i++)
{
accounts[i] = initBalance;
}
}
public synchronized void transfer(int from,int to,double amount)throws InterruptedException
{
if(accounts[from]<amount)
{
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;
for(double ac : accounts)
{
sum +=ac;
}
return sum;
}
public int size()
{
return accounts.length;
}
}
//线程函数
public class TransferRunnable implements Runnable
{
private Bank bank;
private int fromAc;
private double maxAmt;
private int delay = 10;
public TransferRunnable(Bank bank, int fromAC, double maxAmt)
{
super();
this.bank = bank;
this.fromAc = fromAc;
this.maxAmt = maxAmt;
}
@Override
public void run()
{
while(true)
{
try
{
int toAc =(int)(bank.size()*Math.random());
double amt = maxAmt*Math.random();
bank.transfer(fromAc, toAc, amt);
Thread.sleep((int)(delay*Math.random()));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//测试类
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
Bank b = new Bank(10,10000);
for(int i = 0;i<10;i++)
{
TransferRunnable r = new TransferRunnable(b, 0, 10000);
Thread t = new Thread(r);
t.start();
}
}
}
代码给你吧
//银行类
public class Bank
{
private final double[] accounts;
public Bank(int n , double initBalance)
{
accounts = new double[n];
for(int i = 0;i<accounts.length;i++)
{
accounts[i] = initBalance;
}
}
public synchronized void transfer(int from,int to,double amount)throws InterruptedException
{
if(accounts[from]<amount)
{
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;
for(double ac : accounts)
{
sum +=ac;
}
return sum;
}
public int size()
{
return accounts.length;
}
}
//线程函数
public class TransferRunnable implements Runnable
{
private Bank bank;
private int fromAc;
private double maxAmt;
private int delay = 10;
public TransferRunnable(Bank bank, int fromAC, double maxAmt)
{
super();
this.bank = bank;
this.fromAc = fromAc;
this.maxAmt = maxAmt;
}
@Override
public void run()
{
while(true)
{
try
{
int toAc =(int)(bank.size()*Math.random());
double amt = maxAmt*Math.random();
bank.transfer(fromAc, toAc, amt);
Thread.sleep((int)(delay*Math.random()));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//测试类
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
Bank b = new Bank(10,10000);
for(int i = 0;i<10;i++)
{
TransferRunnable r = new TransferRunnable(b, 0, 10000);
Thread t = new Thread(r);
t.start();
}
}
}
更多追问追答
追问
好多错误 不会改- -!
追答
我测试了,,没有错哦,,你要写三个类,,三个文件,,而不是把他们放在一个文件里哦
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这不是刚开始学java让做的东西么......好久没见过了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询