File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes How singleton object behaves in multithreading environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How singleton object behaves in multithreading environment" Watch "How singleton object behaves in multithreading environment" New topic
Author

How singleton object behaves in multithreading environment

samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Hello ranchers
Can somebody please tell me how does a singleton object behaves when multiple threads tries to access it. Does it get locked or does each thread carries a separate but identical copy of the same object when multiple threads tries to access it.? Any link white paper will greatly be appreciated .
Thanks a in advance
Samir
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

A true singleton will be one single instance shared by many threads. The developer of the singleton class needs to take care it's properly synchronized.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Right, the singleton behaves just like any other object once it is created. It has no special cases for multi-threaded applications, no protections against concurrent activities, and no inherent synchronization. You must write all those protections yourself.

Steve
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Thank you so much for the quick response Rob and Steave.
Though i am still little confused about what happens internally when thread tries to invoke method on an object which is of Truly singlton class.Does the thread takes the LOCK over the object and then invoke the method ? If this is true then I guess all singlton will be inherently synchronized . Am I thinking in right direction ? Or there is something else that is happening ?
Thanks in advance
Samir
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Unless you've explicitly made the singleton synchronized in some way, there is no locking at all. You will need to add all synchronization yourself, you don't get it "for free" because your object happens to be a singleton. The compiler doesn't know it is, the JVM doesn't know it is, only you and people using it know.
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Got it , thank you do much Rob . That helped
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 62
There is no relation between Singleton and LOCK.

Only thing to ensure is that the object creation happens only once. For which we need synchronized access else there is no relation of a LOCK and a singleton object
Sachin Bhosale
Greenhorn

Joined: Jul 06, 2009
Posts: 7

There is very nice article on implementing Singleton in multi-threaded environment. This should answer your most of the questions.
http://www.ibm.com/developerworks/java/library/j-dcl/index.html

Regards,
Sachin Bhosale (SCJP 1.4 | SCWCD 1.4 | SCEA 5 | IBM Certified SOA Solution Designer)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How singleton object behaves in multithreading environment