Java.util.concurrent package source Reading (ii) java.util.concurrent.atomic package

Source: Internet
Author: User
Tags abstract cas integer thread volatile

There are four types of aomic data types: Aomicboolean, Aomicinteger, Aomiclong, and Aomicreferrence (for object) and their array types.

There is also a special aomicstampedreferrence, which is not a subclass of Aomicreferrence, but an extension class that uses aomicreferrence to store references and integer groups

First, all atomic operations are dependent on the Sun.misc.Unsafe class, which is implemented by C + +, using pointers to implement data manipulation

About CAs

A lock-free mechanism for comparing and exchanging operations that contain three operands-the memory position (V), the expected original value (A), and the new value (B). If the value of the memory location matches the expected original value, the processor automatically updates the location value to the new value. Otherwise, the processor does nothing. In either case, it returns the value of the location before the CAS directive. CAS effectively describes "I think location V should contain a value A; If you include that value, put B in that position; otherwise, do not change the location, just tell me the current value of this position."

Benefits: Operating system level support, higher efficiency, no lock mechanism, lower thread waiting, actually throw this task to the operating system.

This theory is the foundation of the entire java.util.concurrent package.

About Sun.misc.Unsafe

A few questions

1) Four basic types of compareandset and Weakcompareandset implementations are the same?

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

2 Atomiclong Set method is not thread safe, why? Not thread-incomplete, but for long updater, there will be Vm_supports_long_cas, if the JVM's LONG operation is atomized, it will be updated with a lock-free CAS, and will be updated with a lock if not supported.

ATOMICXXXX four numeric types

1. The value members are all volatile

2. Basic Method Get/set

3. Compareandset

Weakcompareandset,

Lazyset: Use unsafe to update the C + + implementation of the reference unsafe in sequence)

Getandset: Takes the current value, uses the current value and prepares the updated value to do the CAs

4. For long and integer

Getandincrement/incrementandget

Getanddecrement/decrementandget

Getandadd/addandget

All three groups of methods and Getandset, take the current value, add and subtract to get ready to update the value, and then do cas,/the difference is whether to return the current value or update value.

About arrays

1. There is no Boolean array, you can replace with integer, the bottom of the implementation of exactly the same, after all, Atomicboolean is the bottom of the implementation of the integer

2. Array variable volatile does not make sense, so set/get need unsafe to do it, the method is consistent with the above, but one more index to specify the operation of which element in the array.

About Fieldupdater

1) using the principle of reflection, to implement an atomized update of a field of a class that must be consistent with updater requirements, for example, if the atomicintegerfieldupdater is used, the field must be an integer type. and must have a volatile qualifier. The updater can be invoked in exactly the same way as the number type, adding an additional object of that type as a parameter, and Updater will update that field for that object.

2) updater itself is an abstract class, but there is a privatized implementation, using façade mode, in the abstract class using static methods to create the implementation

Atomicmarkablereference/atomicstampedreference

The former Referencebooleanpair type of Atomicreference,referencebooleanpair represents an object and a Boolean-labeled pair

The former Referenceintegerpair type of Atomicreference,referencebooleanpair represents an object and an integer labeled Pair

Author: cnblogs Wu Liu Qi

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.