Introduction to CAS and AQS of Java concurrency

Source: Internet
Author: User

1, what is CAs

CAS (Compare and Swap), that is, compare and swap. is a mechanism to solve the performance loss caused by the use of locks in multi-threaded parallel situations, theCAS operation consists of three operands-the memory location (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 will return the value of that location before the CAS directive. CAS effectively explains that "I think position v should contain a value of a; if it is included, B is placed in this position; otherwise, do not change the position, just tell me the current value of this position."

In Java, sun.misc.Unsafe classes provide a hardware-level atomic operation to implement this CAS. The class java.util.concurrent 's CAS operations are used by a large number of classes under the package Unsafe.java . As Unsafe.java for the specific implementation here is not discussed.

CAS Typical applications

java.util.concurrent.atomicThe classes under the package are mostly implemented using CAS operations (eg. AtomicInteger.java , AtomicBoolean , AtomicLong ). The AtomicInteger.java implementation of these atomic classes is described in some detail below.

2,aqs ( AbstractQueuedSynchronizer) What is Aqs

FIFO queue + Atomic int (indicates state)

Atom int:Atomicinteger The existence of this class is to satisfy the problem that the native shaping numeric self-increment thread is unsafe in the case of high concurrency;

AQS ( AbstractQueuedSynchronizer ), AQS is a synchronization framework provided under the JDK to implement a blocking lock based on the FIFO wait queue and the associated Synchronizer. This abstract class is designed as the base class for the Synchronizer that represents the state as some of the available atomic int values . If you have seen a similar CountDownLatch type of source code implementation, you will find that there is an inherited AbstractQueuedSynchronizer internal class Sync . Visible CountDownLatch is a synchronizer that is implemented based on the AQS framework. A similar synchronizer has a lot of juc under it. (eg. Semaphore )

AQS usage

As mentioned above, Aqs manages a single integer about the state information that can represent any state. For example, use it to show the Semaphore remaining number of ReentrantLock licenses, and use it to show how many locks have been requested by the thread that owns it, and FutureTask use it to show the status of the task (not yet started, run, completed, and canceled).

For details, see:

Https://www.cnblogs.com/waterystone/p/4920797.html

73824757

78552741

Introduction to CAS and AQS of Java concurrency

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.