Explanation of Java CAS Operation and Unsafe Class

  • 2021-08-31 07:53:52
  • OfStack

Catalogue 1. Review
2. Comparison between the two
3. Under what circumstances will volatile be used
4. Atomic operations in Java
5. CAS Operation in Java
6. ABA issues
7. Unsafe category
1.long objectFieldOffset(Field field)
2.int arrayBaseOffset(Class arrayClass)
3.int arrayIndexOffset(Class arrayClass)
4.boolean compareAndSwapLong(Object obj,long offset,long expect,long update)
8. Source:

STEP 1 Review

Brief introduction of computer memory model, synchronized and volatile keywords

2. Comparison between the two

Both sychronized and volatile address memory visibility issues
Differences:
(1) The former is an exclusive lock with the overhead of context switching and the overhead of thread rescheduling; The latter is a non-blocking algorithm, which does not cause the overhead of context switching.
(2) The former can guarantee the atomicity of operation, but the latter cannot.

3. Under what circumstances will volatile be used

Write variable is not dependent on the current value, if it is dependent on the current value, because get-calculate-write, 3 is not atomic operation, and volatile is guaranteed atomic operation. When the variable is not locked, if the variable is locked, the visibility of the memory can be guaranteed, so it is no longer necessary to use volatile

4. Atomic Operation in Java

Generally speaking, atomic operation is a group of operations, which either succeed or fail, and there is no partial success Using synchronized keyword can guarantee the atomicity of operation and the visibility of memory, while volatile can only guarantee the visibility of memory, but can not guarantee the atomicity of operation; synchronized is good, but in the case of high concurrency, because it is an exclusive lock, it will cause poor performance.

5. CAS Operation in Java

Definition: CAS (compare and swap) Compare and Exchange, a non-blocking algorithm provided by JDK, which guarantees the atomicity of comparison-update through hardware. The Unsafe class in JDK provides a series of compareAndSwap* methods. The following is an example of compareAndSwapLong boolean compare(Object obj,long offset,long expect,long update) First, explain each parameter under 1 separately, obj is a reference of an object (that is, the address stored by the object), offset is an offset relative to the previous address, expect is an expected value, and update represents that if the expected value is 1, then the value of update is used instead, and true is returned, otherwise false is returned This is an atomic instruction provided by the processor

6. ABA issues

Description: Thread 1 gets the value of the variable x to A, and then tries to modify it to B, but if another thread modifies the value of x to B and modifies it to A at the same time, then this A of thread 2 and A before thread 1 are not the same A Cause: Circular dependency, variable values from A to B, then from B to A, which can only rotate in one direction, but this will not happen if it is from A to B, then from B to C. Solution: AtomicStampedReferece in JDK gives each variable a timestamp, thus avoiding the ABA problem

7. Unsafe category

In the rt. jar package of JDK, many methods are native, which is a kind of hardware-level operation. JNI is used to call C + + underlying functions to operate.

1.long objectFieldOffset(Field field)

Explanation: Gets the memory offset in the object where a domain value in the object is located


try{
 long value = Unsafe.objectFieldOffset(AutomicLong.class.getDeclaredField("value"));
}catch(Exception e){
 e.printStackTrace();
}

2.int arrayBaseOffset(Class arrayClass)

Paraphrase: Gets the address of the first element in the array

3.int arrayIndexOffset(Class arrayClass)

Explanation: Gets the byte size of the first element in the array

4.boolean compareAndSwapLong(Object obj,long offset,long expect,long update)

See above

8. Source:

Package: com. ruigege. OtherFoundationOfConcurrent2

https://github.com/ruigege66/ConcurrentJava

These are the details of Java CAS operation and Unsafe class. For more information about Java CAS operation and Unsafe class, please pay attention to other related articles on this site!


Related articles: