认识线程同步
![Java线程同步_第1张图片](http://img.e-com-net.com/image/info8/c56603b1f64c4559b1633bd0509a0518.jpg)
解决方案
![Java线程同步_第2张图片](http://img.e-com-net.com/image/info8/902e33ad8cab424692c1b8d7cf345716.jpg)
方法一:同步代码块
![Java线程同步_第3张图片](http://img.e-com-net.com/image/info8/8b338260b9cd4fab826b85e3cc7427f3.jpg)
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;
}
@Override
public void run() {
//取钱(小明、小红)
acc.drawMoney(100000);
}
}
方法二:同步方
![Java线程同步_第5张图片](http://img.e-com-net.com/image/info8/e0a0d6487cfe4fd0abdfb972e8333e3e.jpg)
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;
}
@Override
public void run() {
//取钱(小明、小红)
acc.drawMoney(100000);
}
}
![Java线程同步_第6张图片](http://img.e-com-net.com/image/info8/791c08af084a4f31aa8b17540ae11b6f.jpg)
方法三:Lock锁
![Java线程同步_第7张图片](http://img.e-com-net.com/image/info8/2957b5fa683848649758562f3f98ced6.jpg)
package com.itheima.d3;
public class DrawThread extends Thread{
private Accout acc;
public DrawThread(Accout acc, String name){
super(name);
this.acc = acc;
}
@Override
public 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();//小红
}
}