Register now or log in to join your professional community.
Double checked locking is not recomended in programming best practices mainly due to the fact that it can be unsafe and in most places it is considered an Anti-Pattern. The case where double checked locking becomes unsafe is documented on Wikipedia
Thread B notices that the shared variable has been initialized (or so it appears), and returns its value. Because thread B believes the value is already initialized, it does not acquire the lock. If B uses the object before all of the initialization done by A is seen by B (either because A has not finished initializing it or because some of the initialized values in the object have not yet percolated to the memory B uses (cache coherence)), the program will likely crash.
There are effectively3 correct ways to acheive the same result, but each one comes with different performance impacts.
Performacne between using a final wrapper and volatile keyword may not be big, as always it is recommended to benchmark your options to suite your requirements.
Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.