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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题