• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

performance between Singleton and non singleton

I have a Java class which handles persistence of my application. I would like to each end user use this persistence later. Inside persistence layer, there is some shared resources.

1) One solution is that use persistence all the way as singleton among multiple user.
2) The other way, every one holds a persistence object, in shared place, I use synchronized method.

Which one has better performance? In my mind, method 2) would have high performance. Any suggestions?
0
JianJunShen
Asked:
JianJunShen
  • 3
  • 2
  • 2
  • +1
6 Solutions
 
Gurvinder Pal SinghCommented:
Go for singleton. Let me explain
Create a class with a private constructor, and a getInstance() method.
Have private static class level variable, in which you will hold the persisted or cached information and then expose methods to access those information.

If the information has to be updated regularly also, then use synchronized method, else do not use synchronized method since it is an extra over head.
0
 
objectsCommented:
better off to use a connection pool and let it handle connection access

> 1) One solution is that use persistence all the way as singleton among multiple user.

you're still going to need to synchronize access to the connetction
0
 
ksivananthCommented:
if you use shared objects in multiple threads, you would need synchronization, no matter its singleton or not.

if you are allowed to use multiple instances of shared resources, you can use ThreadLocal to avoid synchronization cost.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
JianJunShenAuthor Commented:
What about the performance: is shared ojects in multiple threads fast enough comparing each thread has its own object?
0
 
ksivananthCommented:
definitely the performance will be good if each thread has its own preinitialized objects but you should consider the cost of creating those objects!
0
 
objectsCommented:
synchronizing access to a single object is going to be slower
whether its fast enough depends on your application.

0
 
JianJunShenAuthor Commented:
What about in singleton object (the singleton object itself has no synchronized method, but there is a method call other objects synchronized method), in this case, what about performance if I make this singleton as non singleton? i.e. Is threads has own objects much faster than this singleton object?
0
 
ksivananthCommented:
sync... is needed only if there is a shared access, this is nothing to do with singleton/non-singleton. if a thread operate on its own instance of resources, then no synchronization is is needed which will be faster than sync'ed one but you need to look at the cost of having own resource for each thread.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now