Atomic原子类 Atomic 翻译成中文是原子的意思。在化学上,我们知道原子是构成一般物质的最小单位,在化学反应中是不可分割的。在我们这里 Atomic 是指一个操作是不可中断的。即使是在多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰。所以,所谓原子类说简单点就是具有原子/原子操作特征的类。
原子类分类 根据操作的数据类型,可以将 JUC 包中的原子类分为 4 类
基本类型 使用原子的方式更新基本类型
AtomicInteger
:整型原子类
AtomicLong
:长整型原子类
AtomicBoolean
:布尔型原子类
常用方法
1 2 3 4 5 6 7 public final int get () public final int getAndSet (int newValue) public final int getAndIncrement () public final int getAndDecrement () public final int getAndAdd (int delta) boolean compareAndSet (int expect, int update) public final void lazySet (int newValue)
示例
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 AtomicInteger atomicInt = new AtomicInteger (0 );int tempValue = atomicInt.getAndSet(3 ); System.out.println("tempValue: " + tempValue + "; atomicInt: " + atomicInt); tempValue = atomicInt.getAndIncrement(); System.out.println("tempValue: " + tempValue + "; atomicInt: " + atomicInt); tempValue = atomicInt.getAndAdd(5 ); System.out.println("tempValue: " + tempValue + "; atomicInt: " + atomicInt);boolean updateSuccess = atomicInt.compareAndSet(9 , 10 ); System.out.println("Update Success: " + updateSuccess + "; atomicInt: " + atomicInt);int currentValue = atomicInt.get(); System.out.println("Current value: " + currentValue); atomicInt.lazySet(15 ); System.out.println("After lazySet, atomicInt: " + atomicInt);
输出
1 2 3 4 5 6 tempValue: 0 ; atomicInt: 3 tempValue: 3 ; atomicInt: 4 tempValue: 4 ; atomicInt: 9 Update Success: true ; atomicInt: 10 Current value: 10 After lazySet, atomicInt: 15
数组类型Array 使用原子的方式更新数组里的某个元素
AtomicIntegerArray
:整型数组原子类
AtomicLongArray
:长整型数组原子类
AtomicReferenceArray
:引用类型数组原子类
常用方法
1 2 3 4 5 6 7 public final int get (int i) public final int getAndSet (int i, int newValue) public final int getAndIncrement (int i) public final int getAndDecrement (int i) public final int getAndAdd (int i, int delta) boolean compareAndSet (int i, int expect, int update) public final void lazySet (int i, int newValue)
示例
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 int [] nums = {1 , 2 , 3 , 4 , 5 , 6 };AtomicIntegerArray atomicArray = new AtomicIntegerArray (nums); System.out.println("Initial values in AtomicIntegerArray:" );for (int j = 0 ; j < nums.length; j++) { System.out.print("Index " + j + ": " + atomicArray.get(j) + " " ); }int tempValue = atomicArray.getAndSet(0 , 2 ); System.out.println("\nAfter getAndSet(0, 2):" ); System.out.println("Returned value: " + tempValue);for (int j = 0 ; j < atomicArray.length(); j++) { System.out.print("Index " + j + ": " + atomicArray.get(j) + " " ); } tempValue = atomicArray.getAndIncrement(0 ); System.out.println("\nAfter getAndIncrement(0):" ); System.out.println("Returned value: " + tempValue);for (int j = 0 ; j < atomicArray.length(); j++) { System.out.print("Index " + j + ": " + atomicArray.get(j) + " " ); } tempValue = atomicArray.getAndAdd(0 , 5 ); System.out.println("\nAfter getAndAdd(0, 5):" ); System.out.println("Returned value: " + tempValue);for (int j = 0 ; j < atomicArray.length(); j++) { System.out.print("Index " + j + ": " + atomicArray.get(j) + " " ); }
输出
1 2 3 4 5 6 7 8 9 10 11 Initial values in AtomicIntegerArray:Index 0 : 1 Index 1 : 2 Index 2 : 3 Index 3 : 4 Index 4 : 5 Index 5 : 6 After getAndSet(0 , 2 ):Returned value: 1 Index 0 : 2 Index 1 : 2 Index 2 : 3 Index 3 : 4 Index 4 : 5 Index 5 : 6 After getAndIncrement(0 ):Returned value: 2 Index 0 : 3 Index 1 : 2 Index 2 : 3 Index 3 : 4 Index 4 : 5 Index 5 : 6 After getAndAdd(0 , 5 ):Returned value: 3 Index 0 : 8 Index 1 : 2 Index 2 : 3 Index 3 : 4 Index 4 : 5 Index 5 : 6
引用类型Reference
AtomicReference
:引用类型原子类
AtomicStampedReference
:原子更新带有版本号的引用类型 。该类将整数值与引用关联起来,可用于解决原子的更新数据和数据的版本号,可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
AtomicMarkableReference
:原子更新带有标记的引用类型。 该类将 boolean 标记与引用关联起来,也可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
AtomicReference
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 class Person { private String name; private int age; } AtomicReference<Person> ar = new AtomicReference <>(new Person ("SnailClimb" , 22 )); System.out.println("Initial Person: " + ar.get().toString());Person updatePerson = new Person ("Daisy" , 20 ); ar.compareAndSet(ar.get(), updatePerson); System.out.println("Updated Person: " + ar.get().toString());Person anotherUpdatePerson = new Person ("John" , 30 );boolean isUpdated = ar.compareAndSet(updatePerson, anotherUpdatePerson); System.out.println("Second Update Success: " + isUpdated); System.out.println("Final Person: " + ar.get().toString());
输出
1 2 3 4 Initial Person: Person{name ='SnailClimb' , age =22} Updated Person: Person{name ='Daisy' , age =20} Second Update Success: true Final Person: Person{name ='John' , age =30}
AtomicStampedReference
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 AtomicStampedReference<String> asr = new AtomicStampedReference <>("SnailClimb" , 1 );int [] initialStamp = new int [1 ];String initialRef = asr.get(initialStamp); System.out.println("Initial Reference: " + initialRef + ", Initial Stamp: " + initialStamp[0 ]);int oldStamp = initialStamp[0 ];String oldRef = initialRef;String newRef = "Daisy" ;int newStamp = oldStamp + 1 ;boolean isUpdated = asr.compareAndSet(oldRef, newRef, oldStamp, newStamp); System.out.println("Update Success: " + isUpdated);int [] updatedStamp = new int [1 ];String updatedRef = asr.get(updatedStamp); System.out.println("Updated Reference: " + updatedRef + ", Updated Stamp: " + updatedStamp[0 ]);boolean isUpdatedWithWrongStamp = asr.compareAndSet(newRef, "John" , oldStamp, newStamp + 1 ); System.out.println("Update with Wrong Stamp Success: " + isUpdatedWithWrongStamp);int [] finalStamp = new int [1 ];String finalRef = asr.get(finalStamp); System.out.println("Final Reference: " + finalRef + ", Final Stamp: " + finalStamp[0 ]);
输出
1 2 3 4 5 Initial Reference: SnailClimb, Initial Stamp: 1 Update Success: true Updated Reference: Daisy, Updated Stamp: 2 Update with Wrong Stamp Success: false Final Reference: Daisy, Final Stamp: 2
AtomicMarkableReference
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 AtomicMarkableReference<String> amr = new AtomicMarkableReference <>("SnailClimb" , false );boolean [] initialMark = new boolean [1 ];String initialRef = amr.get(initialMark); System.out.println("Initial Reference: " + initialRef + ", Initial Mark: " + initialMark[0 ]);String oldRef = initialRef;String newRef = "Daisy" ;boolean oldMark = initialMark[0 ];boolean newMark = true ;boolean isUpdated = amr.compareAndSet(oldRef, newRef, oldMark, newMark); System.out.println("Update Success: " + isUpdated);boolean [] updatedMark = new boolean [1 ];String updatedRef = amr.get(updatedMark); System.out.println("Updated Reference: " + updatedRef + ", Updated Mark: " + updatedMark[0 ]);boolean isUpdatedWithWrongMark = amr.compareAndSet(newRef, "John" , oldMark, !newMark); System.out.println("Update with Wrong Mark Success: " + isUpdatedWithWrongMark);boolean [] finalMark = new boolean [1 ];String finalRef = amr.get(finalMark); System.out.println("Final Reference: " + finalRef + ", Final Mark: " + finalMark[0 ]);
输出
1 2 3 4 5 Initial Reference: SnailClimb, Initial Mark: false Update Success: true Updated Reference: Daisy, Updated Mark: true Update with Wrong Mark Success: false Final Reference: Daisy, Final Mark: true
对象的属性修改类型FieldUpdater 如果需要原子更新某个类里的某个字段时,需要用到对象的属性修改类型原子类。
AtomicIntegerFieldUpdater
:原子更新整型字段的更新器
AtomicLongFieldUpdater
:原子更新长整型字段的更新器
AtomicReferenceFieldUpdater
:原子更新引用类型里的字段
要想原子地更新对象的属性需要两步。
第一步,因为对象的属性修改类型原子类都是抽象类,所以每次使用都必须使用静态方法 newUpdater()创建一个更新器,并且需要设置想要更新的类和属性。
第二步,更新的对象属性必须使用 public volatile 修饰符。
上面三个类提供的方法几乎相同,所以我们这里以 AtomicIntegerFieldUpdater
为例子来介绍。
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 class Person { private String name; private volatile int age; } AtomicIntegerFieldUpdater<Person> ageUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age" );Person person = new Person ("SnailClimb" , 22 ); System.out.println("Initial Person: " + person); ageUpdater.incrementAndGet(person); System.out.println("After Increment: " + person); ageUpdater.addAndGet(person, 5 ); System.out.println("After Adding 5: " + person); ageUpdater.compareAndSet(person, 28 , 30 ); System.out.println("After Compare and Set (28 to 30): " + person);boolean isUpdated = ageUpdater.compareAndSet(person, 28 , 35 ); System.out.println("Compare and Set (28 to 35) Success: " + isUpdated); System.out.println("Final Person: " + person);
输出
1 2 3 4 5 6 Initial Person: Name : SnailClimb, Age: 22 After Increment : Name: SnailClimb, Age: 23 After Adding 5 : Name : SnailClimb, Age: 28 After Compare and Set (28 to 30 ): Name : SnailClimb, Age: 30 Compare and Set (28 to 35 ) Success: false Final Person: Name : SnailClimb, Age: 30
基本数据类型原子类的优势 通过一个简单例子带大家看一下基本数据类型原子类的优势
1、多线程环境不使用原子类保证线程安全(基本数据类型)
1 2 3 4 5 6 7 8 9 10 11 class Test { private volatile int count = 0 ; public synchronized void increment () { count++; } public int getCount () { return count; } }
2、多线程环境使用原子类保证线程安全(基本数据类型)
1 2 3 4 5 6 7 8 9 10 class Test2 { private AtomicInteger count = new AtomicInteger (); public void increment () { count.incrementAndGet(); } public int getCount () { return count.get(); } }
AtomicInteger 线程安全原理简单分析 AtomicInteger
类的部分源码:
1 2 3 4 5 6 7 8 9 10 11 12 private static final Unsafe unsafe = Unsafe.getUnsafe();private static final long valueOffset;static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value" )); } catch (Exception ex) { throw new Error (ex); } }private volatile int value;
AtomicInteger
类主要利用 CAS (compare and swap) + volatile 和 native 方法来保证原子操作,从而避免 synchronized 的高开销,执行效率大为提升。
CAS 的原理是拿期望的值和原本的一个值作比较,如果相同则更新成新的值。UnSafe 类的 objectFieldOffset()
方法是一个本地方法,这个方法是用来拿到“原来的值”的内存地址 。另外 value 是一个 volatile 变量,在内存中可见,因此 JVM 可以保证任何时刻任何线程总能拿到该变量的最新值。