package com.itheima.d3_thread_safe;
public class Account {
private String cardId;
private double money; //账户的余额
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
// //100个线程人
// public static void run(){
// synchronized (Account.class){
//
// }
// }
/**
* 小明 小红
* @return
*/
public void drawMoney(double money) {
//1.拿到是谁来取钱
String name = Thread.currentThread().getName();
//同步代码块
//小明 小红
// this == acc 共享账户
synchronized (this) {
//2.判断余额是否足够
if (this.money >= money) {
//钱够了
System.out.println(name + "来取钱成功,吐出:" + money);
//更新余额
this.money -= money;
System.out.println(name + "取钱后剩余:" + this.money);
} else {
//3.余额不足
System.out.println(name + "来取钱,余额不足!");
}
}
}
}
package com.itheima.d3_thread_safe;
/**
* 取钱的线程类
*/
public class DrawThread extends Thread{
//接受处理的账户对象。
private Account acc;
public DrawThread(Account acc, String name){
super(name);
this.acc = acc;
}
@Override
public void run() {
//小明 小红 :取钱的
acc.drawMoney(100000);
}
}
package com.itheima.d3_thread_safe;
public class TestSafeDemo {
public static void main(String[] args) {
//测试线程安全问题
//1.定义线程类,创建一个共享的账户对象
Account acc = new Account("ICBC-111",100000);
//2.创建2个线程对象,代表小明和小红同时进来了。
new DrawThread(acc,"小明").start();
new DrawThread(acc,"小红").start();
}
}
package com.itheima.d5_thread_synchronized_mothod;
public class Account {
private String cardId;
private double money; //账户的余额
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
// //100个线程人
// public static void run(){
// synchronized (Account.class){
//
// }
// }
/**
* 小明 小红
* this == acc
* @return
*/
public synchronized void drawMoney(double money) {
//1.拿到是谁来取钱
String name = Thread.currentThread().getName();
//2.判断余额是否足够
//小明 小红
// this == acc 共享账户
if (this.money >= money) {
//钱够了
System.out.println(name + "来取钱成功,吐出:" + money);
//更新余额
this.money -= money;
System.out.println(name + "取钱后剩余:" + this.money);
} else {
//3.余额不足
System.out.println(name + "来取钱,余额不足!");
}
}
}
package com.itheima.d5_thread_synchronized_mothod;
import com.itheima.d3_thread_safe.Account;
import com.itheima.d3_thread_safe.DrawThread;
public class TestSafeDemo {
public static void main(String[] args) {
//测试线程安全问题
//1.定义线程类,创建一个共享的账户对象
com.itheima.d3_thread_safe.Account acc = new Account("ICBC-111",100000);
//2.创建2个线程对象,代表小明和小红同时进来了。
new com.itheima.d3_thread_safe.DrawThread(acc,"小明").start();
new DrawThread(acc,"小红").start();
}
}
package com.itheima.d6_thread_synchronized_look;
import com.itheima.d3_thread_safe.Account;
import com.itheima.d3_thread_safe.DrawThread;
public class TestSafeDemo {
public static void main(String[] args) {
//测试线程安全问题
//1.定义线程类,创建一个共享的账户对象
Account acc = new Account("ICBC-111",100000);
//2.创建2个线程对象,代表小明和小红同时进来了。
new DrawThread(acc,"小明").start();
new DrawThread(acc,"小红").start();
}
}
package com.itheima.d6_thread_synchronized_look;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Account {
private String cardId;
private double money; //余额 关键信息
//final修饰后: 锁对象是唯一和不可替换的,非常专业
private final Lock lock = new ReentrantLock();
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
// //100个线程人
// public static void run(){
// synchronized (Account.class){
//
// }
// }
/**
* 小明 小红
* this == acc
* @return
*/
public void drawMoney(double money) {
//1.拿到是谁来取钱
String name = Thread.currentThread().getName();
//2.判断余额是否足够
//小明 小红
lock.lock(); //上锁
try{
if (this.money >= money) {
//钱够了
System.out.println(name + "来取钱成功,吐出:" + money);
//更新余额
this.money -= money;
System.out.println(name + "取钱后剩余:" + this.money);
} else {
//3.余额不足
System.out.println(name + "来取钱,余额不足!");
}
}finally {
lock.unlock(); //解锁
}
}
}
package com.itheima.d6_thread_synchronized_look;
import com.itheima.d3_thread_safe.Account;
/**
* 取钱的线程类
*/
public class DrawThread extends Thread{
//接受处理的账户对象。
private Account acc;
public DrawThread(Account acc, String name){
super(name);
this.acc = acc;
}
@Override
public void run() {
//小明 小红 :取钱的
acc.drawMoney(100000);
}
}