Atomicreference atomic class of Java concurrency_ Power node Java college sorting

Atomicreference introduction and function list

Atomicreference is used to perform atomic operations on "objects".

Atomicreference function list

Atomicreference source code analysis (based on jdk1.7.0_40)

At jdk1 7.0_ Atomicreference The source code of Java is as follows:

explain:

The source code of atomicreference is relatively simple. It implements atomic operations through "Volatile" and "CAS functions provided by unsafe".

(01) value is of volatile type. This ensures that when a thread modifies the value, the value value seen by other threads is the latest value, that is, the modified volatile value.

(02) set value through CAS. This ensures that when a thread pool sets value through CAS function (such as compareandset function), its operation is atomic, that is, the thread will not be interrupted when operating value.

Atomicreference example

// AtomicReferenceTest. Java source code

Operation results:

Result description:

When you create a new atomicreference object AR, initialize it to P1.

Next, set it through the CAS function. If the value of AR is P1, set it to P2.

Finally, get the object corresponding to AR and print the result. p3. The result of equals (P1) is false because person does not override the equals () method, but inherits from object The equals () method of Java; And object Equals () in Java actually calls "= =" to compare two objects, that is, to compare whether the addresses of the two objects are equal.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>