• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How singleton object behaves in multithreading environment

 
samir ware
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
samir ware
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it , thank you do much Rob . That helped
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic