First, the basic
problem : How to communicate between threads and how threads are synchronized.
Communication: How information is exchanged between multiple threads.
Means of communication: Shared memory (implicit) and message passing (explicit).
The corresponding concurrent programming model has two kinds: concurrent programming model of shared memory and concurrent programming model of message passing.
Synchronization: A mechanism for controlling the relative order of operations between different threads.
There are also synchronization and implicit synchronization shown.
The concurrent programming model for shared memory requires a display synchronization.
The concurrent programming model for message passing is synchronized implicitly.
Concurrent programming model for shared memory used by the Java Concurrency programming model.
Second, JMM
question : How does a thread in Java deliver a message?
Java heap memory is shared between threads, and the stack is not.
Instance fields, static fields, and arrays are all stored in the heap. called Shared variables.
JMM controls when a thread writes a shared variable to another thread when it is visible.
The following describes the relationship between local memory and main memory in the thread:
The shared variables in the thread are stored in main memory.
Each thread has a private local memory, and local memory is an abstraction that includes caching, write buffers, registers, other hardware, and compiler optimizations.
Steps for thread A and B communication:
1. Thread A first flushes a copy of the shared variable that may have changed locally into main memory.
2, then thread B reads the variable from main memory, and then updates the copy of its own variable.
从今天开始跑,加速度的跑、不要回头、当速度足够块的时候,你将看到一些你从未看到过的风景。 --安藤忠雄
A simple summary of the Java memory model