Atomicintegerarray, Atomiclongarray, Atomicreferencearray are similar in principle and usage to the atomic classes of the 3 array types. This chapter describes an atomic class of Atomiclongarray array types. The function of Atomiclongarray is to perform atomic operations on the "long Shaped array".
Atomiclongarray Source Analysis (based on jdk1.7.0_40)
Atomiclongarray's Complete source code
Full source See http://www.cnblogs.com/skywang12345/p/3514604.html
Atomiclongarray code is very simple, the following is only Incrementandget () as an example, the principle of Atomiclong is described.
Incrementandget () source code is as follows:
Public final long Incrementandget (int i) { return Addandget (i, 1);}
Description : The Role of Incrementandget () is to atomically add an element of the long array's index I to 1 and return the value after 1.
Addandget () source code is as follows:
public long addandget (int i, long delta) { //Check if array is out of bounds long offset = checkedbyteoffset (i); while (true) { //Gets the original value of the index offset of the long array, long current = Getraw (offset); Modify Long value long next = current + Delta; The value of the index offset of the long array is updated by CAs. if (Compareandsetraw (offset, current, next)) return to Next;} }
description : Addandget () first checks if the array is out of bounds. If there is no bounds, the value of the array index i is obtained first, and then the value of I is updated with the CAS function.
Getraw () source code is as follows:
Private long Getraw (long offset) { return Unsafe.getlongvolatile (array, offset);}
Description : Unsafe is an unsafe object returned by Unsafe.getunsafe (). The elements of a long array are atomically manipulated by the CAS function of unsafe. such as Compareandsetraw () is called the CAS function of unsafe, its source code is as follows:
Private Boolean Compareandsetraw (long offset, long expect, long update) { return Unsafe.compareandswaplong (array, Offset, expect, update);}
Java multithreaded--atomiclongarray