In Java, all instance, static, and array elements are stored in heap memory, and heap memory is shared between threads. Local variables, method definition parameters, and exception handler parameters are not thread-
Shared between them, they will not have memory visibility issues and are not affected by the memory model.
Communication between Java threads is controlled by the Java Memory Model (JMM), and JMM determines when a thread's write to a shared variable is visible to another thread. From an abstract point of view, JMM
Defines the abstract relationship between the thread and the main memory: Shared variables between threads are stored in main memory, and each thread has a private local memory (local memory), where the thread is stored in a copy of the read/write shared variable. Local memory is an abstract concept of JMM and is not really there. It covers caching, write buffers, registers, and other hardware and compiler optimizations. The Java memory model is abstracted as follows:
From the view, if you want to communicate between thread A and thread B, you have to go through the following 2 steps:
1. First, thread a flushes the updated shared variables in local memory A to the main memory.
2. Then, thread B to main memory fetches the shared variable that was updated before thread A.
1. Java Memory model