I'm not sure about the thread safety of one of my classes. Here is some code.
I assume it's Thread safe, but I'm just not sure. Can someone confirm or comment on this?
SCJP, SCWCD, SCJD
posted 7 years ago
it's not thread safe.
You must synchronize access to getInstance method.
In case when your singleton is not yet initialized (threadSafety == null) and two (or more) threads call getInstance method at the same time,
there is a chance that two (or more) singletons will be created.
To make this code thread safe either getInstance must be synchronized or singleton object must be created in the static initialization block like that: