本人菜鸟想求教!为什么只有存款线程启动,取款线程没反应

J5175 2010-11-22 09:08:35
package account;
public class Bank {
public static void main(String[] argos){

Account a=new Account("12345678",100);
PutMoney p=new PutMoney(a);
GetMoney g=new GetMoney(a);
Thread tp=new Thread(p);
Thread tg=new Thread(g);
tp.start();
tg.start();
}
}




class Account {
String ID;
double balance;
public Account(String strID,double b){
ID=strID;
balance=b;
}

public synchronized void putMoney(double money){

System.out.println("账号:"+ID+"存入"+money);
balance +=money;
System.out.println("账号:"+ID+" 余额"+balance);





}
public synchronized void subMoney(double money){

System.out.println("账号:"+ID+" 取出"+money);
if(balance>money){
balance -=money;
System.out.println("账号:"+ID+" 余额"+balance);
b=true;
}
else{
System.out.println("余额不足,取款失败!");
}
}}


class PutMoney implements Runnable{
Account a;

public PutMoney(Account a){

this.a=a;
}
public void run(){
for(int i=0;i<10;i++){
a.putMoney(Math.random()*1000);
}

}
}

class GetMoney implements Runnable{
Account a;

public GetMoney(Account a){

this.a=a;
}
public void run(){
for(int i=0;i<10;i++){
a.putMoney(Math.random()*1000);
}
}
}
...全文
62 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zh2332926106 2010-11-23
  • 打赏
  • 举报
回复
GetMoney类的run方法调用subMoney而不是putMoney
lihaigai11 2010-11-22
  • 打赏
  • 举报
回复
发错地方了?
另b=true; 哪来的b?
ufo1440 2010-11-22
  • 打赏
  • 举报
回复

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧