Reference Address: http://blog.csdn.net/xh16319/article/details/17056767
After Java6 we not only contacted the lock related locks, but also touched a lot of more optimistic atomic modification operations, that is, in the modification we only need to ensure that the moment it is safe, after the corresponding packaging can be processed after the object of concurrent modification, and the concurrency of the ABA problem, This article describes the implementation of the atomic family of classes and how to use them, including:
Basic class: Atomicinteger, Atomiclong, Atomicboolean;
Reference type: Examples of ABA in Atomicreference, Atomicreference, Atomicstampedrerence, atomicmarkablereference;
Array Type: Atomicintegerarray, Atomiclongarray, Atomicreferencearray
attribute Atomic modifier (Updater): Atomicintegerfieldupdater, Atomiclongfieldupdater, Atomicreferencefieldupdater
Before using the atomic series, we need to know that one thing is the unsafe class, The full name is: Sun.misc.Unsafe, this class contains a lot of operations on C code, including a lot of direct memory allocation and atomic operation of the call, and it is marked as non-secure, is to tell you that there are a number of method calls inside the security risks, need to be careful to use, otherwise it will lead to serious consequences, such as through UNSA Fe allocates memory, if you specify certain areas may cause some similar C + + pointers out to other processes, but its specific use is not the focus of this article, this article focuses on the atomic series of content will be based on the following several local methods in the unsafe class to operate:
after the reference of the object is exchanged,the interchange succeeds and returns True, and the interchange fails to return false, the exchange process is completely atomic, and after the results are computed on the CPU, the results of the memory are compared to the original values, but if not, they are considered not to be replaced. Because the variable is a volatile type, the final data written will be seen by other threads, so when a thread is modified successfully, the other threads find that their modifications have failed.
parameter 1: object of the class itself (typically this is a modification of an object's properties before concurrency occurs, so the class that exists for the object also has an object)
Parameter 2: This property in the relatively inexpensive position of the object, in fact, compared to the memory unit, so the starting position of the property needs, and the reference is to modify the reference address (depending on the OS, the number of VM bits and parameter configuration decision width is generally 4-8 bytes), int is the modification of the associated 4 bytes, and long is the 8 bytes that are modified.
Getting the offset is also a way of passing unsafe: objectfieldoffset (Fieldfield) to get the offset of the property in the object; static variables need to be obtained by: staticfieldoffset (Field field), The total method to invoke is: FieldOffset (Fieldfield)
Parameter 3: The original value of the modified reference, used to compare the original reference and the target to be modified is consistent.
Parameter 4: Modify the target value, to modify the data to what.
[Java]View Plaincopy
- Public final native boolean Compareandswapobject (Object ParamObject1, long Paramlong, object ParamObject2, Object paramObject3);
- Public final native boolean compareandswapint (Object paramobject, long paramlong, int paramInt1 , int paramInt2);
#对long的操作, depends on whether the VM supports a long CAs, because it is possible that the VM itself is not supported, if not supported, this time the operation will become lock mode, but now the VM is basically supported.
[Java]View Plaincopy
- Public final native boolean compareandswaplong (Object paramobject, long paramLong1, long paramLong2 , long ParamLong3);
We do not recommend using unsafe directly to manipulate atomic variables, but rather to manipulate atomic variables through some classes of Java encapsulation.
The last part of this article is to introduce the updater is the modifier, it is a atomic series of an extension, Atomic series is for you to define some of the objects you can use, but if someone else already in use of the object will be the original code needs to be modified to atomic series, At this point, it is cumbersome to modify the type to the corresponding object, because there is a lot of code involved, when Java provides an external updater can provide a similar atomic to the modification of the object's property itself, that is, its operation on these ordinary properties is concurrent security, respectively, by: Atomicintegerfieldupdater, Atomiclongfieldupdater, Atomicreferenceupdater, the system will be more flexible after this operation, That is, it is possible that the properties of those classes only need to control concurrency in some cases, and many times not, but their use usually has the following limitations:
Limit 1: The target of the operation cannot be a static type, as mentioned earlier, unsafe can be guessed that it extracts the property offset of the non-static type, if the static type is not used when the corresponding method will be an error, And this updater does not use the corresponding method.
Limit 2: The target of the operation cannot be of the final type, because final cannot be modified at all.
Limit 3: Must be a volatile type of data, that is, the data itself is read-consistent.
Limit 4: The property must be visible to the region where the current updater is located , that is, private if not the current class is definitely invisible,protected If there is no parent-child relationship, thedefault is not visible under the same package.
implementation: The property is found by reflection, the property is manipulated, but it is not set accessable, so it must be a visible property in order to operate.
Atomicinteger Related Classes