Use of volatile in java concurrent programming

Volatile application

Volatile is a lightweight synchronized, which guarantees the "visibility" of shared variables in multi-processor development
(visibility means that when one thread modifies a shared variable, another thread can read the modified value).

Definition and realization principle of volatile

如果一个字段被声明成volatile,Java线程内存模型所有线程看到这个变量的值是一致的。

When a shared variable modified by a volatile variable is written, two things will happen when the Java code is converted into an assembly instruction under a multi-core processor:

将当前处理器缓存行的数据写回到系统内存(锁缓存不锁内存)  
这个写回内存的操作会使其他在cpu里缓存了该内存地址的数据无效,处理器通过嗅探重新讲内存数据读到缓存中。

Note: In order to improve processing efficiency, the processor does not directly manipulate the memory, first read the data in the system memory to the cache, and then manipulate the cache.

volatile optimization

JDK7新增LinkedTransferQueue,使用追加字节的方式来优化队列出队入队的性能。

Guess you like

Origin blog.csdn.net/weixin_44146509/article/details/109035391