The concept of concurrency and parallelism:
Concurrency: Execute multiple threads within a period of time (assuming there is only one CPU), and execute in sequence when multiple threads
Parallelism: At the same point in time, multiple threads execute simultaneously (multiple CPUs)
What is concurrent programming?
In the application of modern Internet, there will be multiple requests to access shared resources at the same time, such as in the scenarios of buying tickets, flash sales and snapping up
At this time, the problem of thread safety will appear. Concurrent programming is to control multiple threads to execute sequentially through programming to prevent the problem of thread safety.
Root cause of concurrency issues
Multi-core CPU processing
Java-based memory model
JMM
The full name of JMM is Java Memory Model, which means the Java memory model, which is standardized in the java virtual machine.
The java memory model stipulates that all variables are stored in the working memory, and all threads share the variables in the working memory
Each thread will have its own private working memory, and the thread will read the shared variables in the working memory into the working memory as a cache
The core problem solved by concurrent programming
visibility
Visibility means that after a thread modifies a shared variable, other threads can immediately know
In the current multi-core CPU processing, after a single thread modifies the shared variable, other threads are invisible
orderliness
Sequence refers to the order in which the code runs in the order in which it was written
In the process of compiling and running the code, in order to improve performance, the CPU may disrupt the original writing order of the code. For example, when it needs to read a variable that takes a long time to read, it may skip it to read a variable with a faster speed.
This is caused by the read waiting mechanism of the CPU . When the CPU reads a variable with a long read time, it will read other variables at the same time.
atomicity
Atomicity refers to the atomicity of operations. When multiple instruction operations are performed at the same time, the consistent execution of these multiple commands should be guaranteed.
For example, the operation "i++" is a programming instruction in a high-level language, but in fact, it can be split into three CPU execution instructions when the CPU executes, which are respectively to read the value of the variable i from the main memory, and in the work Operate the value of the variable in memory, and then update the value in main memory
If the multi-core CPU supports thread switching scheduling at this time, it will cause the "i++" command to switch threads before the execution is completed, resulting in thread safety issues.
summary
The problem of visibility is caused by caching, the problem of order is caused by compilation, and the problem of atomicity is caused by thread switching
The starting point of the three questions is to improve the performance of the program, which is consistent with the purpose of concurrent programming