This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Replacing Singleton class with Synchronized behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Replacing Singleton class with Synchronized behaviour" Watch "Replacing Singleton class with Synchronized behaviour" New topic
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
    
  16

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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing Singleton class with Synchronized behaviour
 
Similar Threads
singleton synchronized doubt
Example for java doubleton pattern.
Does Singleton has many definition?
Difference between synchronized(this) vs synchronized(someClass.class)
Singleton in multithreads