public class Account {
private String actno;
private double balance;
public Account() {
}
public Account(String actno, double balance) {
this.actno = actno;
this.balance = balance;
}
public String getActno() {
return actno;
}
public void setActno(String actno) {
this.actno = actno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withDraw(double money) {
/*
线程同步机制语法是
synchronized() {
线程同步代码块
}
synchronized后面小括号中的数据是需要线程同步的那些线程共享的数据
*/
synchronized (this) {
double before = this.getBalance();
double after = before - money;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setBalance(after);
}
}
}
javase:线程同步
最新推荐文章于 2024-08-16 23:08:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)