悲观锁和乐观锁详解

        乐观锁和悲观锁是并发控制的一种机制,用于多线程或多进程环境下对共享资源的访问管理,以防止数据不一致或竞态条件。它们的主要区别在于对待冲突的策略。

一、悲观锁(Pessimistic Locking) 

        悲观锁是一种对资源持有较悲观态度的锁定方式。它假设数据在并发访问时极有可能发生冲突,因此每次访问数据时都会先加锁,以确保其他线程不能访问此数据直到锁被释放。主要特点包括: 

1、锁定机制:一旦线程获得锁,其它尝试获取锁的线程都会被阻塞,直到锁被释放。常见的实现方式是数据库中的行级锁、表级锁或行级锁等。

2、适用场景:在高并发、数据竞争激烈的场景中使用,如金融交易、库存管理等。这类场景下,冲突发生的可能性很高,所以加锁确保数据的一致性。 

3、 缺点

        (1)可能导致系统吞吐量降低,因为锁定机制会阻止其他线程并发访问资源。

        (2)容易产生死锁(Deadlock),如果锁的持有和释放管理不当,会导致系统无法继续运行 。

4、代码示例 :

public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

    // 同步方法,用于存款
    public synchronized void deposit(double amount) {
        double newBalance = balance + amount;
        balance = newBalance;  // 更新余额
        System.out.println("存款: " + amount + ", 余额: " + balance);
    }

    // 同步方法,用于取款
    public synchronized void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println("取款: " + amount + ", 余额: " + balance);
        } else {
            System.out.println("余额不足");
        }
    }

    // 同步方法,获取余额
    public synchronized double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000);

        // 创建线程执行存款操作
        Thread depositThread = new Thread(() -> {
            account.deposit(200);
        });

        // 创建线程执行取款操作
        Thread withdrawThread = new Thread(() -> {
            account.withdraw(150);
        });

        depositThread.start();
        withdrawThread.start();
    }
}

二、乐观锁(Optimistic Locking) 

        乐观锁则持相对乐观的态度,假设并发操作冲突的可能性较小,因此不会主动加锁,而是进行数据版本检查来决定是否提交操作。主要特点包括: 

1、版本控制:乐观锁一般通过版本号或时间戳等机制来实现。在数据读取时,获取当前版本号,在数据更新时,检查版本号是否与之前读取时的一致。如果一致,表示没有其他并发操作修改过数据,可以提交;否则,操作失败,通常需要重试。 

2、 适用场景:适用于读操作多、写操作少的场景,如一些阅读类应用、CMS系统等。因为这些场景下,冲突发生的概率较低,乐观锁可以提高系统的并发性。

3、优点

        (1)提高系统并发性能,因为不需要加锁阻塞其他线程。

        (2)减少死锁的风险,因为不使用显式锁定。

4、缺点

        (1)在并发冲突频繁的场景下,可能会导致大量重试操作,影响性能。

        (2)需要开发人员显式管理版本控制机制,增加开发复杂度。

5、代码示例: 

 实体类

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private int quantity;

    @Version
    private int version;

    // getters and setters 省略
}

 Repository接口:

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {
}

服务类: 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Transactional
    public void updateProduct(Long productId, int newQuantity) {
        Product product = productRepository.findById(productId).orElseThrow(() -> new RuntimeException("产品没找到"));
        product.setQuantity(newQuantity);
        productRepository.save(product);
    }
}

测试乐观锁: 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class OptimisticLockTestRunner implements CommandLineRunner {

    @Autowired
    private ProductService productService;

    @Override
    public void run(String... args) throws Exception {
        Long productId = 1L;

        // 模拟第一个事务
        new Thread(() -> {
            try {
                productService.updateProduct(productId, 50);
                System.out.println("线程1: 产品更新成功");
            } catch (Exception e) {
                System.out.println("线程1: " + e.getMessage());
            }
        }).start();

        // 模拟第二个事务
        new Thread(() -> {
            try {
                Thread.sleep(1000); // 保证第一个事务先执行
                productService.updateProduct(productId, 100);
                System.out.println("线程2: 产品更新成功");
            } catch (Exception e) {
                System.out.println("线程2: " + e.getMessage());
            }
        }).start();
    }
}
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真真假假々

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值