认识线程同步
解决方案
方法一:同步代码块
package com.itheima.d3;public class ThreadTest {public static void main(String[] args) {Accout acc = new Accout("ICBC-110",100000);new DrawThread(acc,"小明").start();//小明new DrawThread(acc,"小红").start();//小红Accout acc1 = new Accout("ICBC-112",100000);new DrawThread(acc,"小黑").start();//小黑new DrawThread(acc,"小白").start();//小白}
}
package com.itheima.d3;public class Accout {private String cardId;private double money;public Accout() {}public Accout(String cardId, double money) {this.cardId = cardId;this.money = money;}public static void test(){synchronized (Accout.class){}}public void drawMoney(double money) {//是谁来取钱String name = Thread.currentThread().getName();//1、判断余额是否足够//this代表共享资源synchronized (this) {if (this.money >= money){System.out.println(name + "来取钱" + money + "成功");this.money -= money;System.out.println(name + "取钱后余额" + this.money);}else {System.out.println(name + "来取钱余额不足");}}}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;}}
package com.itheima.d3;public class DrawThread extends Thread{private Accout acc;public DrawThread(Accout acc, String name){super(name);this.acc = acc;}@Overridepublic void run() {//取钱(小明、小红)acc.drawMoney(100000);}
}
方法二:同步方
package com.itheima.d3;public class ThreadTest {public static void main(String[] args) {Accout acc = new Accout("ICBC-110",100000);new DrawThread(acc,"小明").start();//小明new DrawThread(acc,"小红").start();//小红}
}
package com.itheima.d3;public class Accout {private String cardId;private double money;public Accout() {}public Accout(String cardId, double money) {this.cardId = cardId;this.money = money;}//小明、小红同时来取钱//同步方法public synchronized void drawMoney(double money) {//是谁来取钱String name = Thread.currentThread().getName();//1、判断余额是否足够//this代表共享资源if (this.money >= money) {System.out.println(name + "来取钱" + money + "成功");this.money -= money;System.out.println(name + "取钱后余额" + this.money);} else {System.out.println(name + "来取钱余额不足");}}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;}}
package com.itheima.d3;public class DrawThread extends Thread{private Accout acc;public DrawThread(Accout acc, String name){super(name);this.acc = acc;}@Overridepublic void run() {//取钱(小明、小红)acc.drawMoney(100000);}
}
方法三:Lock锁
package com.itheima.d3;public class DrawThread extends Thread{private Accout acc;public DrawThread(Accout acc, String name){super(name);this.acc = acc;}@Overridepublic void run() {//取钱(小明、小红)acc.drawMoney(100000);}
}
package com.itheima.d3;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class Accout {private String cardId;private double money;//创建了一个对象private final Lock lk = new ReentrantLock();public Accout() {}public Accout(String cardId, double money) {this.cardId = cardId;this.money = money;}//小明、小红同时来取钱public void drawMoney(double money) {//是谁来取钱String name = Thread.currentThread().getName();lk.lock();//加锁try {//1、判断余额是否足够if (this.money >= money) {System.out.println(name + "来取钱" + money + "成功");this.money -= money;System.out.println(name + "取钱后余额" + this.money);} else {System.out.println(name + "来取钱余额不足");}} catch (Exception e) {e.printStackTrace();}finally {lk.unlock();//解锁}}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;}}
package com.itheima.d3;public class ThreadTest {public static void main(String[] args) {Accout acc = new Accout("ICBC-110",100000);new DrawThread(acc,"小明").start();//小明new DrawThread(acc,"小红").start();//小红}
}