本人菜鸟想求教!为什么只有存款线程启动,取款线程没反应
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);
}
}
}