并发编程--原子类AotmicInteger

本文详细介绍了Java并发编程包java.util.concurrent中的AtomicInteger类。AtomicInteger利用volatile变量和Unsafe类提供的CAS操作实现了原子性的整数操作,适用于多线程环境。文章还提供了AtomicInteger的部分源码解析。

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

前几篇博客中我们已经介绍了线程、volatile、synchronized和cas自旋相关的知识,接下来我介绍一下jdk提供的并发编程包java.util.concurrent中相关的实现类知识。

AtomicInteger简单来说就是一个能进行原子操作的Integer,这样在多线程操作下对AtomicInteger的操作是原子操作的,操作后的值对所有线程都是立即可见的。简单来说其实现就是使用的volatile变量 value和sun.misc.Unsafe提供的CAS操作完成的,volatile变量value就表明任意一个线程对value的操作都是对其他线程可见的,并且对于value的修改使用Unsafe提供的方法实现线程不阻塞来完成对value值的修改等操作。

AtomicInteger的变量值:

[java]  view plain  copy
  1. private volatile int value;  
get和set操作就是直接赋值给value

[java]  view plain  copy
  1. public final int get() {  
  2.        return value;  
  3.    }  
  4.   
  5. public final void set(int newValue) {  
  6.        value = newValue;  
  7.    }  
加加和减减操作就是使用Unsafe提供的方法完成CAS操作

[java]  view plain  copy
  1. public final int incrementAndGet() {  
  2.     return unsafe.getAndAddInt(this, valueOffset, 1) + 1;  
  3. }  
  4.   
  5. public final int decrementAndGet() {  
  6.     return unsafe.getAndAddInt(this, valueOffset, -1) - 1;  
  7. }  


简单来说AtomicInteger的实现原理就是通过一个volatile的变量value以及Unsafe类提供的CAS操作来完成的。

AtomicInteger源码:

[java]  view plain  copy
  1. public class AtomicInteger extends Number implements java.io.Serializable {  
  2.     private static final long serialVersionUID = 6214790243416807050L;  
  3.   
  4.     private static final Unsafe unsafe = Unsafe.getUnsafe();  
  5.     private static final long valueOffset;  
  6.   
  7.     static {  
  8.         try {  
  9.             valueOffset = unsafe.objectFieldOffset  
  10.                 (AtomicInteger.class.getDeclaredField("value"));  
  11.         } catch (Exception ex) { throw new Error(ex); }  
  12.     }  
  13.   
  14.     private volatile int value;  
  15.   
  16.     public AtomicInteger(int initialValue) {  
  17.         value = initialValue;  
  18.     }  
  19.       
  20.     public AtomicInteger() {  
  21.     }  
  22.   
  23.     public final int get() {  
  24.         return value;  
  25.     }  
  26.   
  27.     public final void set(int newValue) {  
  28.         value = newValue;  
  29.     }  
  30.       
  31.     public final void lazySet(int newValue) {  
  32.         unsafe.putOrderedInt(this, valueOffset, newValue);  
  33.     }  
  34.   
  35.     
  36.     public final int getAndSet(int newValue) {  
  37.         return unsafe.getAndSetInt(this, valueOffset, newValue);  
  38.     }  
  39.   
  40.     public final boolean compareAndSet(int expect, int update) {  
  41.         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);  
  42.     }  
  43.   
  44.     public final boolean weakCompareAndSet(int expect, int update) {  
  45.         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);  
  46.     }  
  47.   
  48.     public final int getAndIncrement() {  
  49.         return unsafe.getAndAddInt(this, valueOffset, 1);  
  50.     }  
  51.   
  52.     public final int getAndDecrement() {  
  53.         return unsafe.getAndAddInt(this, valueOffset, -1);  
  54.     }  
  55.   
  56.     public final int getAndAdd(int delta) {  
  57.         return unsafe.getAndAddInt(this, valueOffset, delta);  
  58.     }  
  59.   
  60.     public final int incrementAndGet() {  
  61.         return unsafe.getAndAddInt(this, valueOffset, 1) + 1;  
  62.     }  
  63.   
  64.     public final int decrementAndGet() {  
  65.         return unsafe.getAndAddInt(this, valueOffset, -1) - 1;  
  66.     }  
  67.   
  68.     public final int addAndGet(int delta) {  
  69.         return unsafe.getAndAddInt(this, valueOffset, delta) + delta;  
  70.     }  
  71.   
  72.     public final int getAndUpdate(IntUnaryOperator updateFunction) {  
  73.         int prev, next;  
  74.         do {  
  75.             prev = get();  
  76.             next = updateFunction.applyAsInt(prev);  
  77.         } while (!compareAndSet(prev, next));  
  78.         return prev;  
  79.     }  
  80.   
  81.     public final int updateAndGet(IntUnaryOperator updateFunction) {  
  82.         int prev, next;  
  83.         do {  
  84.             prev = get();  
  85.             next = updateFunction.applyAsInt(prev);  
  86.         } while (!compareAndSet(prev, next));  
  87.         return next;  
  88.     }  
  89.   
  90.     public final int getAndAccumulate(int x,  
  91.                                       IntBinaryOperator accumulatorFunction) {  
  92.         int prev, next;  
  93.         do {  
  94.             prev = get();  
  95.             next = accumulatorFunction.applyAsInt(prev, x);  
  96.         } while (!compareAndSet(prev, next));  
  97.         return prev;  
  98.     }  
  99.   
  100.     public final int accumulateAndGet(int x,  
  101.                                       IntBinaryOperator accumulatorFunction) {  
  102.         int prev, next;  
  103.         do {  
  104.             prev = get();  
  105.             next = accumulatorFunction.applyAsInt(prev, x);  
  106.         } while (!compareAndSet(prev, next));  
  107.         return next;  
  108.     }  
  109.   
  110.     public String toString() {  
  111.         return Integer.toString(get());  
  112.     }  
  113.   
  114.     public int intValue() {  
  115.         return get();  
  116.     }  
  117.   
  118.     public long longValue() {  
  119.         return (long)get();  
  120.     }  
  121.   
  122.     public float floatValue() {  
  123.         return (float)get();  
  124.     }  
  125.   
  126.     public double doubleValue() {  
  127.         return (double)get();  
  128.     }  
  129.   
  130. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值