`
aahyhaa
  • 浏览: 7900 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Volatile关键字的作用

    博客分类:
  • Java
阅读更多

It is possible for each thread to have a local 

stack and maintain copies of some variables there. If you define a variable as volatile, it tells 

the compiler not to do any optimizations that would remove reads and writes that keep the 

field in exact synchronization with the local data in the threads. In effect, reads and writes go 

directly to memory, and are not cached, volatile also restricts compiler reordering of 

accesses during optimization.

 

Basically, you should make a field volatile if that field could be simultaneously accessed by 

multiple tasks, and at least one of those accesses is a write. For example, a field that is used 

as a flag to stop a task must be declared volatile; otherwise, that flag could be cached in a                                                        

register, and when you make changes to the flag from outside the task, the cached value 

wouldn’t be changed and the task wouldn’t know it should stop. 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics