CAS (Compare and Swap) understand

Source: Internet
Author: User

What is CAs (Compare and Swap)?

Hardware Synchronization Primitives!! What is the name of the egg ache, the average person is difficult to understand. According to the English full name translation = = comparison and exchange, the name can generally understand a little, so now for the first time so understand it.

What's the use?

For commonly used multi-threaded programming people estimate know, for the average person estimates have never heard. before Jdk5, we know that in multithreaded programming, in order to ensure that multiple threads on an object at the same time access, we need to add a synchronous lock synchronized, to ensure the correctness of the object in use, but the locking mechanism will lead to the following problems

1. With multi-threaded competition, locking and releasing locks can lead to more context switching, causing performance problems.

2. Multithreading can cause a deadlock problem.

3. Multiple threads holding a lock can cause the other thread that needs this lock to hang.

4 ...

is there a better way?

Categories of Locks: Exclusive lock (Pessimistic lock), optimistic lock

Exclusive Lock:synchronized is an exclusive lock that causes all threads that need this lock to hang and wait for the lock to be released.

Optimistic lock: Each time without locking to complete the operation, if the conflict failed to retry, until successful.

The mechanism of CAS is equivalent to this (non-blocking algorithm), CAS is implemented by CPU hardware, so execution is fairly fast. CAS has three operating parameters: memory address, expectation, new value to be modified, when the expected value and memory values are not equal, indicating that the memory value has been changed by the other thread, the failure is returned, when equal, the value in memory is changed to the new value, and return success.

code implementation in Java

In the Java.util.concurrent.atomic package can see the implementation of the source code, here Java.util.concurrent.atomic.AtomicLong source code, paste out the core code:

//+1 Operation     Public Final Longgetandincrement () { while(true) {            LongCurrent =get (); LongNext = current + 1; //when the +1 operation succeeds, return directly, exit this loop            if(Compareandset (current, next))returnCurrent ; }    }    //invoking JNI to implement CAS     Public Final BooleanCompareandset (LongExpectLongupdate) {    returnUnsafe.compareandswaplong ( This, Valueoffset, expect, update); }

CAS (Compare and Swap) understand

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.