In layman's Java Concurrency (3): Atomic operation part 2[go]

Source: Internet
Author: User

In this section, we begin to discuss array atomic operations and some other atomic operations.

Atomicintegerarray/atomiclongarray/atomicreferencearray 's API is similar, select a representative atomicintegerarray to describe these issues.

int get (int i)

Gets i The current value of the position. Obviously, because this is an array operation, there is an index cross-boundary problem (Indexoutofboundsexception exception).

For the following API starting and Atomicinteger is similar, this method, the name of the parameter can get the function of the meaning of the writing is very commendable. This practice is highly respected in refactoring: improving both the design of existing code and the Code cleanliness path.


void set (int i, int newvalue)
void Lazyset (int i, int newvalue)
int getandset (int i, int newvalue)
Boolean compareandset (int i, int expect, int update)
Boolean weakcompareandset (int i, int expect, int update)
int getandincrement (int i)
int getanddecrement (int i)
int getandadd (int i, int delta)
int incrementandget (int i)
int decrementandget (int i)
int addandget (int i, int delta)

As a whole, the atomic operations of arrays are relatively easy to understand, and these APIs are more useful to realize their benefits than just stay in the theoretical stage.

Now focus on the atomic update of the field.

atomicintegerfieldupdater<t>/atomiclongfieldupdater<t>/atomicreferencefieldupdater<t,v> is the value of the field that is updated based on the reflected atom.

The corresponding API is also very simple, but there are some constraints.

(1) The field must be of type volatile! In a later chapter, I'll explain in detail why it must be a volatile,volatile.

(2) The description type (modifier public/protected/default/private) of the field is consistent with the caller's relationship to the Action Object field. That is, the caller is able to manipulate the object field directly, then the atomic operation can be reflected. However, for a field of the parent class, the subclass cannot be manipulated directly, although the subclass can access the fields of the parent class.

(3) can only be an instance variable, not a class variable, that is, the static keyword cannot be added.

(4) can only be modifiable variables, cannot make final variables, because final semantics is not modifiable. There is actually a conflict between final semantics and volatile, and these two keywords cannot exist at the same time.

(5) for atomicintegerfieldupdater and atomiclongfieldupdater only fields of type Int/long can be modified, their wrapper type cannot be modified (integer/ Long). If you want to modify the wrapper type, you need to use atomicreferencefieldupdater.

The method of operation is described in the following example.

Package xylz.study.concurrency.atomic;

Import Java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class Atomicintegerfieldupdaterdemo{

Class Demodata{
public volatile int value1 = 1;
volatile int value2 = 2;
protected volatile int value3 = 3;
private volatile int value4 = 4;
}
Atomicintegerfieldupdater<demodata> Getupdater (String fieldName){
Return Atomicintegerfieldupdater.newupdater (Demodata.class, fieldName);
}
void doit (){
Demodata data = new Demodata ();
System.out.println ("1 ==>" +getupdater ("value1"). Getandset (data, 10));
System.out.println ("3 ==>" +getupdater ("value2"). Incrementandget (data));
System.out.println ("2 ==>" +getupdater ("Value3"). Decrementandget (data));
System.out.println ("True ==>" +getupdater ("Value4"). Compareandset (Data, 4, 5));
}
public static void Main (string[] args){
Atomicintegerfieldupdaterdemo demo = new Atomicintegerfieldupdaterdemo ();
Demo.doit ();
}
}


In the example above, the Demodata field value3/value4 is not visible to the Atomicintegerfieldupdaterdemo class, so it is not possible to modify its value directly by reflection.

The atomicmarkablereference class describes an <Object,Boolean> pair that can be atomically modified by an Object or Boolean value, which is useful in some caches or state descriptions. This structure can effectively improve throughput when a single or simultaneous modification of the Object/boolean is possible.

The atomicstampedreference class maintains an object reference with an integer "flag" that can be updated atomically. Comparing the <object,boolean> of the atomicmarkablereference class,atomicstampedreference maintains a similar <object, The data structure of int> is actually a concurrent count of objects (references). However, unlike Atomicinteger , this data structure can carry an object reference, and the ability to atomically manipulate both the object and the count.

The "ABA Problem" is mentioned in later chapters, and atomicmarkablereference/atomicstampedreference is useful in solving "ABA problems" .

The use of atomic operations is probably so much, generally speaking is relatively clear, in the next chapter, the atomic operation of the object is summarized, focusing on the principle of atomic operation and design ideas.

In layman's Java Concurrency (3): Atomic operation part 2[go]

Related Article

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.