您的位置:首页 > 其它

银行取款程序 线程

2016-11-02 19:42 148 查看
public class Account01 {

public int balance;

public Account01(int balance) {
this.balance = balance;
}

public void qu(int money){

synchronized (this) {//对象锁
if(balance >= money){
System.out.println(Thread.currentThread().getName()+"当前余额为:"+balance);
System.out.println(Thread.currentThread().getName()+"取款:"+money);
balance-=money;
System.out.println(Thread.currentThread().getName()+"取款后余额为:"+balance);
}
}


}

}

第2个类 线程类

public class Method01 extends Thread {

public Account01 account;
public int money;

public  Method01(Account01 account,int money) {
this.account = account;
this.money = money;
}

@Override
public void run() {

account.qu(money);

}


}

第3个类 实现类

public class Test01 {

public static void main(String[] args) {

Account01 xm = new Account01(1000);

Thread wx = new Method01(xm,100);
wx.setName("微信");
Thread qq = new Method01(xm,100);
qq.setName("QQ钱包");
Thread zfb = new Method01(xm,100);
zfb.setName("支付宝");

wx.start();
qq.start();
zfb.start();


// Object o1 = new Object();

// Object o2 = new Object();

//

// ThreadA a = new ThreadA(o1,o2);

// ThreadB b = new ThreadB(o1,o2);

//

// a.start();

// b.start();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: