package thread;
import java.util.concurrent.locks.ReentrantLock;
/**
* @auto dh
* @create 2020-03-28-13:07
*/
class Account {
private Double balance;
private ReentrantLock lock=new ReentrantLock();
public Account(Double balance) {
this.balance = balance;
}
public void deposite(Double amt) {
try {
lock.lock();
if (amt > 0) {
balance += amt;
}
System.out.println(Thread.currentThread().getName() + ":存款成功,余额为" + balance);
}finally {
lock.unlock();
}
}
}
class Customer extends Thread {
private Account account;
public Customer(Account account) {
this.account = account;
}
public void run() {
for (int i = 0; i < 3; i++) {
this.account.deposite(1000.0);
}
}
}
public class Thread005 {
public static void main(String[] args) {
Account account=new Account(0.0);
Customer customerA=new Customer(account);
Customer customerB=new Customer(account);
customerA.setName("张三");
customerB.setName("李四");
customerA.start();
customerB.start();
}
}