package com.bjpowernode.java.threadsafe2;
public class Test {
public static void main(String[] args) {
Account act = new Account("act-001", 10000);
Thread t1 = new AccountThread(act);
Thread t2 = new AccountThread(act);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
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 (this){//这里的this和下面(AccountThread线程类中同步代码块)的this有什么区别
double before = this.getBalance();
double after = before - money;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {**
e.printStackTrace();
}
this.setBalance(after);
}
}
}
class AccountThread extends Thread {
private Account act;
public AccountThread(Account act) {
this.act = act;
}
public void run(){
double money = 5000;
synchronized (this) {//这里的this为什么不能实现同步机制,
act.withdraw(money);
}
System.out.println(Thread.currentThread().getName() + "对"+act.getActno()+"取款"+money+"成功,余额" + act.getBalance());
}
}