Results 1 to 15 of 15
- 01-06-2010, 09:25 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Using Synchronized keyword in Singleton Design Pattern
Below is a code of a singleton design pattern. Got some doubt mentioned at the bottom.
public class SingleTon implements Cloneable
{
private SingleTon()
{
}
private static SingleTon singleTonInstance;
public static synchronized SingleTon getInstance()
{
if (singleTonInstance == null)
singleTonInstance = new SingleTon();
return singleTonInstance;
}
protected Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}
Statement : If getInstance() is called by thousand threads at a time and lets say getInstance() takes 1 sec for each execution; then it takes 1000 sec for all the threads to complete.
Question : What could be the solution to this?
- 01-06-2010, 09:41 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
See Singleton pattern - Wikipedia, the free encyclopedia
For options.
- 01-06-2010, 12:06 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Thanks. wz really helpfull.
- 01-08-2010, 01:48 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
- 01-08-2010, 02:06 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
As explained on wiki, the SingletonHolder class is only loaded on the very first call of getInstance. The instance is created on class loading. So creation of the instance though started by a thread by calling getInstance, is actually completed by the JVM when loading the SingletonHolder class.
Last edited by r035198x; 01-08-2010 at 02:10 PM.
- 01-08-2010, 02:23 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
As said : So creation of the instance though started by a thread by calling getInstance, is actually completed by the JVM when loading the SingletonHolder class.
Now What I want to understand from the above statement is if two thread are calling getInstance at the same time wont it create 2 instances?
If no then how exactly is it doing the same?
Till now what i have understood from the wiki is the only use of inner class is just not create the instance at the class laoding time. instead created at the first call to getnstance.
please correct me if i am going wrong anywhere.
- 01-08-2010, 02:28 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Two instances wont get created because the code fore creating the instance is in a static initializer of the class which is called by the JVM when the class is loaded. The class is only ever loaded once and the initialzer runs soon after (only) once. So the instance is not created on the calling thread but by the JVM. That's why I said that the creation is completed by the JVM not by the calling thread.
- 01-08-2010, 02:37 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Hi r035198x,
One last question.
So, instead of writing an inner class if i write in below way what would be the difference other than lazy loading?
public class SingleTon
{
private static final Singleton INSTANCE = new Singleton();
private SingleTon()
{
}
private static SingleTon singleTonInstance;
public static SingleTon getInstance()
{
return INSTANCE ;
}
}
- 01-08-2010, 02:42 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Your SingleTon class will be loaded and the instance created before any calls to getInstance are made at all. i.e Your solution does not have lazy initilization of the singleton since your SingleTon class is always loaded.
More helpful links are
Initialization on demand holder idiom - Wikipedia, the free encyclopedia
Double-checked locking - Wikipedia, the free encyclopedia
- 01-08-2010, 03:04 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
To be honest, the only advantage of the lazy loading one comes if you have a Class with other static methods in it beyond the getInstance(), otherwise it's next to pointless.
- 01-08-2010, 03:22 PM #11
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Hi,
thanks got it. :)
- 01-08-2010, 04:09 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 01-08-2010, 04:23 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Well, the class won't be loaded until the getInstance() is called anyway...in almost every circumstance I can think of. So the inner class gains you nothing, as far as I can see.
- 01-08-2010, 04:35 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Last edited by r035198x; 01-08-2010 at 04:36 PM. Reason: each ->its
- 01-08-2010, 04:43 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
using singleton pattern in java
By anandjain1984 in forum New To JavaReplies: 1Last Post: 12-16-2009, 02:57 PM -
Singleton Pattern
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:21 PM -
singleton pattern
By Peter in forum Advanced JavaReplies: 1Last Post: 07-09-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks