The volatile keyword is used as a modifier on member variables to force individual threads to reread the variable’s value from shared memory every time the variable is accessed. In addition, individual threads are forced to write changes back to shared memory as soon as they occur. This way, two different threads always see the same value for a member variable at any particular time.
不对把,我记得是会拷贝的。
对于volatile修饰的变量,jvm虚拟机只是保证从主内存加载到线程工作内存的值是最新的,因此才会由volatile不能完全确保线程安全这样的问题。比如自增自减
threads to reread the variable’s value from shared memory every time the variable is
accessed. In addition, individual threads are forced to write changes back to shared
memory as soon as they occur. This way, two different threads always see the same
value for a member variable at any particular time.