当前位置: 技术问答>java相关
volatile是什么意思?
来源: 互联网 发布时间:2017-03-31
本文导语: volatile是做什么的,什么情况下需要这样一个关键字 | 好像跟c/c++里的含义不太一样,下面摘自《The Java ™ Language Specification Second Edition》: A field may be declared volatile, in which case a...
volatile是做什么的,什么情况下需要这样一个关键字
|
好像跟c/c++里的含义不太一样,下面摘自《The Java ™ Language Specification Second Edition》:
A field may be declared volatile, in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables onbehalf of a thread are performed by the main memory in exactly the order that the thread requested.
后一句意义很重要,如果有多个volatail变量,当多个线程都操作和访问它们时,如果每个线程访问的顺序是同样的,就能达到synchronized的效果。
A field may be declared volatile, in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables onbehalf of a thread are performed by the main memory in exactly the order that the thread requested.
后一句意义很重要,如果有多个volatail变量,当多个线程都操作和访问它们时,如果每个线程访问的顺序是同样的,就能达到synchronized的效果。