Solved

performance between Singleton and non singleton

Posted on 2010-09-13
8
599 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 84 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:JianJunShen
Comment Utility
What about the performance: is shared ojects in multiple threads fast enough comparing each thread has its own object?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 249 total points
Comment Utility
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
Comment Utility
synchronizing access to a single object is going to be slower
whether its fast enough depends on your application.

0
 

Author Comment

by:JianJunShen
Comment Utility
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
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now