Does the following code do safe publication?
public MyThread extends Thread {
@Override
public void run(){
for(int i=0; i < 100; i++){
MyObject[] array = new MyObject[16];
for(int j=0; j < 16; j++){
array[i] = new MyObject(j);
array[i].memberAttribute++;
}
// At this point the array is assigned to a shared volatile
// MyObject[] ref or put inside a concurrent collection
}
}
}
Does Happens-before here apply to every newly created object or only to the array itself?
Even if the field that will hold j
inside MyObject
is not final
?