• JUC笔记(五) --- 无锁共享模型


    无锁共享模型

    本章内容

    5.1问题引入

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class TestAccount {
        public static void main(String[] args) {
            Account account = new AccountCas(10000);
            Account.demo(account);
        }
    }
    
    class AccountUnsafe implements Account {
    
        private Integer balance;
    
        public AccountUnsafe(Integer balance) {
            this.balance = balance;
        }
    
        @Override
        public Integer getBalance() {
            synchronized (this) {
                return this.balance;
            }
        }
    
        @Override
        public void withdraw(Integer amount) {
            synchronized (this) {
                this.balance -= amount;
            }
        }
    }
    
    interface Account {
        // 获取余额
        Integer getBalance();
    
        // 取款
        void withdraw(Integer amount);
    
        /**
         * 方法内会启动 1000 个线程,每个线程做 -10 元 的操作
         * 如果初始余额为 10000 那么正确的结果应当是 0
         */
        static void demo(Account account) {
            List<Thread> ts = new ArrayList<>();
            for (int i = 0; i < 1000; i++) {
                ts.add(new Thread(() -> {
                    account.withdraw(10);
                }));
            }
            long start = System.nanoTime();
            ts.forEach(Thread::start);
            ts.forEach(t -> {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            long end = System.nanoTime();
            System.out.println(account.getBalance()
                    + " cost: " + (end-start)/1000_000 + " ms");
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    out:

    330 cost: 306 ms
    
    • 1

    可以发现withdraw()方法并不是线程安全的

    对应的字节码文件:

    ALOAD 0 // <- this
    ALOAD 0
    GETFIELD cn/itcast/AccountUnsafe.balance : Ljava/lang/Integer; // <- this.balance
    INVOKEVIRTUAL java/lang/Integer.intValue ()I // 拆箱
    ALOAD 1 // <- amount
    INVOKEVIRTUAL java/lang/Integer.intValue ()I // 拆箱
    ISUB // 减法
    INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; // 结果装箱
    PUTFIELD cn/itcast/AccountUnsafe.balance : Ljava/lang/Integer; // -> this.balance
    
    // 多线程执行流程
    ALOAD 0 // thread-0 <- this 
    ALOAD 0 
    GETFIELD cn/itcast/AccountUnsafe.balance // thread-0 <- this.balance 
    INVOKEVIRTUAL java/lang/Integer.intValue // thread-0 拆箱
    ALOAD 1 // thread-0 <- amount 
    INVOKEVIRTUAL java/lang/Integer.intValue // thread-0 拆箱
    ISUB // thread-0 减法
    INVOKESTATIC java/lang/Integer.valueOf // thread-0 结果装箱
    PUTFIELD cn/itcast/AccountUnsafe.balance // thread-0 -> this.balance 
     
     
    ALOAD 0 // thread-1 <- this 
    ALOAD 0 
    GETFIELD cn/itcast/AccountUnsafe.balance // thread-1 <- this.balance 
    INVOKEVIRTUAL java/lang/Integer.intValue // thread-1 拆箱
    ALOAD 1 // thread-1 <- amount 
    INVOKEVIRTUAL java/lang/Integer.intValue // thread-1 拆箱
    ISUB // thread-1 减法
    INVOKESTATIC java/lang/Integer.valueOf // thread-1 结果装箱
    PUTFIELD cn/itcast/AccountUnsafe.balance // thread-1 -> this.balance
    
    • 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
    • 单核的指令交错

    • 多核的指令交错

    解决思路:

    • withDraw()加锁:方法参照之前讲的 eg. synchronized , lock
    • 无锁解决
    @Override
        public void withdraw(Integer amount) {
            /*while(true) {
                // 获取余额的最新值
                int prev = balance.get();
                // 要修改的余额
                int next = prev - amount;
                // 真正修改
                if(balance.compareAndSet(prev, next)) {
                    break;
                }
            }*/
            balance.getAndAdd(-1 * amount);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    out:

    0 cost: 302 ms
    
    • 1

    5.2CAS volatile(**)

    前面看到的 AtomicInteger 的解决方法,内部并没有用锁来保护共享变量的线程安全。那么它是如何实现的呢?

    • compareAndSet 正是做这个检查,在 set 前,先比较 prev 与当前值

      • 不一致了,next 作废,返回 false 表示失败

      比如,别的线程已经做了减法,当前值已经被减成了 990

      那么本线程的这次 990 就作废了,进入 while 下次循环重试

      • 一致,以 next 设置为新值,返回 true 表示成功

    感觉有点想分布式控制一致性时的版本戳

    其中的关键是 compareAndSet,它的简称就是 CAS (也有 Compare And Swap 的说法),它必须是原子操作。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-unG3wTRL-1667462986359)(assets/image-20221101165115909.png)]

    注意

    其实 CAS 的底层是 lock cmpxchg 指令(X86 架构),在单核 CPU 和多核 CPU 下都能够保证【比较-交

    换】的原子性。

    • 在多核状态下,某个核执行到带 lock 的指令时,CPU 会让总线锁住,当这个核把此指令执行完毕,再

      开启总线。这个过程中不会被线程的调度机制所打断,保证了多个线程对内存操作的准确性,是原子

      的。

    volatile

    获取共享变量时,为了保证该变量的可见性,需要使用 volatile 修饰。

    它可以用来修饰成员变量和静态成员变量,他可以避免线程从自己的工作缓存中查找变量的值,必须到主存中获取

    它的值,线程操作 volatile 变量都是直接操作主存。即一个线程对 volatile 变量的修改,对另一个线程可见。

    注意

    volatile 仅仅保证了共享变量的可见性,让其它线程能够看到最新值,但不能解决指令交错问题(不能保证原

    子性)

    CAS 必须借助 volatile 才能读取到共享变量的最新值来实现【比较并交换】的效果

    无锁的效率更高
    • 无锁情况下,即使重试失败,线程始终在高速运行,没有停歇,而 synchronized 会让线程在没有获得锁的时候,发生上下文切换,进入阻塞。打个比喻
    • 线程就好像高速跑道上的赛车,高速运行时,速度超快,一旦发生上下文切换,就好比赛车要减速、熄火,等被唤醒又得重新打火、启动、加速… 恢复到高速运行,代价比较大
    • 但无锁情况下,因为线程要保持运行,需要额外 CPU 的支持,CPU 在这里就好比高速跑道,没有额外的跑道,线程想高速运行也无从谈起,虽然不会进入阻塞,但由于没有分到时间片,仍然会进入可运行状态,还是会导致上下文切换。
    cas的特点

    结合 CAS 和 volatile 可以实现无锁并发,适用于线程数少、多核 CPU 的场景下

    • CAS 是基于乐观锁的思想:最乐观的估计,允许别的线程来修改共享变量,就算修改也没关系
    • synchronized 是基于悲观锁的思想:最悲观的估计,禁止着其它线程来修改共享变量
    • CAS 体现的是无锁并发、无阻塞并发,请仔细体会这两句话的意思
      • 因为没有使用 synchronized,所以线程不会陷入阻塞,这是效率提升的因素之一
      • 如果竞争激烈,可以想到重试必然频繁发生,反而效率会受影响

    5.3原子整数

    J.U.C 并发包提供了:

    • AtomicBoolean

    • AtomicInteger

    • AtomicLong

    eg.

    AtomicInteger i = new AtomicInteger(0);
    // 获取并自增(i = 0, 结果 i = 1, 返回 0),类似于 i++
    System.out.println(i.getAndIncrement());
    // 自增并获取(i = 1, 结果 i = 2, 返回 2),类似于 ++i
    System.out.println(i.incrementAndGet());
    // 自减并获取(i = 2, 结果 i = 1, 返回 1),类似于 --i
    System.out.println(i.decrementAndGet());
    // 获取并自减(i = 1, 结果 i = 0, 返回 1),类似于 i--
    System.out.println(i.getAndDecrement());
    // 获取并加值(i = 0, 结果 i = 5, 返回 0)
    System.out.println(i.getAndAdd(5));
    // 加值并获取(i = 5, 结果 i = 0, 返回 0)
    System.out.println(i.addAndGet(-5));
    // 获取并更新(i = 0, p 为 i 的当前值, 结果 i = -2, 返回 0)
    // 其中函数中的操作能保证原子,但函数需要无副作用
    System.out.println(i.getAndUpdate(p -> p - 2));
    // 更新并获取(i = -2, p 为 i 的当前值, 结果 i = 0, 返回 0)
    // 其中函数中的操作能保证原子,但函数需要无副作用
    System.out.println(i.updateAndGet(p -> p + 2));
    // 获取并计算(i = 0, p 为 i 的当前值, x 为参数1, 结果 i = 10, 返回 0)
    // 其中函数中的操作能保证原子,但函数需要无副作用
    // getAndUpdate 如果在 lambda 中引用了外部的局部变量,要保证该局部变量是 final 的
    // getAndAccumulate 可以通过 参数1 来引用外部的局部变量,但因为其不在 lambda 中因此不必是 final
    System.out.println(i.getAndAccumulate(10, (p, x) -> p + x));
    // 计算并获取(i = 10, p 为 i 的当前值, x 为参数1, 结果 i = 0, 返回 0)
    // 其中函数中的操作能保证原子,但函数需要无副作用
    System.out.println(i.accumulateAndGet(-10, (p, x) -> p + x));
    
    • 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

    5.4原子引用

    • AtomicReference

    • AtomicMarkableReference

    • AtomicStampedReference

    public interface DecimalAccount {
            // 获取余额
            BigDecimal getBalance();
            // 取款
            void withdraw(BigDecimal amount);
            /**
             * 方法内会启动 1000 个线程,每个线程做 -10 元 的操作
             * 如果初始余额为 10000 那么正确的结果应当是 0
             */
            static void demo(DecimalAccount account) {
                List<Thread> ts = new ArrayList<>();
                for (int i = 0; i < 1000; i++) {
                    ts.add(new Thread(() -> {
                        account.withdraw(BigDecimal.TEN);
                    }));
                }
                ts.forEach(Thread::start);
                ts.forEach(t -> {
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
                System.out.println(account.getBalance());
            }
        }
    
    • 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
    提供不同的 DecimalAccount 实现,实现安全的取款操作
    • 使用锁
    class DecimalAccountSafeLock implements DecimalAccount {
        private final Object lock = new Object();
        BigDecimal balance;
        public DecimalAccountSafeLock(BigDecimal balance) {
            this.balance = balance;
        }
        @Override
        public BigDecimal getBalance() {
            return balance;
        }
        @Override
        public void withdraw(BigDecimal amount) {
            synchronized (lock) {
                BigDecimal balance = this.getBalance();
                this.balance = balance.subtract(amount);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 使用CAS
    class DecimalAccountSafeCas implements DecimalAccount {
        AtomicReference<BigDecimal> ref;
        public DecimalAccountSafeCas(BigDecimal balance) {
            ref = new AtomicReference<>(balance);
        }
        @Override
        public BigDecimal getBalance() {
            return ref.get();
        }
        @Override
        public void withdraw(BigDecimal amount) {
            while (true) {
                BigDecimal prev = ref.get();
                BigDecimal next = prev.subtract(amount);
                if (ref.compareAndSet(prev, next)) {
                    break;
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    out:

    0 cost: 285 ms 
    0 cost: 274 ms
    
    • 1
    • 2
    ABA问题解决:
        static AtomicReference<String> ref = new AtomicReference<>("A");
        public static void main(String[] args) throws InterruptedException {
            log.debug("main start...");
            // 获取值 A
            // 这个共享变量被它线程修改过?
            String prev = ref.get();
            other();
            sleep(1);
            // 尝试改为 C
            log.debug("change A->C {}", ref.compareAndSet(prev, "C"));
        }
        private static void other() {
            new Thread(() -> {
                log.debug("change A->B {}", ref.compareAndSet(ref.get(), "B"));
            }, "t1").start();
            sleep(0.5);
            new Thread(() -> {
                log.debug("change B->A {}", ref.compareAndSet(ref.get(), "A"));
            }, "t2").start();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    out:

    11:29:52.325 c.Test36 [main] - main start... 
    11:29:52.379 c.Test36 [t1] - change A->B true 
    11:29:52.879 c.Test36 [t2] - change B->A true 
    11:29:53.880 c.Test36 [main] - change A->C true
    
    • 1
    • 2
    • 3
    • 4

    主线程仅能判断出共享变量的值与最初值 A 是否相同,不能感知到这种从 A 改为 B 又 改回 A 的情况,如果主线程

    希望:

    只要有其它线程【动过了】共享变量,那么自己的 cas 就算失败,这时,仅比较值是不够的,需要再加一个版本号

    AtomicStampedReference
        static AtomicStampedReference<String> ref = new AtomicStampedReference<>("A", 0);
        public static void main(String[] args) throws InterruptedException {
            log.debug("main start...");
            // 获取值 A
            String prev = ref.getReference();
            // 获取版本号
            int stamp = ref.getStamp();
            log.debug("版本 {}", stamp);
            // 如果中间有其它线程干扰,发生了 ABA 现象
            other();
            sleep(1);
            // 尝试改为 C
            log.debug("change A->C {}", ref.compareAndSet(prev, "C", stamp, stamp + 1));
        }
        private static void other() {
            new Thread(() -> {
                log.debug("change A->B {}", ref.compareAndSet(ref.getReference(), "B",
                        ref.getStamp(), ref.getStamp() + 1));
                log.debug("更新版本为 {}", ref.getStamp());
            }, "t1").start();
            sleep(0.5);
            new Thread(() -> {
                log.debug("change B->A {}", ref.compareAndSet(ref.getReference(), "A",
                        ref.getStamp(), ref.getStamp() + 1));
                log.debug("更新版本为 {}", ref.getStamp());
            }, "t2").start();
        }
        
    
    • 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
    15:41:34.891 c.Test36 [main] - main start... 
    15:41:34.894 c.Test36 [main] - 版本 0 
    15:41:34.956 c.Test36 [t1] - change A->B true 
    15:41:34.956 c.Test36 [t1] - 更新版本为 1 
    15:41:35.457 c.Test36 [t2] - change B->A true 
    15:41:35.457 c.Test36 [t2] - 更新版本为 2 
    15:41:36.457 c.Test36 [main] - change A->C false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    AtomicStampedReference 可以给原子引用加上版本号,追踪原子引用整个的变化过程,如: A -> B -> A ->C,通过AtomicStampedReference,我们可以知道,引用变量中途被更改了几次。

    但是有时候,并不关心引用变量更改了几次,只是单纯的关心是否更改过,所以就有了

    AtomicMarkableReference

    AtomicMarkableReference
    class GarbageBag {
        String desc;
        public GarbageBag(String desc) {
            this.desc = desc;
        }
        public void setDesc(String desc) {
            this.desc = desc;
        }
        @Override
        public String toString() {
            return super.toString() + " " + desc;
        }
    }
    @Slf4j
    public class TestABAAtomicMarkableReference {
        public static void main(String[] args) throws InterruptedException {
            GarbageBag bag = new GarbageBag("装满了垃圾");
            // 参数2 mark 可以看作一个标记,表示垃圾袋满了
            AtomicMarkableReference<GarbageBag> ref = new AtomicMarkableReference<>(bag, true);
            log.debug("主线程 start...");
            GarbageBag prev = ref.getReference();
            log.debug(prev.toString());
            new Thread(() -> {
                log.debug("打扫卫生的线程 start...");
                bag.setDesc("空垃圾袋");
                while (!ref.compareAndSet(bag, bag, true, false)) {}
                log.debug(bag.toString());
            }).start();
            Thread.sleep(1000);
            log.debug("主线程想换一只新垃圾袋?");
            boolean success = ref.compareAndSet(prev, new GarbageBag("空垃圾袋"), true, false);
            log.debug("换了么?" + success);
            log.debug(ref.getReference().toString());
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35

    out.

    2019-10-13 15:30:09.264 [main] 主线程 start... 
    2019-10-13 15:30:09.270 [main] cn.itcast.GarbageBag@5f0fd5a0 装满了垃圾
    2019-10-13 15:30:09.293 [Thread-1] 打扫卫生的线程 start... 
    2019-10-13 15:30:09.294 [Thread-1] cn.itcast.GarbageBag@5f0fd5a0 空垃圾袋
    2019-10-13 15:30:10.294 [main] 主线程想换一只新垃圾袋?
    2019-10-13 15:30:10.294 [main] 换了么?false 
    2019-10-13 15:30:10.294 [main] cn.itcast.GarbageBag@5f0fd5a0 空垃圾袋
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.5 原子数组

    • AtomicIntegerArray

    • AtomicLongArray

    • AtomicReferenceArray

    /**
         参数1,提供数组、可以是线程不安全数组或线程安全数组
         参数2,获取数组长度的方法
         参数3,自增方法,回传 array, index
         参数4,打印数组的方法
         */
        // supplier 提供者 无中生有  ()->结果
        // function 函数   一个参数一个结果   (参数)->结果  ,  BiFunction (参数1,参数2)->结果
        // consumer 消费者 一个参数没结果  (参数)->void,      BiConsumer (参数1,参数2)->
        private static <T> void demo(
                Supplier<T> arraySupplier,
                Function<T, Integer> lengthFun,
                BiConsumer<T, Integer> putConsumer,
                Consumer<T> printConsumer ) {
            List<Thread> ts = new ArrayList<>();
            T array = arraySupplier.get();
            int length = lengthFun.apply(array);
            for (int i = 0; i < length; i++) {
                // 每个线程对数组作 10000 次操作
                ts.add(new Thread(() -> {
                    for (int j = 0; j < 10000; j++) {
                        putConsumer.accept(array, j%length);
                    }
                }));
            }
    
            ts.forEach(t -> t.start()); // 启动所有线程
            ts.forEach(t -> {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });     // 等所有线程结束
            printConsumer.accept(array);
        }
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    UnsafeArray
    demo(
     ()->new int[10],
     (array)->array.length,
     (array, index) -> array[index]++,
     array-> System.out.println(Arrays.toString(array))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    out.

    [9870, 9862, 9774, 9697, 9683, 9678, 9679, 9668, 9680, 9698]
    
    • 1
    SafeArray
    demo(
     ()-> new AtomicIntegerArray(10),
     (array) -> array.length(),
     (array, index) -> array.getAndIncrement(index),
     array -> System.out.println(array)
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    out.

    [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000]
    
    • 1

    5.6字段更新器

    • AtomicReferenceFieldUpdater // 域 字段

    • AtomicIntegerFieldUpdater

    • AtomicLongFieldUpdater

    利用字段更新器,可以针对对象的某个域(Field)进行原子操作,只能配合 volatile 修饰的字段使用,否则会出现

    异常

    Exception in thread "main" java.lang.IllegalArgumentException: Must be volatile type
    
    • 1
    public class Test5 {
        private volatile int field;
        public static void main(String[] args) {
            AtomicIntegerFieldUpdater fieldUpdater =AtomicIntegerFieldUpdater.newUpdater(Test5.class, "field");
            Test5 test5 = new Test5();
            fieldUpdater.compareAndSet(test5, 0, 10);
            // 修改成功 field = 10
            System.out.println(test5.field);
            // 修改成功 field = 20
            fieldUpdater.compareAndSet(test5, 10, 20);
            System.out.println(test5.field);
            // 修改失败 field = 20
            fieldUpdater.compareAndSet(test5, 10, 30);
            System.out.println(test5.field);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    out.

    10 
    20 
    20
    
    • 1
    • 2
    • 3

    5.7原子累加器(**)

    他的设计模式挺重要的,并且并发效率非常高

    累加器性能比较
    private static <T> void demo(Supplier<T> adderSupplier, Consumer<T> action) {
            T adder = adderSupplier.get();
            long start = System.nanoTime();
            List<Thread> ts = new ArrayList<>();
            // 4 个线程,每人累加 50 万
            for (int i = 0; i < 40; i++) {
                ts.add(new Thread(() -> {
                    for (int j = 0; j < 500000; j++) {
                        action.accept(adder);
                    }
                }));
            }
            ts.forEach(t -> t.start());
            ts.forEach(t -> {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            long end = System.nanoTime();
            System.out.println(adder + " cost:" + (end - start)/1000_000);
        }
        
        // 比较 AtomicLong 与 LongAdder
        for (int i = 0; i < 5; i++) {
     		demo(() -> new LongAdder(), adder -> adder.increment());
    	}
    	for (int i = 0; i < 5; i++) {
    	 	demo(() -> new AtomicLong(), adder -> adder.getAndIncrement());
    	}
    
    • 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

    out.

    1000000 cost:43 
    1000000 cost:9 
    1000000 cost:7 
    1000000 cost:7 
    1000000 cost:7 
    1000000 cost:31 
    1000000 cost:27 
    1000000 cost:28 
    1000000 cost:24 
    1000000 cost:22
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    性能提升的原因很简单,就是在有竞争时,设置多个累加单元,Therad-0 累加 Cell[0],而 Thread-1 累加

    Cell[1]… 最后将结果汇总。这样它们在累加时操作的不同的 Cell 变量,因此减少了 CAS 重试失败,从而提高性

    能。

    源码解析

    高并发并发下N多线程同时去操作一个变量会造成大量线程CAS失败,然后处于自旋状态,导致严重浪费CPU资源,降低了并发性。既然AtomicLong性能问题是由于过多线程同时去竞争同一个变量的更新而降低的,那么如果把一个变量分解为多个变量,让同样多的线程去竞争多个资源。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gf20BVxv-1667462986361)(assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW5ndGlhbmppYW8=,size_16,color_FFFFFF,t_70.png)]

    基础属性

    // 累加单元数组, 懒惰初始化
    transient volatile Cell[] cells;
    // 基础值, 如果没有竞争, 则用 cas 累加这个域
    transient volatile long base;
    // 在 cells 创建或扩容时, 置为 1, 表示加锁
    transient volatile int cellsBusy;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    cells数组是LongAdder高性能实现的必杀器: AtomicInteger只有一个value,所有线程累加都要通过cas竞争value这一个变量,高并发下线程争用非常严重
    而LongAdder则有两个值用于累加,一个是base,它的作用类似于AtomicInteger里面的value,在没有竞争的情况不会用到cells数组,这时使用base做累加,有了竞争后cells数组就上场了,第一次初始化长度为2,以后每次扩容都是变为原来的两倍,直到cells数组的长度大于等于当前服务器cpu的数量为止就不在扩容(CPU能够并行的CAS操作的最大数量是它的核心数),每个线程会通过线程对cells[threadLocalRandomProbe%cells.length]位置的Cell对象中的value做累加,这样相当于将线程绑定到了cells中的某个cell对象上。

     // 为提高性能,使用注解@sun.misc.Contended,用来避免伪共享
     // 伪共享简单来说就是会破坏其它线程在缓存行中的值,导致重新从主内存读取,降低性能。
     @sun.misc.Contended static final class Cell {
            //用来保存要累加的值
            volatile long value;
            Cell(long x) { value = x; }
            //使用UNSAFE类的cas来更新value值
            final boolean cas(long cmp, long val) {
                return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);
            }
            private static final sun.misc.Unsafe UNSAFE;
            //value在Cell类中存储位置的偏移量;
            private static final long valueOffset;
            //这个静态方法用于获取偏移量
            static {
                try {
                    UNSAFE = sun.misc.Unsafe.getUnsafe();
                    Class<?> ak = Cell.class;
                    valueOffset = UNSAFE.objectFieldOffset
                        (ak.getDeclaredField("value"));
                } catch (Exception e) {
                    throw new Error(e);
                }
            }
        }
    
    • 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

    成员变量cellsBusy

    cellsBusy作用是当要修改cells数组时加锁,防止多线程同时修改cells数组,0为无锁,1为加锁,加锁的状况有三种

    • cells数组初始化的时候
    • cells数组扩容的时候
    • 如果cells数组中某个元素为null,给这个位置创建新的Cell对象的时候

    成员变量base
    它有两个作用:

    • 在开始没有竞争的情况下,将累加值累加到base
    • 在cells初始化的过程中,cells不可用,这时会尝试将值累加到base上

    LongAdder

    add()流程

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m2Z5FPe3-1667462986362)(assets/image-20221101193056787.png)]

    longAccumulate()流程

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EF4mLhLp-1667462986363)(assets/image-20221101193145039.png)]

    创建Cell流程

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fEsaTMcJ-1667462986365)(assets/image-20221101194113023.png)]

    进入longAccumulate()获取线程对应的Cell单元

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LmWKcnWh-1667462986366)(assets/image-20221101194238510.png)]

    // 累加方法,参数x为累加的值    
    public void add(long x) {
            Cell[] as; long b, v; int m; Cell a;
            /**
             * 如果一下两种条件则继续执行if内的语句
             * 1. cells数组不为null(不存在争用的时候,cells数组一定为null,一旦对base的cas操作失败,才会初始化cells数组)
             * 2. 如果cells数组为null,如果casBase执行成功,则直接返回,如果casBase方法执行失败(casBase失败,说明第一次争用冲突产生,需要对cells数组初始化)进入if内;
             * casBase方法很简单,就是通过UNSAFE类的cas设置成员变量base的值为base+要累加的值
             * casBase执行成功的前提是无竞争,这时候cells数组还没有用到为null,可见在无竞争的情况下是类似于AtomticInteger处理方式,使用cas做累加。
             */
            if ((as = cells) != null || !casBase(b = base, b + x)) {
                //uncontended判断cells数组中,当前线程要做cas累加操作的某个元素是否#不#存在争用,如果cas失败则存在争用;uncontended=false代表存在争用,uncontended=true代表不存在争用。
                boolean uncontended = true;
                /**
                *1. as == null : cells数组未被初始化,成立则直接进入if执行cell初始化
                *2. (m = as.length - 1) < 0: cells数组的长度为0
                *条件1与2都代表cells数组没有被初始化成功,初始化成功的cells数组长度为2;
                *3. (a = as[getProbe() & m]) == null :如果cells被初始化,且它的长度不为0,则通过getProbe方法获取当前线程Thread的threadLocalRandomProbe变量的值,初始为0,然后执行threadLocalRandomProbe&(cells.length-1 ),相当于m%cells.length;如果cells[threadLocalRandomProbe%cells.length]的位置为null,这说明这个位置从来没有线程做过累加,需要进入if继续执行,在这个位置创建一个新的Cell对象;
                *4. !(uncontended = a.cas(v = a.value, v + x)):尝试对cells[threadLocalRandomProbe%cells.length]位置的Cell对象中的value值做累加操作,并返回操作结果,如果失败了则进入if,重新计算一个threadLocalRandomProbe;
                如果进入if语句执行longAccumulate方法,有三种情况
                1. 前两个条件代表cells没有初始化,
                2. 第三个条件指当前线程hash到的cells数组中的位置还没有其它线程做过累加操作,
                3. 第四个条件代表产生了冲突,uncontended=false
                **/
                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);
            }
        }
     
    final void longAccumulate(long x, LongBinaryOperator fn, boolean wasUncontended) {
            //获取当前线程的threadLocalRandomProbe值作为hash值,如果当前线程的threadLocalRandomProbe为0,说明当前线程是第一次进入该方法,则强制设置线程的threadLocalRandomProbe为ThreadLocalRandom类的成员静态私有变量probeGenerator的值,后面会详细将hash值的生成;
            //另外需要注意,如果threadLocalRandomProbe=0,代表新的线程开始参与cell争用的情况
            //1.当前线程之前还没有参与过cells争用(也许cells数组还没初始化,进到当前方法来就是为了初始化cells数组后争用的),是第一次执行base的cas累加操作失败;
            //2.或者是在执行add方法时,对cells某个位置的Cell的cas操作第一次失败,则将wasUncontended设置为false,那么这里会将其重新置为true;第一次执行操作失败;
           //凡是参与了cell争用操作的线程threadLocalRandomProbe都不为0;
            int h;
            if ((h = getProbe()) == 0) {
                //初始化ThreadLocalRandom;
                ThreadLocalRandom.current(); // force initialization
                //将h设置为0x9e3779b9
                h = getProbe();
                //设置未竞争标记为true
                wasUncontended = true;
            }
            //cas冲突标志,表示当前线程hash到的Cells数组的位置,做cas累加操作时与其它线程发生了冲突,cas失败;collide=true代表有冲突,collide=false代表无冲突 
            boolean collide = false; 
            for (;;) {
                Cell[] as; Cell a; int n; long v;
                //这个主干if有三个分支
                //1.主分支一:处理cells数组已经正常初始化了的情况(这个if分支处理add方法的四个条件中的3和4)
                //2.主分支二:处理cells数组没有初始化或者长度为0的情况;(这个分支处理add方法的四个条件中的1和2)
                //3.主分支三:处理如果cell数组没有初始化,并且其它线程正在执行对cells数组初始化的操作,及cellbusy=1;则尝试将累加值通过cas累加到base上
                //先看主分支一
                if ((as = cells) != null && (n = as.length) > 0) {
                    /**
                     *内部小分支一:这个是处理add方法内部if分支的条件3:如果被hash到的位置为null,说明没有线程在这个位置设置过值,没有竞争,可以直接使用,则用x值作为初始值创建一个新的Cell对象,对cells数组使用cellsBusy加锁,然后将这个Cell对象放到cells[m%cells.length]位置上 
                     */
                    if ((a = as[(n - 1) & h]) == null) {
                        //cellsBusy == 0 代表当前没有线程cells数组做修改
                        if (cellsBusy == 0) {
                            //将要累加的x值作为初始值创建一个新的Cell对象,
                            Cell r = new Cell(x); 
                            //如果cellsBusy=0无锁,则通过cas将cellsBusy设置为1加锁
                            if (cellsBusy == 0 && casCellsBusy()) {
                                //标记Cell是否创建成功并放入到cells数组被hash的位置上
                                boolean created = false;
                                try {
                                    Cell[] rs; int m, j;
                                    //再次检查cells数组不为null,且长度不为空,且hash到的位置的Cell为null
                                    if ((rs = cells) != null &&
                                        (m = rs.length) > 0 &&
                                        rs[j = (m - 1) & h] == null) {
                                        //将新的cell设置到该位置
                                        rs[j] = r;
                                        created = true;
                                    }
                                } finally {
                                    //去掉锁
                                    cellsBusy = 0;
                                }
                                //生成成功,跳出循环
                                if (created)
                                    break;
                                //如果created为false,说明上面指定的cells数组的位置cells[m%cells.length]已经有其它线程设置了cell了,继续执行循环。
                                continue;
                            }
                        }
                       //如果执行的当前行,代表cellsBusy=1,有线程正在更改cells数组,代表产生了冲突,将collide设置为false
                        collide = false;
     
                    /**
                     *内部小分支二:如果add方法中条件4的通过cas设置cells[m%cells.length]位置的Cell对象中的value值设置为v+x失败,说明已经发生竞争,将wasUncontended设置为true,跳出内部的if判断,最后重新计算一个新的probe,然后重新执行循环;
                     */
                    } else if (!wasUncontended)  
                        //设置未竞争标志位true,继续执行,后面会算一个新的probe值,然后重新执行循环。 
                        wasUncontended = true;
                    /**
                    *内部小分支三:新的争用线程参与争用的情况:处理刚进入当前方法时threadLocalRandomProbe=0的情况,也就是当前线程第一次参与cell争用的cas失败,这里会尝试将x值加到cells[m%cells.length]的value ,如果成功直接退出  
                    */
                    else if (a.cas(v = a.value, ((fn == null) ? v + x :
                                                 fn.applyAsLong(v, x))))
                        break;
                    /**
                     *内部小分支四:分支3处理新的线程争用执行失败了,这时如果cells数组的长度已经到了最大值(大于等于cup数量),或者是当前cells已经做了扩容,则将collide设置为false,后面重新计算prob的值*/
                    else if (n >= NCPU || cells != as)
                        collide = false;
                    /**
                     *内部小分支五:如果发生了冲突collide=false,则设置其为true;会在最后重新计算hash值后,进入下一次for循环
                     */
                    else if (!collide)
                        //设置冲突标志,表示发生了冲突,需要再次生成hash,重试。 如果下次重试任然走到了改分支此时collide=true,!collide条件不成立,则走后一个分支
                        collide = true;
                    /**
                     *内部小分支六:扩容cells数组,新参与cell争用的线程两次均失败,且符合库容条件,会执行该分支
                     */
                    else if (cellsBusy == 0 && casCellsBusy()) {
                        try {
                            //检查cells是否已经被扩容
                            if (cells == as) {      // Expand table unless stale
                                Cell[] rs = new Cell[n << 1];
                                for (int i = 0; i < n; ++i)
                                    rs[i] = as[i];
                                cells = rs;
                            }
                        } finally {
                            cellsBusy = 0;
                        }
                        collide = false;
                        continue;                   // Retry with expanded table
                    }
                    //为当前线程重新计算hash值
                    h = advanceProbe(h);
     
                //这个大的分支处理add方法中的条件1与条件2成立的情况,如果cell表还未初始化或者长度为0,先尝试获取cellsBusy锁。
                }else if (cellsBusy == 0 && cells == as && casCellsBusy()) {
                    boolean init = false;
                    try {  // Initialize table
                        //初始化cells数组,初始容量为2,并将x值通过hash&1,放到0个或第1个位置上
                        if (cells == as) {
                            Cell[] rs = new Cell[2];
                            rs[h & 1] = new Cell(x);
                            cells = rs;
                            init = true;
                        }
                    } finally {
                        //解锁
                        cellsBusy = 0;
                    }
                    //如果init为true说明初始化成功,跳出循环
                    if (init)
                        break;
                }
                /**
                 *如果以上操作都失败了,则尝试将值累加到base上;
                 */
                else if (casBase(v = base, ((fn == null) ? v + x : fn.applyAsLong(v, x)))) // Fall back on using base
                    break;  
            }
        }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161

    sum获取结果

    public long sum() {
     Cell[] as = cells; Cell a;
     long sum = base;
     if (as != null) {
     for (int i = 0; i < as.length; ++i) {
     if ((a = as[i]) != null)
     	sum += a.value;
     	}
     }
     return sum;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    hash生成策略

    • hash决定了当前线程将累加值定位到哪个cell中,hash算法尤其重要。
    • hash就是java的Thread类里面有一个成员变量,初始值为0。
    /** Probe hash value; nonzero if threadLocalRandomSeed initialized */
    @sun.misc.Contended("tlr") 
    int threadLocalRandomProbe;
     
    // LongAdder的父类Striped64里通过getProbe方法获取当前线程threadLocalRandomProbe
    static final int getProbe() {
        // PROBE是threadLocalRandomProbe变量在Thread类里面的偏移量
        return UNSAFE.getInt(Thread.currentThread(), PROBE);
    }
     
    // threadLocalRandomProbe初始化
    // 线程对LongAdder的累加操作,在没有进入longAccumulate方法前,threadLocalRandomProbe一直都是0,当发生争用后才会进入longAccumulate方法中,进入该方法第一件事就是判断threadLocalRandomProbe是否为0,如果为0,则将其设置为0x9e3779b9
    int h;
    if ((h = getProbe()) == 0) {
       ThreadLocalRandom.current(); 
       h = getProbe();
       //设置未竞争标记为true
       wasUncontended = true;
    }
     
    static final void localInit() {
       // private static final AtomicInteger probeGenerator = new AtomicInteger();
       // private static final int PROBE_INCREMENT = 0x9e3779b9;
       int p = probeGenerator.addAndGet(PROBE_INCREMENT);
       int probe = (p == 0) ? 1 : p; // skip 0
       long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
       Thread t = Thread.currentThread();
       UNSAFE.putLong(t, SEED, seed);
       UNSAFE.putInt(t, PROBE, probe);
    }
     
    threadLocalRandomProbe重新生成
    static final int advanceProbe(int probe) {
       probe ^= probe << 13;   // xorshift
       probe ^= probe >>> 17;
       probe ^= probe << 5;
       UNSAFE.putInt(Thread.currentThread(), PROBE, probe);
       return probe;
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    5.8false sharing

    主内存RAM是数据存在的地方,CPU和主内存之间有好几级缓存,因为即使直接访问主内存相对来说也是非常慢的。

    如果对一块数据做相同的运算多次,那么在执行运算的时候把它加载到离CPU很近的地方就有意义了,比如一个循环计数,不会每次循环都到主内存中去取这个数据

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AMrgm9Ft-1667462986367)(assets/format,png.png)]

    当CPU执行运算的时候,它先去L1查找所需的数据,再去L2,然后L3,最后如果这些缓存中都没有,所需的数据就要去主内存拿。

    走得越远,运算耗费的时间就越长。所以如果进行一些很频繁的运算,要确保数据在L1缓存中。

    缓存行

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bsB4iOSi-1667462986367)(assets/format,png-16673020220207.png)]

    在程序运行的过程中,缓存每次更新都从主内存中加载连续的64个字节。因此,如果访问一个long类型的数组时,当数组中的一个值被加载到缓存中时,另外7个元素也会被加载到缓存中。但是,如果使用的数据结构中的项在内存中不是彼此相邻的,比如链表,那么将得不到免费缓存加载带来的好处。

    不过,这种免费加载也有一个坏处。设想如果我们有个long类型的变量a,它不是数组的一部分,而是一个单独的变量,并且还有另外一个long类型的变量b紧挨着它,那么当加载a的时候将免费加载b。

    看起来似乎没有什么问题,但是如果一个cpu核心的线程在对a进行修改,另一个cpu核心的线程却在对b进行读取。当前者修改a时,会把a和b同时加载到前者核心的缓存行中,更新完a后其它所有包含a的缓存行都将失效,因为其它缓存中的a不是最新值了。而当后者读取b时,发现这个缓存行已经失效了,需要从主内存中重新加载。

    请记着,我们的缓存都是以缓存行作为一个单位来处理的,所以失效a的缓存的同时,也会把b失效,反之亦然。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Lv448KO-1667462986368)(assets/format,png-166730217020410.png)]

    public final class Constants {
    
        static class Pointer {
            volatile long x;
            volatile long y;
        }
    
        public static void main(String[] args) throws InterruptedException {
            testPointer();
        }
    
        private static void testPointer(Pointer pointer) throws InterruptedException {
            long start = System.currentTimeMillis();
            Thread t1 = new Thread(() -> {
                for (int i = 0; i < 100000000; i++) {
                    pointer.x++;
                }
            });
    
            Thread t2 = new Thread(() -> {
                for (int i = 0; i < 100000000; i++) {
                    pointer.y++;
                }
            });
    
            t1.start();
            t2.start();
            t1.join();
            t2.join();
    
            System.out.println(System.currentTimeMillis() - start);
            System.out.println(pointer);
        }
    
        private static void testPointer() throws InterruptedException {
            long start = System.currentTimeMillis();
            Thread t1 = new Thread(() -> {
                int x1 = 0;
                for (int i = 0; i < 100000000; i++) {
                    x1++;
                }
            });
    
            Thread t2 = new Thread(() -> {
                int y1 = 0;
                for (int i = 0; i < 100000000; i++) {
                    y1++;
                }
            });
    
            t1.start();
            t2.start();
            t1.join();
            t2.join();
    
            System.out.println(System.currentTimeMillis() - start);
        }
    
    
    
    
    }
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    可以发现效率会大大提高

    解决方法:
    • @sun.misc.Contended 用来解决这个问题,它的原理是在使用此注解的对象或字段的前后各增加 128 字节大小的

      padding,从而让 CPU 将对象预读至缓存时占用不同的缓存行,这样,不会造成对方缓存行的失效

    值得注意的是:Cell单元就是这么做的

    • 两个long类型的变量之间再加7个long类型,凑成一个缓冲行

    5.8Unsafe

    Unsafe 对象提供了非常底层的,操作内存、线程的方法,Unsafe 对象不能直接调用,只能通过反射获得

    public class UnsafeAccessor {
            static Unsafe unsafe;
            static {
                try {
                    Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
                    theUnsafe.setAccessible(true);
                    unsafe = (Unsafe) theUnsafe.get(null);
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    throw new Error(e);
                }
            }
            static Unsafe getUnsafe() {
                return unsafe;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Unsafe CAS 操作

    @Data
    class Student {
     volatile int id;
     volatile String name;
    }
    
    
    Unsafe unsafe = UnsafeAccessor.getUnsafe();
    Field id = Student.class.getDeclaredField("id");
    Field name = Student.class.getDeclaredField("name");
    // 获得成员变量的偏移量
    long idOffset = UnsafeAccessor.unsafe.objectFieldOffset(id);
    long nameOffset = UnsafeAccessor.unsafe.objectFieldOffset(name);
    Student student = new Student();
    // 使用 cas 方法替换成员变量的值
    UnsafeAccessor.unsafe.compareAndSwapInt(student, idOffset, 0, 20); // 返回 true
    UnsafeAccessor.unsafe.compareAndSwapObject(student, nameOffset, null, "张三"); // 返回 true
    System.out.println(student);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    系统篇: ubuntu 下 sudo 命令执行慢的解决办法
    离线数仓(1):什么是数据仓库
    Dapper迁移SqlSugar问题汇总
    laravel 发生异常时回滚数据库变化
    XML简单介绍
    C++ 语言学习 day06 string , 异常
    GEE——使用MODIS GPP和LAI数据进行一元线性回归计算和R2分析
    【Java 进阶篇】MySQL外键约束详解
    Centos7 安装KVM
    论文超详细精读|万字:2s-AGCN
  • 原文地址:https://blog.csdn.net/qq_57115378/article/details/127672566