黑马JAVA P172 线程同步、同步代码块、同步方法、同步锁

 

 


 

 

 

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值