Java Foundation---Multi-threaded---JUC atomic class

Source: Internet
Author: User
Tags cas volatile

Summarize:
    1. The atomic package provides four types of atomic classes. Includes four types of basic data types, arrays, reference types, and reference type member variables.
    2. The lower level uses CAS operations to ensure atomicity, and CAS is compare and swap first, and if expected values are exchanged. CAs are implemented on a hardware level by adding a bus lock or using a memory lock.
    3. CAS has some drawbacks: 1. A single operation can guarantee atomicity, but a composite operation cannot be guaranteed. 2. When the concurrency is large, it can result in a lot of cyclic comparison operation consumption performance. 3. ABA issues. Some intermediate processes will be lost.
    4. Solve the ABA problem using atomicstampedreference resolution, add a version version number each time you judge the edition.
  === Brief Description Http://www.cnblogs.com/skywang12345/p/java_threads_category.htmlatomic provides 12 classes of atomic operations, four types. Enables atomic manipulation of basic data types, arrays, objects, and object members. Atomic manipulation means that the operation process is not interrupted by another thread. 1.  Basic types: Atomicinteger, Atomiclong, Atomicboolean;2.  array type: Atomicintegerarray, Atomiclongarray, Atomicreferencearray;3.  Reference type: Atomicreference, atomicstampedrerence, Atomicmarkablereference;4.  Property modification Type of object: Atomicintegerfieldupdater, Atomiclongfieldupdater, Atomicreferencefieldupdater.  === when it comes to updating data, how does the Atomic class guarantee atomicity? What kind of method is used? The underlying uses volatile to define the member variables in the class to achieve visibility. CAS operations to achieve atomicity. The bottom use of the unsafe class three methods Compareandswapobject,compareandswapint and Compareandswaplong, and then see Atomicboolean source Code, It is found that it is the first to convert a Boolean into an integer, and then use Compareandswapint for CAs, so the atomic update double can also be implemented with similar ideas. A CAS operation is an expectation, a modification of a value, if the current value equals the expectation. How does the  ===cas operation ensure thread safety? How exactly is it implemented on the hardware? CAs method does not do any processing in the language aspect, does not lock, but realizes the hardware level blocking. The processor uses an atomic operation between multiple processors based on a cache-lock or bus-lock approach. The first mechanism is to ensure atomicity through a bus lock. If multiple processors simultaneously read and overwrite shared variables (i++ is the classic read overwrite operation), then the shared variable will be manipulated by multiple processors at the same time, so that the read rewrite operation is not atomic, and the value of the shared variable will be inconsistent with the expected after the operation, for example: if I=1, we do two times I + + operation, we expect theThe result is 3, but it is possible that the result is 2. The reason is that it is possible for multiple processors to read the variable i from their respective caches at the same time, adding one operation separately and then writing to the system memory. If you want to ensure that the operation of the read overwrite of the shared variable is atomic, you must ensure that the CPU1 read overwrites the shared variable, and CPU2 cannot manipulate the cache that caches the shared variable's memory address. The processor uses a bus lock to solve this problem. The so-called bus lock is a lock# signal that is provided by the processor, and when a processor outputs this signal on the bus, the other processor's request is blocked, and the processor can use the shared memory exclusively. The second mechanism is to ensure atomicity through cache locking. At the same time we just need to ensure that the operation of a memory address is atomic, but the bus lock the CPU and memory communication between the lock, which makes the lock during the other processors can not manipulate the data of other memory addresses, so bus locking overhead is relatively large, Recent processors use cache locking instead of bus locking in some situations to optimize. Disadvantages and solutions of Http://blog.csdn.net/a953713428/article/details/54562648 ===cas operation http://blog.csdn.net/ goodlixueyong/article/details/51339689       cas Although it is very efficient to achieve atomic operation, but it still has three problems. 1, ABA problem. CAS checks if the value has changed while operating the value and does not change until it is updated. But if a value turns out to be a, b, and a, then the CAS will consider the value unchanged, but it actually changes. The solution to the ABA problem is to use the version number. Append the version number before the variable, and add a version number one each time the variable is updated, then a-b-a becomes 1a-2b-3a. A class atomicstampedreference is provided in the atomic package of the JDK starting with Java1.5 to solve the ABA problem. Add a version number identification, determine whether the version number is consistent, the same exchange. 2, the higher the concurrency, the more the number of failures, CAS if the long time is unsuccessful, will greatly increase the CPU overhead. CAs are therefore not suitable for scenarios where competition is very frequent. 3. Only one atomic operation with a shared variable can be guaranteed. When operating on multiple shared variables, CAS cannot guarantee the atomicity of operations, which can be manipulated by locking or merging multiple shared variables into a single shared variable. For example, there are two shared variable i=2,j=a, merge ij=2a, and then use CAs to manipulate IJ. Starting from Java1.5 The JDK provides the Atomicreference classTo guarantee the atomicity of the referenced object, you can put multiple variables in an object for CAS operations.  ===atomicintegerfieldupdater uses the reflection mechanism to perform atomic modification operations on object member variables. At the same time to set the member variable that accepts the change to the type of volatile  ===longadder is there any advantage over Atomicinteger? Longadder is much more efficient than atomitinteger in high concurrency scenarios. Atomic class is mainly relying on CAS operation to ensure synchronization, but the disadvantage is that because it is a single point update only a value, so high concurrency under the high probability of failure of CAS, repeated operations are many, consumption performance. The advantage of  longadder is that in low concurrency scenarios, CAS atom updates are used in the same way as atomic, but the method of point updating is used in high concurrency scenarios. Maintain a cell array, the cell has a value, the high concurrency scenario is assigned to different cells to update the corresponding value value, that is, the multipart update operation, the total number of times required to add up on it.   

Java Foundation---multithreaded---juc atomic class

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.