• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How singleton object behaves in multithreading environment

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it , thank you do much Rob . That helped
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic