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
Solved

performance between Singleton and non singleton

Posted on 2010-09-13
8
627 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
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 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