Java Multi-threaded---no lock

Source: Internet
Author: User
Tags cas

Lock-Free is an optimistic strategy that uses CAS (compare and swap) to identify thread conflicts and, if a conflict occurs, retries the current operation until there is no conflict.

CAS (v,e,n) V variables to be updated, E for expected values, N for heart values

V is set to n only when V=e is present if v!=e indicates that another thread has made updates to the current thread and does nothing

CAS operations can find other threads interfering with the current thread and handle it appropriately, even without a lock.

Atomicinteger

#final boolean compareandset (int expect, int u)//If the current value is expect set to u

Compareandset----> Unsafe.compareandswapint (objuect obj,long valueoffset,int expect,int Update)

Static fields are not supported

in fact, CAS is also a lock operation, but is triggered by the CPU, more than the synchronized performance better . ---http://www.jianshu.com/p/9f0ba2bab24e

Your application cannot use the unsafe class it is a proprietary class used inside a JDK

Atomicreference//Guaranteed thread safety when modifying object references

Atomicstampedreference//Apply to "one-time modification" at this point, it is not only more than the value of the time stamp

Atomicintegerarray Atomiclongarray Atomicreferencearray

Atomicintegerfieldupdater

Using the reflection mechanism

Variables need to be defined as volatile and cannot be private static type

Java Multi-threaded---no lock

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.