Is Singleton class thread safe?
I'll answer
Earn 20 gold coins for an accepted answer.20
Earn 20 gold coins for an accepted answer.
40more
40more
Studied at the University of Melbourne, Lives in Melbourne, Australia.
In the above code, getInstance() method is not thread safe. Multiple threads can access it at the same time and for the first few threads when the instance variable is not initialized, multiple threads can enters the if loop and create multiple instances and break our singleton implementation.
2023-05-14 04:45:24
评论(499)
Helpful(122)
Helpful
Helpful(2)

Lucas Wilson
QuesHub.com delivers expert answers and knowledge to you.
In the above code, getInstance() method is not thread safe. Multiple threads can access it at the same time and for the first few threads when the instance variable is not initialized, multiple threads can enters the if loop and create multiple instances and break our singleton implementation.