volatile:声明一个共享变量,如:class VolatileVar{volatile int volatileV;……}由多个并发线程共享的变量可以用volatile来修饰,使得各个线程对该变量的访问能保持一致,(目前Java运行时系统忽视这一限定)。

解决方案 »

  1.   

    好像跟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的效果。