Introduce a,
Volatile is a type modifier. Volatile is used as an instruction key to ensure that the instruction is not omitted for compiler optimizations
Second, the characteristics of
- This ensures visibility when different threads operate on the variable, i.e. when one thread changes the value of a variable, the new value is immediately visible to other threads. (Implement visibility)
- Command reordering is disabled. Volatile guarantees atomicity only for single reads/writes. I++ does not guarantee atomicity. Atomicity of volatile can be understood as a single read/write to a volatile variable being synchronized using the same lock.