Java concurrent programming atomic operation class

Basic classes of atomic operations

AtomicBoolean: Atomic update Boolean type.

AtomicInteger: Atomic update integer.

AtomicLong: Atomic update long integer.

 

AtomicInteger:

int addAndGet (int delta): atomically combine the entered value with the value in the instance (the AtomicInteger

value) to add and return the result.

boolean compareAndSet (int expect, int update): If the input value is equal to the expected value, the value is atomically set to the input value.

int getAndIncrement(): Add 1 to the current value atomically. Note that what is returned here is the value before the increment.

void lazySet (int newValue): Will eventually be set to newValue. After using lazySet to set the value, other threads may still be able to read the old value for a short period of time.

int getAndSet(int newValue): Set the value of newValue atomically and return the old value.

Atomic array operations

AtomicIntegerArray: Atomic update of the elements in the integer array.

AtomicLongArray: Atomic updates the elements in the long integer array.

AtomicReferenceArray: Atomic update of the elements in the reference type array.

The AtomicIntegerArray class mainly provides an atomic way to update the integer type in the array

 

AtomicIntegerArray:

int addAndGet(int i, int delta): Atomically add the input value to the element at index i in the array.

boolean compareAndSet (int i, int expect, int update): If the current value is equal to the expected value, then use the atom

Set the element at position i of the array to the update value.

 

Atom citation operation

AtomicReference: Atomic update reference type.

AtomicReferenceFieldUpdater: Atomic update the fields in the reference type.

AtomicMarkableReference: Atomic update of the reference type with mark bits. A boolean can be updated atomically

Type mark bit and reference type. The construction method is AtomicMarkableReference (V initialRef, boolean

initialMark)。

Example 1:

public static AtomicReference<User> atomicUserRef = new AtomicReference<User>();

public static void main(String[] args) {

User updateUser= new User("conan", 15);

User user=atomicUserRef.get();

atomicUserRef.compareAndSet(user, updateUser);

}

Example 2:
 

private static AtomicIntegerFieldUpdater<User> a = AtomicIntegerFieldUpdater.newUpdater(User.class, "old");

public static void main(String[] args) {

User conan = new User("conan", 10);

System.out.println(getAndIncrement(conan));

System.out.println(a.get(conan));

}

Example 3:

private static AtomicMarkableReference<Integer> a=new AtomicMarkableReference<Integer>(1, false);

public static void main(String[] args) {

System.out.println(a.compareAndSet(1, 2, false, true));

}

Atomic update field class

AtomicIntegerFieldUpdater: An updater that atomically updates integer fields.

AtomicLongFieldUpdater: An updater that atomically updates long integer fields.

AtomicStampedReference: Atomic update reference type with version number. This class associates integer values ​​with references, can be used for atomic update data and data version numbers, and can solve the ABA problem that may occur when using CAS for atomic updates.

Guess you like

Origin blog.csdn.net/weixin_44416039/article/details/86160627