OverwhelmingCC 2018-12-18 21:14 采纳率: 100%
浏览 1499
已采纳

为什么我下面的程序,既没有报错但是没有输出啊?

class Account2
{
private String accountNo;
private double balance;
private boolean flag=false;
public Account2() {}
public Account2(String accountNo,double balance)
{
this.accountNo=accountNo;
this.balance=balance;
}
public void setAccountNo(String n)
{
accountNo=n;
}
public String getAccountNo()
{
return accountNo;
}
public double getBalance()
{
return this.balance;
}
public synchronized void draw(double drawAmount)
{
try
{
if(!flag)
{
wait();
}
else
{
System.out.println(Thread.currentThread().getName()+"取钱:"+drawAmount);
balance-=drawAmount;
System.out.println("账户余额为:"+balance);
flag=false;
notifyAll();
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public synchronized void depoist(double depoistAmount)
{
try
{
if(flag)
{
wait();
}
else
{
System.out.println(Thread.currentThread().getName()+"存钱:"+depoistAmount);
balance+=depoistAmount;
System.out.println("账户余额为:"+balance);
flag=true;
notifyAll();
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public int hashCode()
{
return accountNo.hashCode();
}
public boolean equals(Object obj)
{
if(this==obj)
return true;
if(obj!=null&& obj.getClass()==Account1.class)
{
Account1 target=(Account1)obj;
return target.getAccountNo().equals(accountNo);
}
return false;
}
}
class DrawThread1 extends Thread
{
private Account2 account;
private double drawAmount;
public DrawThread1(String name,Account2 account,double drawAmount)
{
super(name);
this.account=account;
this.drawAmount=drawAmount;
}
public void run()
{
for(int i=0;i<100;i++)
{
account.draw(drawAmount);
}
}
}
class DepoistThread extends Thread
{
private Account2 account;
private double depoistAmount;
public DepoistThread(String name,Account2 account,double depoistAmount)
{
super(name);
this.account=account;
this.depoistAmount=depoistAmount;
}
public void run()
{
for(int i=0;i<100;i++)
{
account.draw(depoistAmount);
}
}
}
public class DrawSaveDemo {

public static void main(String[] args) {
    Account2 acct=new Account2("1234567",0);
    new DrawThread1("取钱者",acct,800).start();
    new DepoistThread("存款者甲",acct,800).start();
    new DepoistThread("存款者乙",acct,800).start();
    new DepoistThread("存款者丙",acct,800).start();
}

}



  • 写回答

1条回答 默认 最新

  • 飘落的秋风 2018-12-19 11:51
    关注

    你的DepoistThread 是从DrawThread 复制的吧,里面应该调用的是account.depoist(depoistAmount)方法,你现在代码都是调用draw,所以就一直等待了。
    PS:以后再提问的话,代码就用代码格式写出来,否则的话大部分人不会回答你这样很难看懂的代码的问题的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度