Java线程同步

本文介绍了Java中三种线程同步方法,包括同步代码块、使用`synchronized`关键字的同步方法以及利用`Lock`接口的ReentrantLock。通过实例演示了在多线程环境下如何避免数据竞争,确保共享资源安全。
摘要由CSDN通过智能技术生成

认识线程同步

 解决方案

方法一:同步代码块

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);
    }
}

 

方法二:同步方

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);
    }
}

 

方法三:Lock锁

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();//小红
    }
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值