Hi,
Given a singleton class, if too many threads start accessing the class, the performance will get hampered. SO how do we optimize the code for this?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Given a singleton class, if too many threads start accessing the class, the performance will get hampered. SO how do we optimize the code for this?
If you're not modifying the singleton (either externally or internally) then theoretically every thread can access the singleton mutually with no negative effects.
If you're able to partition the singleton into "sections" (for example, you have a 2D array and each thread operates on only a small block of the array), then again you can safely access the singleton without requiring synchronization.
If your threads require a complete lock on the singleton (or a partial lock that all threads need), then there's no optimization which can take place because at most only 1 thread can safely access the singleton. This is a design bottleneck and no amount of optimization can fix this. The only option is to redesign your application to avoid this case.
Without a more specific example (aka code) it's difficult to determine how to get more performance out of your code, or if this is even required.
Here is my singletonclss code
class SingletonClass { private static SingletonClass singletonObject; /** A private Constructor prevents any other class from instantiating. */ private SingletonClass() { // Optional Code } public static synchronized SingletonClass getSingletonObject() { if (singletonObject == null) { singletonObject = new SingletonClass(); } return singletonObject; } public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } }
Last edited by helloworld922; April 18th, 2011 at 07:58 PM.
On thing I can see right away is to get rid of the synchronization, as well as immediately initializing the singleton object (use a static initializer block, or just initialize it when you declare it). You're getter method can then simply return the singleton object without checking to see if it's null and creating the singleton object. This will allow multiple threads to retrieve the singleton object at the same time. However, there's no way for me to know how you're using that object which would say whether you can safely use that object in multiple threads at the same time.
I would agree with removing the synchronized keyword and rather instantiate your singleton directly at the static declaration.
You should really try to avoid using synchronized if there is no need as it just complicates the code but it's not always possible to do that.
If you leave the code as it is, it will be efficient enough, the getSingletonObject method does not really impose a high performance risk since all it does is return a value.
I myself prefer to use Google Guice these days so I don't see these issues, I never really need to call new on any class except for the odd rare occasion
tcstcs (April 19th, 2011)