0 votes
in Design Patterns by
What would happen if we do not have a synchronized method for returning Singleton instance in a multi-threaded environment?

1 Answer

0 votes
by
In a multi-threaded environment, if we have a non-synchronized method for returning instances, there are chances that the method can create more than one instance. Consider that we have 2 threads and both enter the condition for checking if the instance already exists. Both threads will find that the instance has not been created and hence both will create the class instances. This goes against the principle of the Singleton pattern. Hence, in a multi-threaded environment, it is recommended to use synchronized checks.

Related questions

0 votes
asked Jul 29, 2023 in MemCached by sharadyadav1986
+1 vote
asked Jan 27, 2020 in JAVA by rahuljain1
...