java-Multithreading in Depth (i) JMM

Source: Internet
Author: User

(i) Java Memory Area overview

When the JVM runs a Java program, it divides the managed memory into several parts: The method area, the Java stack, the local method stack, the Java heap, and the PC program counter.

After the class bytecode load is parsed, in a multithreaded environment, the method area and Java heap data are shared, each thread comes with a PC program counter and Java stack, and the stack frame contains all the states of the method (local variables, parameters, return values, intermediate results of the operation, etc.). Multithreading concurrency issues need to be considered for shared data.

More details can be found in the deep understanding of JVM virtual machines.


(ii) Computer system prototypes

1. Memory and processor speed are different, the cache appears, causing data to be out of sync

Multiple threads have a cache backup, write-back may result in data errors

2. Processor and compiler optimization of code and instructions, resulting in operation Chaos

The processor can adjust the order of code execution as long as it does not affect the results of the operation, resulting in data errors in multithreaded operations


(iii) Overview of JMM

JMM (Java momery model) provides a thread for communication, ensuring mutual exclusion and visibility.

(1) Resolving memory visibility issues


The shared data is put into main memory, and each thread has its own working memory.

When thread a reads the data, it first copies the data from the main memory to the working memory, then writes back from the working memory to the main memory, and the threads cannot access each other's data and communicate through the main memory.

JMM can control the specified shared data for memory visibility, and synchronously updates variable values in other threads, such as the use of volatile variables, when writing to master memory.

(2) No reordering

The compiler optimizes the code, does not change the result of the operation, can realize the code reordering, the processor executes the instruction, does not change the result of the operation in the case, can realize the instruction reordering.

JMM can prohibit compiler code reordering and processor execution instruction reordering (by adding an inner mask).

Code Anatomy of Multithreading Concurrency problem

/** * Code Reordering Demo * * @author Peter_wang * @create-time 2015-1-8 pm 3:06:02 */public class Codechangedemo {private Stati    C Boolean ischange = false; private static class Changethread extends Thread {public void run () {System.out.println ("change Thread start ");                          1 Ischange = true;    2};    }; private static class Getchangethread extends Thread {public void run () {System.out.println ("Get            Changethread start ");                if (ischange) {//3 ischange = false;            SYSTEM.OUT.PRINTLN ("Change Success");                } else {System.out.println ("Change no success");            System.exit (0);    }        };    }; /** * @param args */public static void main (string[] args) {while (true) {Changethread cha            Ngethread = new Changethread (); Getchangethread GetchangethRead = new Getchangethread ();            Changethread.start ();            Getchangethread.start (); Prevent too many threads, wait until the two threads are executed and then continue while (Thread.activecount () > 1) {}}}}
Operation Result:

Changethread start
Getchangethread start
Change success
Changethread start
Getchangethread start
Change success
Changethread start
Getchangethread start
Change no success
Process exits possible causes:

1. Thread Changethread in Code order adjustment, 1 and 2 swap, execution order 1-3-2

2. After the thread Changethread executes, the Ischange in the working memory does not write back back to the main memory, causing the Ischange in 3 to be not the latest value.


Reference documents:

1. "In-depth understanding of JVM virtual machines"

2.http://ifeve.com/java-memory-model-1/

java-Multithreading in Depth (i) JMM

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.