Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How do you distinguish between using singleton class and making multiple threads pass through single instance by using synchronize?

user-image
Question added by ajay deepak , software developer , IBM India
Date Posted: 2014/02/25
Maria Thomas
by Maria Thomas , Tech Lead , J P Morgan Chase

Singleton design pattern is not to be confused with multi threading in a single instance. In singleton pattern only one instance for the class will be created at all times. This is useful in case of using logger objects which are used across multiple classes and so a single instance is preferred to avoid resource overhead.

In case of multi-threading multiple objects of the class can be created. However, threads are created on a single instance of each class and access the key resources using synchronised keyword. 

A singleton class had to be protected in a multi-threaded environment by using synchronised keyword on its instance creation code. Else there is a possibility of multiple threads creating multiple objects. 

More Questions Like This