Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

performance between Singleton and non singleton

Posted on 2010-09-13
8
Medium Priority
?
643 Views
Last Modified: 2012-05-10
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
Comment
Question by:JianJunShen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 336 total points
ID: 33669308
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
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 33669484
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 996 total points
ID: 33669697
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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:JianJunShen
ID: 33669761
What about the performance: is shared ojects in multiple threads fast enough comparing each thread has its own object?
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 996 total points
ID: 33669774
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
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 33669778
synchronizing access to a single object is going to be slower
whether its fast enough depends on your application.

0
 

Author Comment

by:JianJunShen
ID: 33669844
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 996 total points
ID: 33669869
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question