I know that constructors cannot be synchronized in Java. Does this mean that if a constructor modifies a static variable within the class, and if constructors could be invoked from multiple threads, that the access needs to be synchronized, as shown?
public class MyClass {
private static int count = 0;
public MyClass() {
synchronized(MyClass.class) {
count++;
}
...
}
}
Absolutely – after all, it’s accessing a shared resource, potentially via many threads. I would personally just use an
AtomicIntegerinstead.Note that we can now make the variable final as that variable doesn’t change value, and we don’t need to synchronize any more as the whole point of the classes in java.util.concurrent.atomic is that they can be used atomically without extra synchronization.
Even if you are in the situation where you need to synchronize, I wouldn’t use
MyClass.classto do so – that’s a reference which other code might decide to synchronize on, so you can’t really reason about your code any more. I would write (again, only ifAtomicIntegerwasn’t good enough for some reason):(In this case you’d also want to synchronize on
countLockanywhere else you accesscount, even just for reading.)