【发布时间】:2011-05-18 13:11:39
【问题描述】:
我是 Java 线程编程的新手。为了理解线程,我正在尝试编写一个简单的程序来模拟银行账户。我刚刚实施了退出并尝试对其进行测试。 输出的前几行如下。
T2提现前余额:1000
T2提现后余额:990
T1提现前余额:1000
T1提现后余额:980
T2提现前余额:980
T2提现后余额:970
T1提现前余额:970
T1提现后余额:960
我的问题是为什么输出中的第 3 行(T1 提款前的余额:1000)给出的是 1000 而不是 990。如果它是正确的,它应该在第 2 行。我错过了什么吗?我的方法正确吗?
我的猜测是,试图写入控制台的线程和线程 T1 都没有机会在第二行写入它。
class BankAccount {
private volatile int balance;
public BankAccount(int b){
balance = b;
}
public BankAccount(){
balance = 0;
}
synchronized public int getBalance(){
return balance;
}
synchronized public int withdraw(int w)
{
int b = getBalance();
if(w <= b){
balance = balance-w;
return w;
}
else
return 0;
}
}
class WithdrawAccount implements Runnable{
private BankAccount acc;
private int amount;
public WithdrawAccount(){
acc = null;
amount = 0;
}
public WithdrawAccount(BankAccount acc,int amount){
this.acc = acc;
this.amount = amount;
}
public void run() {
int w;
for(int i =0; i<20; i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Balance before "+Thread.currentThread().getName()+" withdrawl: "+acc.getBalance());
w = acc.withdraw(amount);
System.out.println("Balance after "+Thread.currentThread().getName()+" withdrawl: "+acc.getBalance());
//System.out.println("amount with drawn by: "+Thread.currentThread().getName()+" "+w);
}
}
}
public class TestBankAccount{
public static void main(String[] args) {
BankAccount b = new BankAccount(1000);
WithdrawAccount w = new WithdrawAccount(b,10);
Thread wt1 = new Thread(w);
wt1.setName("T1");
Thread wt2 = new Thread(w);
wt2.setName("T2");
wt1.start();
wt2.start();
}
}
【问题讨论】:
标签: java synchronization