Why doesn't volatile in Java update the value of a variable?

后端 未结 3 824
北恋
北恋 2021-01-25 10:36

I\'ve read that "volatile" in Java allows different threads to have access to the same field and see changes the other threads has made to that field. If that\'s the c

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 11:36

    volatile doesn't "allow the sharing" of anything. It just prevents the variable from being cached thread local, so that changes to the variables value occur immediately. Your d variable is an instance variable and is thus owned by the instance that holds it. You'll want to re-read the threading tutorials to re-align your assumptions.

    One decent reference is here

提交回复
热议问题