I have a class Item
class Item {
public int count;
public Item(int count) {
this.count = count;
}
}
Then, I will put a reference
Yes, there is a visibility problem, as Holder.item is not final. So there is no guarantee that another thread will see its initialized value after the construction of Holder is finished.
And as @Gray pointed out, the JVM is free to reorder instructions in the absence of memory barriers (which can be created by a lock (synchronization), a final or volatile qualifier). Depending on the context, you must use one of these here to ensure safe publication.