AtomicInteger
- 注意value是volatile修饰的
Unsafe

JDK自带的底层实现,不允许开发者直接调用。只允许bootstrap类加载器加载。调用C++本地方法去执行。
getAndSet
compareAndSet
updateAndGet
public static void main(String[] args) {AtomicInteger i = new AtomicInteger(5);/*System.out.println(i.incrementAndGet()); // ++i 1System.out.println(i.getAndIncrement()); // i++ 2System.out.println(i.getAndAdd(5)); // 2 , 7System.out.println(i.addAndGet(5)); // 12, 12*/// 读取到 设置值// i.updateAndGet(value -> value * 10);System.out.println(updateAndGet(i, p -> p / 2));// i.getAndUpdate()System.out.println(i.get());}//模拟updateAndGetpublic static int updateAndGet(AtomicInteger i, IntUnaryOperator operator) {while (true) {int prev = i.get();int next = operator.applyAsInt(prev);if (i.compareAndSet(prev, next)) {return next;}}}}
