Solved

performance between Singleton and non singleton

Posted on 2010-09-13
8
639 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:
gurvinder372 earned 84 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 167 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 249 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 249 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 167 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 249 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

734 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