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
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply 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: 180
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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18368

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
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 1900

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: 180
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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18368

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: 180
Got it , thank you do much Rob . That helped
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 41
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
 
IntelliJ Java IDE
 
subject: How singleton object behaves in multithreading environment
 
Threads others viewed
Executing Stored Procs using EJB 3
How to accomplish this without threads?
Replacing Singleton class with Synchronized behaviour
clustered environment - Singleton class
Timer starts twice when it should start only once
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com