The application of CAs in Java class

Source: Internet
Author: User
Tags cas
Cas

This instruction full name compare and swap is the comparison substitution instruction, which is newly added to the modern processor instruction.

Guideline: Based on optimistic locking mechanism. Compares the value of a variable in memory value and the current value of the variable (old value). If it is equal, the variable is considered unchanged and the new value is used instead of the old value, otherwise the substitution fails.

Unsafe

In a multithreaded environment of Java programs, if a variable is accessed by multiple threads, to ensure thread safety, in addition to volatile, lock, final, and static methods, you can use Java-provided Sun.misc.Unsafe class

Two features of this class:

Platform-related (Java language Platform-independent), written in C language. The Compareandswap method is provided for the Java class.

Not in the Java API, we can only use some of the official Java-provided classes, such as atomic, random, etc.

Now that we have no way to use unsafe directly, let's look at how this class is used in the JDK.

The application of CAs in Java 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.