悲观锁和乐观锁
悲观锁#
总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁
传统的关系型数据库里边就用到了很多这种锁机制
- 行锁
- 表锁
- 读锁
- 写锁
都是在做操作之前先上锁,再比如Java里面的同步原语synchronized关键字的实现也是悲观锁
乐观锁#
顾名思义,总假设最乐观的情况,每次去拿数据的时候都认为别人不会修改,所以不会上锁
但是在更新的时候会判断一下在此期间别人有没有去更新这个数据可以使用版本号等机制
乐观锁适用于多读的应用类型,这样可以提高吞吐量,像数据库提供的类似于write_condition机制,其实都是提供的乐观锁
在Java中java.util.concurrent.atomic包下面的原子变量类就是使用了乐观锁的一种实现方式--CAS实现的
CAS Compare And Swap#
内存地址V,旧的预期值A,要修改的新值B
更新一个变量的时候,只有当变量的预期值A和内存地址V当中的实际值相同时,才会将内存地址V对应的值修改为B
举个例子#
线程A想更新内存地址C指向的值,假设现在C的值是10,A要进行减1操作
线程B在A对C操作之前捷足先登,对C的值加1,此时C的值是11
线程A先检查C的值是否为10,发现不是,则A不做操作;否则,A对C的值减1
C语言示例#
int cas(long *addr, long old, long new) { /* 原子执行 */ if(*addr != old) return 0; *addr = new; return 1; }
Automic原子类 AutomicInteger#
/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent.atomic; import java.util.function.IntUnaryOperator; import java.util.function.IntBinaryOperator; import sun.misc.Unsafe; /** * An {@code int} value that may be updated atomically. See the * {@link java.util.concurrent.atomic} package specification for * description of the properties of atomic variables. An * {@code AtomicInteger} is used in applications such as atomically * incremented counters, and cannot be used as a replacement for an * {@link java.lang.Integer}. However, this class does extend * {@code Number} to allow uniform access by tools and utilities that * deal with numerically-based classes. * * @since 1.5 * @author Doug Lea */ public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } private volatile int value; /** * Creates a new AtomicInteger with the given initial value. * * @param initialValue the initial value */ public AtomicInteger(int initialValue) { value = initialValue; } /** * Creates a new AtomicInteger with initial value {@code 0}. */ public AtomicInteger() { } /** * Gets the current value. * * @return the current value */ public final int get() { return value; } /** * Sets to the given value. * * @param newValue the new value */ public final void set(int newValue) { value = newValue; } /** * Eventually sets to the given value. * * @param newValue the new value * @since 1.6 */ public final void lazySet(int newValue) { unsafe.putOrderedInt(this, valueOffset, newValue); } /** * Atomically sets to the given value and returns the old value. * * @param newValue the new value * @return the previous value */ public final int getAndSet(int newValue) { return unsafe.getAndSetInt(this, valueOffset, newValue); } /** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * @param expect the expected value * @param update the new value * @return {@code true} if successful. False return indicates that * the actual value was not equal to the expected value. */ public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * <p><a href="package-summary.html#weakCompareAndSet">May fail * spuriously and does not provide ordering guarantees</a>, so is * only rarely an appropriate alternative to {@code compareAndSet}. * * @param expect the expected value * @param update the new value * @return {@code true} if successful */ public final boolean weakCompareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * Atomically increments by one the current value. * * @return the previous value */ public final int getAndIncrement() { return unsafe.getAndAddInt(this, valueOffset, 1); } /** * Atomically decrements by one the current value. * * @return the previous value */ public final int getAndDecrement() { return unsafe.getAndAddInt(this, valueOffset, -1); } /** * Atomically adds the given value to the current value. * * @param delta the value to add * @return the previous value */ public final int getAndAdd(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta); } /** * Atomically increments by one the current value. * * @return the updated value */ public final int incrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, 1) + 1; } /** * Atomically decrements by one the current value. * * @return the updated value */ public final int decrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, -1) - 1; } /** * Atomically adds the given value to the current value. * * @param delta the value to add * @return the updated value */ public final int addAndGet(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta) + delta; } /** * Atomically updates the current value with the results of * applying the given function, returning the previous value. The * function should be side-effect-free, since it may be re-applied * when attempted updates fail due to contention among threads. * * @param updateFunction a side-effect-free function * @return the previous value * @since 1.8 */ public final int getAndUpdate(IntUnaryOperator updateFunction) { int prev, next; do { prev = get(); next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return prev; } /** * Atomically updates the current value with the results of * applying the given function, returning the updated value. The * function should be side-effect-free, since it may be re-applied * when attempted updates fail due to contention among threads. * * @param updateFunction a side-effect-free function * @return the updated value * @since 1.8 */ public final int updateAndGet(IntUnaryOperator updateFunction) { int prev, next; do { prev = get(); next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return next; } /** * Atomically updates the current value with the results of * applying the given function to the current and given values, * returning the previous value. The function should be * side-effect-free, since it may be re-applied when attempted * updates fail due to contention among threads. The function * is applied with the current value as its first argument, * and the given update as the second argument. * * @param x the update value * @param accumulatorFunction a side-effect-free function of two arguments * @return the previous value * @since 1.8 */ public final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) { int prev, next; do { prev = get(); next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(prev, next)); return prev; } /** * Atomically updates the current value with the results of * applying the given function to the current and given values, * returning the updated value. The function should be * side-effect-free, since it may be re-applied when attempted * updates fail due to contention among threads. The function * is applied with the current value as its first argument, * and the given update as the second argument. * * @param x the update value * @param accumulatorFunction a side-effect-free function of two arguments * @return the updated value * @since 1.8 */ public final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) { int prev, next; do { prev = get(); next = accumulatorFunction.applyAsInt(prev, x); } while (!compareAndSet(prev, next)); return next; } /** * Returns the String representation of the current value. * @return the String representation of the current value */ public String toString() { return Integer.toString(get()); } /** * Returns the value of this {@code AtomicInteger} as an {@code int}. */ public int intValue() { return get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code long} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public long longValue() { return (long)get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code float} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public float floatValue() { return (float)get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code double} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public double doubleValue() { return (double)get(); } }
AutomicInteger示例(结果一直是100)#
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class test { public static AtomicInteger count = new AtomicInteger(0); public static void inc(){ try{ Thread.sleep(1); //延迟1毫秒 }catch (InterruptedException e){ //catch住中断异常,防止程序中断 e.printStackTrace(); } count.getAndIncrement();//count值自加1 } public static void main(String[] args) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(100); for(int i=0;i<100;i++){ new Thread(new Runnable() { @Override public void run() { test.inc(); latch.countDown(); } }).start(); } latch.await(); System.out.println("运行结果:"+ test.count); } }
Integer示例(结果可能会出现9x)#
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class test { // public static AtomicInteger count = new AtomicInteger(0); public static Integer count = 0; public static void inc(){ try{ Thread.sleep(1); //延迟1毫秒 }catch (InterruptedException e){ //catch住中断异常,防止程序中断 e.printStackTrace(); } count++; //count.getAndIncrement();//count值自加1 } public static void main(String[] args) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(100); for(int i=0;i<100;i++){ new Thread(new Runnable() { @Override public void run() { test.inc(); latch.countDown(); } }).start(); } latch.await(); System.out.println("运行结果:"+ test.count); } }
CAS的缺点#
ABA问题#
假设线程A在检查预期值C的之前,线程B对C做了改动,执行了线程B中的任务,然后线程B又将C的值改回去了
之后线程A去检查,发现等于预期值,然后执行线程A中的任务,相当于默认了线程B中的任务没有执行,线程A不再等待直接执行,导致乱序
从Java1.5开始JDK的atomic包里提供了一个类AtomicStampedReference来解决ABA问题#
这个类的compareAndSet方法作用是首先检查当前引用是否等于预期引用,并且当前标志是否等于预期标志
如果全部相等,则以原子方式将该引用和该标志的值设置为给定的更新值
循环时间长开销大
自旋CAS(不成功,就一直循环执行,直到成功)如果长时间不成功,会给CPU带来非常大的执行开销
如果JVM能支持处理器提供的pause指令那么效率会有一定的提升
pause指令有两个作用
- 第一它可以延迟流水线执行指令(de-pipeline),使CPU不会消耗过多的执行资源,延迟的时间取决于具体实现的版本,在一些处理器上延迟时间是零
- 第二它可以避免在退出循环的时候因内存顺序冲突(memory order violation)而引起CPU流水线被清空(CPU pipeline flush),从而提高CPU的执行效率。
只能保证一个共享变量的原子操作
当对一个共享变量执行操作时,我们可以使用循环CAS的方式来保证原子操作
但是对多个共享变量操作时,循环CAS就无法保证操作的原子性,这个时候就可以用锁
或者把多个共享变量合并成一个共享变量来操作
比如有两个共享变量i=2,j=a,合并一下ij=2a,然后用CAS来操作ij
从Java1.5开始JDK提供了AtomicReference类来保证引用对象之间的原子性,你可以把多个变量放在一个对象里来进行CAS操作。#
CAS与Synchronized的使用情景:
对于资源竞争较少(线程冲突较轻)的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源
而CAS基于硬件实现,不需要进入内核,不需要切换线程,操作自旋几率较少,因此可以获得更高的性能
对于资源竞争严重(线程冲突严重)的情况,CAS自旋的概率会比较大,从而浪费更多的CPU资源,效率低于synchronized
synchronized在jdk1.6之后,已经改进优化。synchronized的底层实现主要依靠Lock-Free的队列
基本思路是自旋后阻塞,竞争切换后继续竞争锁,稍微牺牲了公平性,但获得了高吞吐量
在线程冲突较少的情况下,可以获得和CAS类似的性能;而线程冲突严重的情况下,性能远高于CAS
Synchronized关键字锁升级过程,参考文章:https://www.cnblogs.com/YC-L/p/14433044.html
LongAdder#
为了解决AutomicInteger等原子操作类在高并发下的性能问题,JDK8添加了LongAdder和DoubleAdder高并发增强原子类
其基本思想就是对于多个线程对同一个变量操作不相互排斥,而是保存每个线程对变量的修改
在读取变量的时候进行汇总,这样就不会造成线程间的互斥和重试,极大提高想并发性能
LongAdder实现类的原理#
-
定义Cell数组用来存储各个线程对变量的修改
-
如果Cell数组不为空则使用Cell数组进行汇总求和,如果Cell数组为空则调用caseBase()方法进行CAS操作
-
如果CAS更新失败则创建Cell数组进行分线程更新
/** * Adds the given value. * * @param x the value to add */ public void add(long x) { Cell[] as; long b, v; int m; Cell a; if ((as = cells) != null || !casBase(b = base, b + x)) { boolean uncontended = true; if (as == null || (m = as.length - 1) < 0 || (a = as[getProbe() & m]) == null || !(uncontended = a.cas(v = a.value, v + x))) longAccumulate(x, null, uncontended); } }
atomic、synchronzied、longadder三个关键字效率比较#
import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAdder; public class test { static long count2 = 0L; static AtomicLong count1 = new AtomicLong(0L); static LongAdder count3 = new LongAdder(); public static void main(String[] args) throws Exception { Thread[] threads = new Thread[1000]; for(int i=0; i<threads.length; i++) { threads[i] = new Thread(()-> { for(int k=0; k<100000; k++) count1.incrementAndGet(); }); } long start = System.currentTimeMillis(); for(Thread t : threads ) t.start(); for (Thread t : threads) t.join(); long end = System.currentTimeMillis(); //TimeUnit.SECONDS.sleep(10); System.out.println("Atomic: " + count1.get() + " time " + (end-start)); //----------------------------------------------------------- Object lock = new Object(); for(int i=0; i<threads.length; i++) { threads[i] = new Thread(new Runnable() { @Override public void run() { for (int k = 0; k < 100000; k++) synchronized (lock) { count2++; } } }); } start = System.currentTimeMillis(); for(Thread t : threads ) t.start(); for (Thread t : threads) t.join(); end = System.currentTimeMillis(); System.out.println("Sync: " + count2 + " time " + (end-start)); //---------------------------------- for(int i=0; i<threads.length; i++) { threads[i] = new Thread(()-> { for(int k=0; k<100000; k++) count3.increment(); }); } start = System.currentTimeMillis(); for(Thread t : threads ) t.start(); for (Thread t : threads) t.join(); end = System.currentTimeMillis(); //TimeUnit.SECONDS.sleep(10); System.out.println("LongAdder: " + count1.longValue() + " time " + (end-start)); } static void microSleep(int m) { try { TimeUnit.MICROSECONDS.sleep(m); } catch (InterruptedException e) { e.printStackTrace(); } } }
测试结果#
作者:BigBender
出处:https://www.cnblogs.com/BigBender/p/14496837.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!