If I decide to use a non-thread-safe collection and synchronize its access, do I need to synchronize any mutation in the constructor? For example in the following code, I understand the reference to the list will be visible to all threads post-construction because it is final. But I don't know if this constitutes safe publication because the add in the constructor is not synchronized and it is adding a reference in ArrayList's elementData array, which is non-final.
private final List<Object> list;
public ListInConstructor()
{
list = new ArrayList<>();
// synchronize here?
list.add(new Object());
}
public void mutate()
{
synchronized (list)
{
if (list.checkSomething())
{
list.mutateSomething();
}
}
}
ArrayList, LinkedList, HashSet,LinkedHashset and TreeSet in Collection Interface and HashMap,LinkedHashMap and Treemap are all non-synchronized.
Note that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.
It's also possible to achieve thread-safety using the set of atomic classes that Java provides, including AtomicInteger, AtomicLong, AtomicBoolean and AtomicReference. Atomic classes allow us to perform atomic operations, which are thread-safe, without using synchronization.
Thread safety is a property that allows code to run in multithreaded environments by re-establishing some of the correspondences between the actual flow of control and the text of the program, by means of synchronization.
Update: The Java Language Specification states that the freeze making the changes visible must be at the end of the constructor, which means your code is correctly synchronized, see the answers from John Vint and Voo.
However you can also do this, which definitely works:
public ListInConstructor()
{
List<Object> tmp = new ArrayList<>();
tmp.add(new Object());
this.list = tmp;
}
Here we mutate the list object before assigning it to the final
field, and so the assignment will guarantee that any changes made to the list will also be visible.
17.5. final Field Semantics
The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.
The highlighted sentence gives you a guarantee that this solution will work. Although, as pointed out at the start of the answer, the original has to work too, but I'll leave this answer here as the specification is slightly confusing. And because this "trick" also works when setting non-final but volatile
fields (from any context, not just constructors).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With