ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل ajay deepak , software developer , IBM India
تاريخ النشر: 2014/02/25
Maria Thomas
من قبل 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. 

المزيد من الأسئلة المماثلة