保证共享变量的可见性缓存
禁止进行指令重排序多线程
Volatile fields are special fields which are used for communicating state between threads. Each read of a volatile will see the last write to that volatile by any thread; in effect, they are designated by the programmer as fields for which it is never acceptable to see a "stale" value as a result of caching or reordering. The compiler and runtime are prohibited from allocating them in registers. They must also ensure that after they are written, they are flushed out of the cache to main memory, so they can immediately become visible to other threads. Similarly, before a volatile field is read, the cache must be invalidated so that the value in main memory, not the local processor cache, is the one seen. There are also additional restrictions on reordering accesses to volatile variables.app
Volatile字段是用来线程间交流状态的特殊字段。线程对volatile的每次读,都会看到其余线程对它的最后一次写,也就是说,volatile是随时最新的。实际上,volatile被设计为,在缓存或重排序中从不会失效的。JMM不容许编译器和运行时将volatile字段分配到寄存器中,只能在写入后,刷进主内存中,所以volatile字段的值老是对其余字段可见的。类似的,在读取前,会先清空本地处理器缓存,所以就能够看到主内存中的最新值了。此外,JMM对volatile变量的访问重排序也有限制。ui
Under the old memory model, accesses to volatile variables could not be reordered with each other, but they could be reordered with nonvolatile variable accesses. This undermined the usefulness of volatile fields as a means of signaling conditions from one thread to another.线程
在旧的内存模型下,对volatile变量的访问,不能被重排序,但访问非volatile变量却能够重排序。这破坏了volatile在多线程中做为一个状态信号的做用。翻译
Under the new memory model, it is still true that volatile variables cannot be reordered with each other. The difference is that it is now no longer so easy to reorder normal field accesses around them. Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire. In effect, because the new memory model places stricter constraints on reordering of volatile field accesses with other field accesses, volatile or not, anything that was visible to thread A when it writes to volatile field f becomes visible to thread B when it reads f.设计
在新内存模型下,volatile变量一样不能被重排序,区别在于,volatile变量周围的普通字段也不会被轻易重排序。在内存的效果上,写volatile与释放监视器相同,读volatile与获取监视器相同。事实上,当对volatile及其余字段的访问在一块儿的时候,因为新内存模型更加严格的重排序约束,无论变量f是否volatile,当A线程写f时,对于任何对其可见的变量,另外一条正在读f的线程也能看到。rest
这部分翻译很蛋疼,彻底不知道原文什么意思,可是看过参考文章后,就立马明白了。对volatile变量访问的语句,前(后)面的不能重排序到后(前)面,可是前(后)面的仍是能够重排序的。code
Here is a simple example of how volatile fields can be used:orm
如下是volatile字段的用法:
class VolatileExample { int x = 0; volatile boolean v = false; public void writer() { x = 42; v = true; } public void reader() { if (v == true) { //uses x - guaranteed to see 42. } } }
Assume that one thread is calling writer, and another is calling reader. The write to v in writer releases the write to x to memory, and the read of v acquires that value from memory. Thus, if the reader sees the value true for v, it is also guaranteed to see the write to 42 that happened before it. This would not have been true under the old memory model. If v were not volatile, then the compiler could reorder the writes in writer, and reader's read of x might see 0.
假设一条线程调用writer,另外一条调用reader。writer中v的写入会将x的写入发布到内存中,而reader中v的读取,会从内存中获取x。所以,当reader看到v是true的时候,确定也会看到在v以前赋值的x,且x已是42了。若是v不是volatile,那么writer中可能重排序,读到的x就有可能为0.
Effectively, the semantics of volatile have been strengthened substantially, almost to the level of synchronization. Each read or write of a volatile field acts like "half" a synchronization, for purposes of visibility.
实际上,volatile的语义被增强了,几乎达到了同步的水平。为了可见性,每一个volatile的写入都至关于半个同步。
Important Note: Note that it is important for both threads to access the same volatile variable in order to properly set up the happens-before relationship. It is not the case that everything visible to thread A when it writes volatile field f becomes visible to thread B after it reads volatile field g. The release and acquire have to "match" (i.e., be performed on the same volatile field) to have the right semantics.
注意:为了设置合适的happens-before关系,每条线程都应该访问相同的volatile变量。
感受翻译了半天仍是挺晕的,没理解并且抱有疑问,因而又找到如下文章《深刻理解volatile关键字》,做者思路清楚,文中所述至关于这一系列的翻译总结,且更加适合阅读。