sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

java 解决线程安全的两种方式(Synchornized和Lock)
原文链接:https://www.cnblogs.com/MrFugui/p/15610780.html

synchornized与lock的不同:

synchronized机制在执行完相应的同步代码以后,自动的释放同步监视器
lock需要手动的启动同步(Lock()),同时结束同步也需要使用手动的实现(unlock())

synchornized方法实现线程同步方法:

public class ThreadBankTest {
    public static void main(String[] args) {
        Account account = new Account(0);
        Customer customer = new Customer(account);
        Customer customer2 = new Customer(account);
        customer.setName("甲");
        customer2.setName("乙");
        customer.start();
        customer2.start();
    }
}
class Customer extends Thread {
    private Account account;
    public Customer(Account account) {
        this.account = account;
    }
    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}
class Account {
    private int balance;
    public Account(int balance) {
        this.balance = balance;
    }
    //存钱
    public synchronized void deposit(int money) {
        if (money > 0) {
            balance += money;
            System.out.println(Thread.currentThread().getName()+"存钱成功"+balance);
        }
    }
}

Lock实现线程同步的方法:

public class ThreadBankTest2 {
    public static void main(String[] args) {
        Account2 account2 = new Account2(0);
        Customer2 customer = new Customer2(account2);
        Customer2 customer2 = new Customer2(account2);
        customer.setName("甲");
        customer2.setName("乙");
        customer.start();
        customer2.start();
    }
}
class Customer2 extends Thread {
    private Account2 account;
    public Customer2(Account2 account) {
        this.account = account;
    }
    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}
class Account2 {
    private int balance;
    //1.实例化ReentrantLock
    private ReentrantLock lock = new ReentrantLock();
    public Account2(int balance) {
        this.balance = balance;
    }
    //存钱
    public void deposit(int money) {
        try {
            //2.调用lock方法:要把要同步的代码放入到try中,try后面的代码就是要实现同步代码
            lock.lock();
            if (money > 0) {
                balance += money;
                System.out.println(Thread.currentThread().getName()+"存钱成功"+balance);
            }
        } finally {
            //3.给lock解锁unlock
            lock.unlock();
        }
    }
}
posted on   sunny123456  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-08-01 $(document).ready和window.onload区别
2021-08-01 为什么script标签一般放在body下面
2021-08-01 idea添加jar和移除jar包的三种方式
2021-08-01 HTML转义字符大全(转)
2021-08-01 C#中 以固定长度 取字符串中的数据
2021-08-01 Oracle的substr函数简单用法和 C#中一样
2021-08-01 ORA-22835:缓冲区对于CLOB到CHAR转换而言太小
点击右上角即可分享
微信分享提示