| Author |
Replacing Singleton class with Synchronized behaviour
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi ,
Can anybody please tell me
What s the need of creating Singleton classes , cant we achive the same thing using a Synchronized method ??
Sorry if its a dump question , but help me in knowing the answer .
Thanks .
|
Save India From Corruption - Anna Hazare.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
You are comparing carrots and chickens here Singleton serve one purpose, which is basically to hold a single instance of an object. Concurrency issues have to be taken into consideration if many threads access a Singleton. A synchronized method does something, anything, and prevent multiple threads to execute it at the same time. You may have some synchronized methods in a singleton, to prevent concurrency issues mentioned previously.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
The reason why i posted this question is taht , we basically create a Singleton class as we want a Single Point of Entry in the Application .
Singleton class for Single point of Entry in the Application .
Synchronized methods in a class : Single point of Entry in the class itself
Later after your response , it was like comparing carrots and chickens
Thanks .
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Use a singleton to assure one point of entry. Then add synchronization to
enforce "single-file" access to its behavior (by multiple threads).
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
Ravi Kiran V wrote:
Synchronized methods in a class : Single point of Entry in the class itself
Also search for static holder idiom, which is an efficient way to implement singleton.
|
http://muhammadkhojaye.blogspot.com/
|
 |
 |
|
|
subject: Replacing Singleton class with Synchronized behaviour
|
|
|