Y
yancheng.cheok
hi all,
i am a bit confused on the usage of volatile keyword. consider the
following statement
public class Me
{
public Me()
{
primitive = 0;
arraylist = new ArrayList(100);
}
volatile int primitive;
volatile ArrayList arraylist;
}
Says there are two Thread A and B, A modify primitive value to 100. B
tries to read primitive value. B will get the latest primiteve value
(100) since B is reading directly from true primitive memory location,
not its cache.
(Please correct me if the above statement is wrong)
For point object reference, A trying to change the object reference to
a new ArrayList(200). B tries to read point. B will get the latest
ArrayList(200) object since B is reading directly from true arraylist
object reference memory location, not its cache.
(Please correct me if the above statement is wrong)
However, how about the elements inside the arraylist itself? Says A
try to change the arraylist element by
arraylist.setElementAtLocation("new value", 37). When B tries to
access the arraylist element at 37th, will it read directly from the
true arraylist 37th element memory location, or there is a possibility
B will read from 37th element cache memory?
Hence, is it safe, that we declare a arraylist as volatile, and assume
that all threads is able to get its most updated "element value"?
Thank you very much!
cheok
i am a bit confused on the usage of volatile keyword. consider the
following statement
public class Me
{
public Me()
{
primitive = 0;
arraylist = new ArrayList(100);
}
volatile int primitive;
volatile ArrayList arraylist;
}
Says there are two Thread A and B, A modify primitive value to 100. B
tries to read primitive value. B will get the latest primiteve value
(100) since B is reading directly from true primitive memory location,
not its cache.
(Please correct me if the above statement is wrong)
For point object reference, A trying to change the object reference to
a new ArrayList(200). B tries to read point. B will get the latest
ArrayList(200) object since B is reading directly from true arraylist
object reference memory location, not its cache.
(Please correct me if the above statement is wrong)
However, how about the elements inside the arraylist itself? Says A
try to change the arraylist element by
arraylist.setElementAtLocation("new value", 37). When B tries to
access the arraylist element at 37th, will it read directly from the
true arraylist 37th element memory location, or there is a possibility
B will read from 37th element cache memory?
Hence, is it safe, that we declare a arraylist as volatile, and assume
that all threads is able to get its most updated "element value"?
Thank you very much!
cheok