The principle and difference between static and volatile, just read this article

static


Indicates that the variable is in the method area and shared by all threads.
The JVM memory structure divides the memory into heap, stack, method area, local method stack and program counter. Among them, the heap and method area are shared by all threads and become main memory in the memory model, while the stack and program counter are private to the thread and become working memory in the memory model.

Summary
JVM memory = heap + stack + method area + local method stack + program counter       
main memory = heap + method area (shared by all threads)
working memory = stack + program counter (thread private) I

mentioned that static is in the method area. There are two threads, one to write and one to read, can you read the latest value right away, not necessarily!
Here to talk about, the JVM specification defines the interaction between threads and memory:
Lock (lock): Act on variables in the main memory, a variable is identified as a thread exclusive state.
Read: Act on variables in the main memory, and transfer the value of a variable from the main memory to the working memory of the thread.
Load: Act on variables in the working memory, and put the value of the variable obtained from the main memory by the read operation into the variable copy of the working memory.
Use: Act on the variable in the working memory, and pass the value of a variable in the working memory to the execution engine.
Assign (assignment): Act on the variable in the working memory, and assign a value received from the execution engine to the variable in the working memory.
Store: Act on variables in the working memory, and transfer the value of a variable in the working memory to the main memory.
Write: Act on the variables in the main memory, and put the values ​​of the variables obtained from the working memory in the store operation into the variables in the main memory.
Unlock (unlock): Act on variables in the main memory, release a variable in a locked state, and then be locked by other threads.

In the above paragraph, as long as you know that a copy of the variables in the main memory is kept in the working memory , the write operation may not be able to update the main memory immediately, and the read operation may not be able to immediately read the updated value in the main memory. This is the same as CPU time slice is related, whether you can read the latest value depends on fate.

to sum up

Static solves the problem of sharing data among all instances, but cannot solve the problem of synchronization between main memory and working memory under multi-threaded conditions. To put it bluntly, one thread updates the value, and another thread may not be able to get the latest value immediately. So volatile played a role.

 

volatile

Guarantee visibility, not atomicity

Volatile ensures that the latest value is taken from the main memory before each working memory operation, and solves the problem of memory invisibility, which static cannot do.
The defect does not guarantee atomicity.
Example: For
  example, if two threads perform i++ operations on a volatile variable 1000 times, the actual value of i will be <=2000, why?
 After a thread has manipulated the copy variable in the working memory, it will be updated to the main memory. During the process of updating the main memory, the lock operation will be performed. At this time, if another thread also wants to update the same variable in the main memory, it will This operation is automatically invalidated, and the value in the main memory is read again after the previous thread is updated.
  Therefore, there may be cases where i++ fails multiple times, and the value is less than or equal to 2000.

The difference between static and volatile

Same point:

1. They can all achieve the purpose of sharing by all threads.

2. For non-atomic operations i++ are thread-unsafe, and data duplication may occur.
difference:

1. static does not guarantee visibility and may not be able to get the latest value in main memory.

2. Volatile guarantees visibility and guarantees to get the latest value in main memory.

Volatile application scenarios

Speaking of volatile, I have to mention that in his application scenarios, static can be seen everywhere, and volatile is really rarely used.

1. The counter cannot be used, and the data may be duplicated, resulting in dirty reads. I++ operation

2. The status flag can be used.

One thread changes the boolean flag, and the other thread uses this boolean variable. Using volatile can ensure that the latest value can be obtained immediately, and every time it goes to the main access.

The other scenes are the same, so keep these two points in mind.


 

Guess you like

Origin blog.csdn.net/x18094/article/details/108429511