深入解析J.U.C并发包(二)——AtomicInteger类

本文深入解析了AtomicInteger类的内部实现,包括如何利用Unsafe类进行线程安全的整数操作,以及getAndSet、getAndIncrement等核心方法的工作原理。同时对比了set与lazySet方法在性能上的差异,阐述了在特定场景下使用lazySet的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、AtomicInteger简介

AtomicInteger是Integer类型的线程安全原子类,可以在应用程序以原子的方式更新int值。

1.创建AtomicInteger对象

// setup to use Unsafe.compareAndSwapInt for updates
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long valueOffset;

    static {
        try {
            // 获取value属性的偏移量
            valueOffset = unsafe.objectFieldOffset
                (AtomicInteger.class.getDeclaredField("value"));
        } catch (Exception ex) { throw new Error(ex); }
    }

    private volatile int value;

    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

    public AtomicInteger() {
    }

2.AtomicInteger的使用

    /**
     * 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);
    }

    public final int getAndSetInt(Object 0, long offset, int newValue) {
        int v;
        do {
            // 根据偏移量获取内存中原来field的值
            v = this.getIntVolatile(0, offset);
        } while(!this.compareAndSwapInt(0, offset, v, newValue));
          // 如果还是v,则设置为newValue,否则返回false,重新获取新的v值,再比较,直到成功为止。
        return v;
    }
    public final int getAndIncrement() {
        return unsafe.getAndAddInt(this, valueOffset, 1);
    }

    public final int getAndAddInt(Object o, long offset, int delta) {
        int v;
        do {
            v = this.getIntVolatile(o, offset);
        } while(!this.compareAndSwapInt(o, offset, v, v + delta));

        return v;
    }

3、AtomicInteger特殊方法

    public final void set(int newValue) {
        value = newValue;
    }

    public final void lazySet(int newValue) {
        unsafe.putOrderedInt(this, valueOffset, newValue);
    }

我们知道通过volatile修饰的变量,可以保证在多处理器环境下 “可见性”。也就是说当一个线程修改一个共享变量时,其他线程能立即读到这个修改的值。volatile的实现最终是加了内存屏障:

内存屏障分为两种:Load Barrier 和 Store Barrier 即读屏障和写屏障

作用:

1、阻止屏障两侧的指令重排序;

2、强制把写缓冲区/高速缓存中脏数据等写回主存,让缓存中相应的数据失效。

 volatile的写为了保证对其他线程的可见性会追加以下两个Fence(内存屏障)。

1)Storestore

2)StoreLoad //最耗性能

For people who like to think of these operations in terms of machine-level barriers on common multiprocessors, 
lazySet provides a preceeding store-store barrier (which is either a no-op or very cheap on current platforms), 
but no store-load barrier (which is usually the expensive part of a volatile-write)."

测试代码: 

// set()
public class LazySetTest {
    private static final AtomicLong a = new AtomicLong();

    public static void main(String[] args) {
        for (int i = 0; i < 100000000; i++) {
            a.set(i);
        }
    }
}

// lazySet()
public class LazySetTest {
    private static final AtomicLong a = new AtomicLong();

    public static void main(String[] args) {
        for (int i = 0; i < 100000000; i++) {
            a.lazySet(i);
        }
    }
}

 汇编:

// set()的assembly code片段:
0x000000010ccbfeb3: mov    %r10,0x10(%r9)
0x000000010ccbfeb7: lock addl $0x0,(%rsp)     ;*putfield value
                                            ; - java.util.concurrent.atomic.AtomicLong::set@2 (line 112)
                                            ; - LazySetTest::main@13 (line 13)
0x000000010ccbfebc: inc    %ebp               ;*iinc
                                            ; - LazySetTest::main@16 (line 12)
// ------------------------------------------------------
// lazySet()的assembly code片段:
0x0000000108766faf: mov    %r10,0x10(%rcx)    ;*invokevirtual putOrderedLong
                                            ; - java.util.concurrent.atomic.AtomicLong::lazySet@8 (line 122)
                                            ; - LazySetTest::main@13 (line 13)
0x0000000108766fb3: inc    %ebp               ;*iinc
                                            ; - LazySetTest::main@16 (line 12)

lock addl $0x0,(%rsp) 实际上就是StoreLoad屏障。

为什么使用lazySet?

private AtomicInteger ai = new AtomicInteger();
lock.lock();
try
{
     ai.set(1);
}
finally
{
    lock.unlock();
}

由于锁的存在:

1、lock()方法获取锁时,和volatile变量的读操作一样,会强制使CPU缓存失效,强制从内存读取变量。

2、unlock()方法释放锁时,和volatile变量的写操作一样,会强制刷新CPU写缓存区,把缓存数据写到主内存。

所以,上述的set方法可以用lazySet方法代替。

由锁来保证共享变量的可见性, 以设置普通变量的方式来修改共享变量,减少不必要的内存屏障,从而提高程序执行的效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值