The thread will enter the synchronized code only when the condition uniqueInstance == null sets to true, so what is the need to check it again inside the critical section?

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because in between the check and the synchronized call you could have been interrupted by another process which acquired the semaphore, wrote to the singleton, and quit. There’s a possibility that you’ll overwrite the value that the other process already initialized unless you check again.
On the other hand, if you make the whole method synchronized, you pay the cost of synchronization on every call to the singleton rather than the first call only. It’s better to check twice.