java - volatile keyword
Posted
by Tiyoal
on Stack Overflow
See other posts from Stack Overflow
or by Tiyoal
Published on 2010-06-03T16:44:36Z
Indexed on
2010/06/03
16:54 UTC
Read the original article
Hit count: 331
Say I have two threads and an object. One thread assigns the object:
public void assign(MyObject o) {
myObject = o;
}
Another thread uses the object:
public void use() {
myObject.use();
}
Does the variable myObject have to be declared as volatile? I am trying to understand when to use volatile and when not, and this is puzzling me. Is it possible that the second thread keeps a reference to an old object in its local memory cache? If not, why not?
Thanks a lot.
© Stack Overflow or respective owner