Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par ajay deepak , software developer , IBM India
Date de publication: 2014/02/25
Maria Thomas
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?