Skip to content

并发篇-悲观锁vs乐观锁

要求

  • 掌握悲观锁和乐观锁的区别

对比悲观锁与乐观锁

  • 悲观锁的代表是 synchronized 和 Lock 锁

    • 其核心思想是【线程只有占有了锁,才能去操作共享变量,每次只有一个线程占锁成功,获取锁失败的线程,都得停下来等待】
    • 线程从运行到阻塞、再从阻塞到唤醒,涉及线程上下文切换,如果频繁发生,影响性能
    • 实际上,线程在获取 synchronized 和 Lock 锁时,如果锁已被占用,都会做几次重试操作,减少阻塞的机会
  • 乐观锁的代表是 AtomicInteger,使用 cas 来保证原子性

    • 其核心思想是【无需加锁,每次只有一个线程能成功修改共享变量,其它失败的线程不需要停止,不断重试直至成功】
    • 由于线程一直运行,不需要阻塞,因此不涉及线程上下文切换
    • 它需要多核 cpu 支持,且线程数不应超过 cpu 核数

cas实现原理

java
package day02;


import jdk.internal.misc.Unsafe;

// --add-opens java.base/jdk.internal.misc=ALL-UNNAMED
public class SyncVsCas {
    static final Unsafe U = Unsafe.getUnsafe();
    static final long BALANCE = U.objectFieldOffset(Account.class, "balance");

    static class Account {
        volatile int balance = 10;
    }

    private static void showResult(Account account, Thread t1, Thread t2) {
        try {
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            LoggerUtils.get().debug("{}", account.balance);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void basicCas(Account account) {
        while (true) {
            int o = account.balance;
            int n = o + 5;
            if(U.compareAndSetInt(account, BALANCE, o, n)){
                break;
            }
        }
        System.out.println(account.balance);
    }

    public static void main(String[] args) {
        Account account = new Account();
        cas(account);
    }
}

synchronized实现线程安全

java
public static void sync(Account account) {
    Thread t1 = new Thread(() -> {
        synchronized (account) {
            int old = account.balance;
            int n = old - 5;
            account.balance = n;
        }
    },"t1");

    Thread t2 = new Thread(() -> {
        synchronized (account) {
            int o = account.balance;
            int n = o + 5;
            account.balance = n;
        }
    },"t2");

    showResult(account, t1, t2);
}

cas实现线程安全

java
public static void cas(Account account) {
    Thread t1 = new Thread(() -> {
        while (true) {
            int o = account.balance;
            int n = o - 5;
            if (U.compareAndSetInt(account, BALANCE, o, n)) {
                break;
            }
        }
    },"t1");

    Thread t2 = new Thread(() -> {
        while (true) {
            int o = account.balance;
            int n = o + 5;
            if (U.compareAndSetInt(account, BALANCE, o, n)) {
                break;
            }
        }
    },"t2");

    showResult(account, t1, t2);
}