Atomiclong atomic class of Java concurrency_ Power node Java college sorting

Atomiclong introduction and function list

Atomiclong is used to perform atomic operations on long shaping.

In a 32-bit operating system, 64 bit long and double variables are not atomic because they are operated by the JVM as two separate 32 bits. Using atomiclong can keep the operation of long atomic.

Atomiclong function list

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

Atomiclong complete source code

The code of atomiclong is very simple. The principle of atomiclong will be explained by taking incrementandget() as an example.

The source code of incrementandget() is as follows:

explain:

(01) incrementandget() will first obtain the long value corresponding to atomiclong according to get(). This value is a variable of volatile type. The source code of get() is as follows:

(02) incrementandget() then increases current by 1, and then assigns the new value to value through CAS function.

The source code of compareandset() is as follows:

Compareandset() updates the long value corresponding to atomiclong. It compares whether the original value of atomiclong is equal to expect. If so, set the value of atomiclong to update.

Atomiclong example

Operation results:

The above is the atomiclong atomic class of Java concurrency introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>