使用synchronized同步线程,同一个账号取款的例子。在run方法中输出的金额有时候是不对的,如图,正常来说是900,800。但是多次运行发现有800,800的情况。
代码如下:
//同一账户同步取款
public class testSynchronized {
public static void main(String[] args) {
Account act = new Account("act-1", 1000);
Thread t1 = new AccountThread(act);
Thread t2 = new AccountThread(act);
t1.setName("t1");
t1.setName("t2");
t1.start();
t2.start();
}
}
//取款线程
class AccountThread extends Thread {
private Account act;
//通过构造方法传递过来账户对象
public AccountThread(Account account) {
this.act = account;
}
@Override
public void run() {
Integer amount = 100; //取款金额
if (act.getBalance() > 0) {
act.withdraw(amount);
System.out.println("---" + act.getBalance());
}
}
}
//账户类
class Account {
private String actno; //账号
private Integer balance; //余额
public Account(String actno, Integer balance) {
this.actno = actno;
this.balance = balance;
}
//取款方法
public void withdraw(Integer amount) {
synchronized (this) {//取款
if (this.getBalance() == 0) {
return;
}
Integer after = this.getBalance() - amount;
this.setBalance(after);
try {
Thread.sleep(1000);
System.out.println(this.getActno() + " 取款: " + amount + " ,余额: " + this.getBalance() + " ---> " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public String getActno() {
return actno;
}
public void setActno(String actno) {
this.actno = actno;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
}
如下: