[Practical Java high concurrency programming design 4] Arrays can also be unlocked: AtomicIntegerArray, java concurrent programming practice pdf

Source: Internet
Author: User

[Practical Java high concurrency programming design 4] Arrays can also be unlocked: AtomicIntegerArray, java concurrent programming practice pdf

In addition to providing basic data types, JDK also prepares arrays and other composite structures for us. The available atomic arrays include AtomicIntegerArray, AtomicLongArray, and AtomicReferenceArray, which respectively represent integer arrays, long arrays, and common object arrays.

The following uses AtomicIntegerArray as an example to describe how to use atomic arrays.

AtomicIntegerArray is essentially an encapsulation of the int [] type. The Unsafe class is used to control the security of int [] in multi-thread mode through CAS. It provides the following core APIs: // obtain the public final int get (int I) element under array I // obtain the length of the array public final int length () // set the subscript I of the array to newValue, and return the old public final int getAndSet (int I, int newValue) // perform the CAS operation, if the element of the lower mark I is equal to audit CT, it is set to update. If the setting is successful, true public final boolean compareAndSet (int I, int audit CT, intupdate) is returned) // Add 1 public final int getAndIncrement (int I) to the element under I. // subtract 1 public final int getAndDecrement (int I) from the element under I) // Add delta (delta can be negative) public final int getAndAdd (int I, int delta)

 

The following is a simple example to show how to use AtomicIntegerArray:

01 public class AtomicIntegerArrayDemo {  02    staticAtomicIntegerArray arr = new AtomicIntegerArray(10);  03     public staticclass AddThread implements Runnable{  04         publicvoid run(){  05            for(intk=0;k<10000;k++)  06                 arr.getAndIncrement(k%arr.length());  07         }  08     }  09    public staticvoid main(String[] args) throws InterruptedException {  10         Thread[]ts=new Thread[10];  11         for(intk=0;k<10;k++){  12            ts[k]=new Thread(new AddThread());  13         }  14         for(intk=0;k<10;k++){ts[k].start();}  15         for(intk=0;k<10;k++){ts[k].join();}  16         System.out.println(arr);  17    }  18 }  

 

Line 2 of the code above declares an array containing 10 elements. The thread defined in row 3rd accumulates 10 elements in the group. Each element is added 1000 times. Line 3: Enable 10 such threads. Therefore, it can be predicted that if the thread is secure, the value of 10 elements in the array must be 10000. Otherwise, if the thread is not secure, some or all values will be less than 10000.

The output result of the program is as follows:

  1. [10000,100 00, 10000,100 00, 10000,100 00, 10000,100 10000,]


This indicates that AtomicIntegerArray ensures the thread security of the array reasonably.

 

 

Excerpt: Java high concurrency Program Design

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.