The underlying implementation of CAs in Java Atomic class

Source: Internet
Author: User
Tags cas

The underlying implementation of CAs in Java Atomic class

From Java to C + + to the assembly, the underlying principles of CAs are explained in depth.

Before introducing the principle, start with a demo

Take the Atomicboolean class as an example. Let's start with a demo that calls CAs.

The main thread in the For statement CAS is busy looping until the CAS operation successfully returns TRUE.

The newly opened county new thread will set flag to true after 4 seconds, in order for the main thread to be able to set the success. (because the expected value of CAS is true and flag is initialized to false)

The phenomenon is that the main thread has been running for a loop. After 4 seconds, the main thread will set the success, then output the time difference, and then terminate the For loop.

public class Testatomicboolean {public    static void Main (string[] args) {        Atomicboolean flag = new Atomicboolean (f Alse);        Long start = System.currenttimemillis ();        New Thread (()->{            try {                thread.sleep (4000);            } catch (Interruptedexception e) {                e.printstacktrace () ;            }            Flag.set (True);        }). Start ();        for (;;) {            if (Flag.compareandset (True,false)) {                System.out.println (System.currenttimemillis ()-start);                System.out.println ("Inner Loop ok!");                Break;}}}}    

Here is just one example, perhaps this is not a good example, this article just lists the API call method, the focus is to introduce the Compareandset () method of the underlying principle.

Java level source Atomicboolean.java

It is found that the Atomicboolean Compareandset () is called the Compareandswapint () method in unsafe.

Java level source Unsafe.java

Some students may be curious, the unsafe is how to come.

Static member variables in the Atomicboolean class:

If you want to scrutiny unsafe.getunsafe () How to achieve the words .... Then I'll put a copy of the Getunsafe code in the unsafe class:

First, in the unsafe class, you have an instance of yourself. (Single case)

The Getunsafe () method in the unsafe class is then checked and will eventually return to the Singleton Theunsafe.

Just ran to fetch introduced the Getunsafe () method ... Then continue to explain the CAS ...

Just now, the Compareandset () method inside the Atomicboolean class is actually called the Compareandswapint () method in the unsafe class.

The Compareandswapint source code in the unsafe category is as follows:

(OpenJDK8 source path: Openjdk/jdk/src/share/classes/sun/misc/unsafe.java)

Found here is a native method. The explanation continues to look at the source word, from here began to leave the Java language ....

C + + level source Unsafe.cpp

The path of the source code in the OpenJDK8 is: openjdk/hotspot/src/share/vm/prims/unsafe.cpp

(Here is a temporary digression: if you want to scrutiny unsafe_entry is what ... Unsafe_entry is Jvm_entry, and jvm_entry is defined in INTERFACESUPPORT.HPP, which is related to JNI. If you want to see, the path to the source path in OpenJDK8 is this:

OPENJDK/HOTSPOT/SRC/SHARE/VM/RUNTIME/INTERFACESUPPORT.HPP)

Back to the topic of this article CAs .... This code above, the last sentence is return, found that the use of the atomic under the Cmpxchg () method.

C + + level source Atomic.cpp

This section of the source code corresponding to OpenJDK8 path is this: Openjdk/hotspot/src/share/vm/runtime/atomic.cpp

The CMPXCHG is the core content. However, this code uses different underlying code depending on the operating system and processor.

In ATOMIC.INLINE.HPP, the statement is as follows:

Visible... Different operating systems, different processors, have to go different cmpxchg () method implementation.

Let's take the Linux OS x86 processor as an example, ATOMIC_LINUX_X86.INLINE.HPP

Compile-level source atomic_linux_x86.inline.hpp

The path in OPENJDK is as follows: OPENJDK/HOTSPOT/SRC/OS_CPU/LINUX_X86/VM/ATOMIC_LINUX_X86.INLINE.HPP

See the __asm__, the C + + to start the inline assembly, said continue to look at the code, will be assembly language.

This is an inline assembly:

Not to be continued .... Sleepy to sleep .....

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