Previous:
Understanding GC in the JVM
We all know that there will be concurrency problems when multiple threads operate on one data at the same time, so why do concurrency problems occur, and what are the reasons for concurrency problems?
Causes of concurrency issues
Generally, concurrency problems are related to the following three characteristics
- atomicity
- visibility
- orderliness
Atomicity:
It is easier to understand that when multiple threads are executing the same task, one of the threads cannot switch to other threads in the middle of execution to execute the method, otherwise there will be problems with incorrect data. For example, the classic ticket selling problem under multi-threading. We can solve this problem by implementing data synchronization with locks.
Now let's look at the visibility issue
Visibility issues in concurrency
Let's look at a piece of code first:
class JMMDemo {
private boolean flag = true;