"Practical Java High Concurrency Programming 4" Array can also be unlocked: Atomicintegerarray

Source: Internet
Author: User

In addition to providing the basic data types, the JDK also prepares us for complex structures such as arrays. The currently available atomic arrays are: Atomicintegerarray, Atomiclongarray, and Atomicreferencearray, representing integer arrays, long arrays, and normal object arrays, respectively.

Here is an example of Atomicintegerarray, which shows how an atomic array is used.

Atomicintegerarray is essentially an encapsulation of the int[] type. Use the unsafe class to control the security of int[] in multi-threaded mode through CAs. It provides the following core APIs://get the elements of the array I subscript Public Final intGetinti)//get the length of the array Public Final intLength ()//sets the array I subscript to newvalue and returns the old value Public Final intGetandset (intIintnewvalue)//for CAS operations, set to update if the elements of the I subscript are equal to expect, the setting returns true successfully Public Final BooleanCompareandset (intIintexpect, intupdate)//add 1 to the element I subscript Public Final intGetandincrement (inti)//minus 1 of the element I subscript Public Final intGetanddecrement (inti)//Add a Delta (Delta can be negative) to the element I subscript Public Final intGetandadd (intIintDelta

Here is a simple example that shows Atomicintegerarray using:

01 Public classAtomicintegerarraydemo {Staticatomicintegerarray arr =NewAtomicintegerarray (10); 03 PublicStaticclass AddthreadImplementsrunnable{04publicvoid Run () {05 for(intk=0;k<10000;k++)  Arr.getandincrement (k%arr.length ()); 07         }  08     }  09 PublicStaticvoid Main (string[] args)throwsinterruptedexception {Ten thread[]ts=NewThread[10]; 11 for(intk=0;k<10;k++){  ts[k]=NewThread (NewAddthread ()); 13         }  14 for(intk=0;k<10;k++) {Ts[k].start ();} 15 for(intk=0;k<10;k++) {ts[k].join ();} 16System.out.println (arr); 17    }  18}

The 2nd line of the above code declares an array containing 10 elements. The thread defined in line 3rd increments the 10 elements within the array, adding 1000 times per element. Line 11th, open 10 such threads. Therefore, it can be predicted that if the thread is safe, the value of the 10 elements in the array must be 10000. Conversely, if the thread is unsafe, some or all of the values will be less than 10000.

The output of the program is as follows:

    1. [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000,10000, 10000 ]


This shows that Atomicintegerarray does reasonably guarantee the thread safety of the array.

Excerpt from: Practical Java High concurrency program design

"Practical Java High Concurrency Programming 4" Array can also be unlocked: Atomicintegerarray

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.