A brief overview of the java Memory Model and the volatile keyword, javavolatile

Source: Internet
Author: User

A brief overview of the java Memory Model and the volatile keyword, javavolatile

If we want to have a deep understanding of Java concurrent programming, we must first understand the Java memory model. The Java memory model defines the visibility of shared variables among multiple threads and how to synchronize shared variables as needed. The original Java memory model is not very efficient. Therefore, Java is reconstructed, and Java 8 still uses Java.

In java, each thread has a local memory, and the local memory stores a copy of the main memory. It is equivalent that when the thread starts, the thread will copy the main memory variable to its local memory. When the local memory variable changes, it is synchronized to the main memory. If multiple variable threads share variables, the main memory will refresh the variables for synchronizing all threads. This kind of synchronization is not real-time.

  

How does one prove that each thread has a master memory copy?

The following code is provided:

Package com. mylienkd; public class MyThread implements Runnable {private Integer nubmer = 0; @ Overridepublic void run () {while (true) {if (nubmer> 1) {System. out. println (Thread. currentThread () + "subthread loop ends"); break ;}} public void setNubmer (Integer nubmer) {this. nubmer = nubmer;} public static void main (String [] args) throws Exception {MyThread thread = new MyThread (); new Thread (thread ). start (); // sleep 1sThread. sleep (1, 1000); thread. setNubmer (10); // sleep 3sThread. sleep (1, 3000); System. out. println ("main thread ended ");}}

Run the code here to get the following results:

After carefully observing the code and results, we can find that after the main thread changes the nubmer variable to 10, the execution of the main thread ends, but the Console does not exit and is still running.

This is because the subthread does not exit the loop. This status occurs only when the number of the local memory of the sub-thread is 0 and the value in the main memory is not refreshed to the local memory of the sub-thread.

How to solve this problem. Java provides the volatile keyword to avoid this situation, and adds the volatile keyword to the number variable.

private volatile Integer nubmer=0;

Run this code to get the result.

  

We can see that this code exits the loop before the main thread exits. This is because the volatile keyword updates this variable in real time. When the number variable is changed, it is refreshed synchronously to change the copy variable of other threads.

The volatile keyword solves visibility in many places.

Related Article

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.