Java multithreaded--atomiclongarray

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.