4-2 线程安全性-原子性-atomic-2

AtomicReference和AtomicLong、AtomicInteger很像,方法也基本上是一样的,然后我们通过引用Integer来做一个简单的例子。

com.mmall.concurrency.example.atomic.AtomicExample4

C:\Users\ZHONGZHENHUA\imooc\concurrency\src\main\java\com\mmall\concurrency\example\atomic\AtomicExample4.java

package com.mmall.concurrency.example.atomic;

import com.mmall.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@ThreadSafe
public class AtomicExample4 {
    private static AtomicReference<Integer> count = new AtomicReference<>(0);

    public static void main(String args[]){
        count.compareAndSet(0,2);  // 2
        count.compareAndSet(0,1);  // no
        count.compareAndSet(1,3);  // no
        count.compareAndSet(2,4);  // 4
        count.compareAndSet(3,5);  // no
        log.info("count:{}",count.get());

    }
}

猜你喜欢

转载自www.cnblogs.com/ZHONGZHENHUA/p/10048046.html
4-2