Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

singleton and performance

Hi: I have a multi thread Java program. There are many singletons. I am wondering whether singleton would decrease the performance of a multi thread program, especially throughput. Some Singleton is simply a single object, some singleton are concurrentHashmap and/or atomicinteger.

like image 320
user84592 Avatar asked Oct 25 '25 01:10

user84592


1 Answers

If your singletons are immutable then they won't cause a decrease in performance.

However as you said, if there are concurrent (synchronized) data structure singletons in your code then your performance will be decreased when compared to non-concurrent data structures.

Basically, think objects as data holders with some methods on them and now think that you have a linked list in your code and many threads are running concurrently and modifying that linked list. Analogous to this example, your singleton data structure is data holder and many threads are trying to update it and the system is staying consistent with locks.

Long story short, I don't think that having singletons in concurrent systems will make a significant difference in performance unless they hold a data structure updated by multiple threads.

like image 105
ahmet alp balkan Avatar answered Oct 27 '25 15:10

ahmet alp balkan