Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a method from a singleton object be called from multiple threads at the same time?

I have a component registered in Castle Windsor as a singleton. This object is being used in many other places within my application which is multithreaded.

Is it possible that the two objects will invoke the same method from that singleton at the same time or 'calling it' will be blocked until the previous object will get result?

Thanks

like image 214
Kris82 Avatar asked May 29 '14 11:05

Kris82


People also ask

Can multiple threads access a singleton?

It can be used in a single threaded environment because multiple threads can break singleton property as they can access get instance method simultaneously and create multiple objects.

What happens when two threads access Singleton at same time?

Now coming to your question: if you share your singleton object among multiple threads and access it concurrently, every single thread will execute Singleton object's portion of code, wrapped in its own execution. Also if you write a Thread.

How do you handle Singleton in multithreading?

Thread Safe Singleton in JavaCreate the private constructor to avoid any new object creation with new operator. Declare a private static instance of the same class. Provide a public static method that will return the singleton class instance variable.

Can multiple threads use the same object?

No. They all use the same object. However, the memory system design of a typical modern computer means that some of the state of an object may be held in machine registers and memory caches associated with more than one processor in a multi-processor system.


2 Answers

You can call a Singleton object method from different threads at the same time and they would not be blocked if there is no locking/ synchronization code. The threads would not wait for others to process the result and would execute the method as they would execute methods on separate objects. This is due to the fact that each thread has a separate stack and have different sets of local variables. The rest of the method just describes the process as to what needs to be done with the data which is held the variables/fields.

What you might want to take care of is if the methods on the Singleton object access any static methods or fields/variables. In that case you might need to work on synchronization part of it. You would need to ensure multi-threaded access to shared resources for the execution of the method to be reliable.

To be able to synchronize, you might need to use lock statement or other forms of thread synchronization techniques.

You might want to refer to this article from Wikipedia which provides information on C# thread local storage as well.

like image 131
Shakti Prakash Singh Avatar answered Oct 13 '22 11:10

Shakti Prakash Singh


You can call the same method or different methods on one object simultaneously from different threads. In the specific methods you'll need to know when sensitive variables are being accessed (mostly when member-variables are changing their values) and will need to implement locking on your own, in order to solve lost updates and other anomalies.

You can lock a part of a code with the lock-statement and here an article on how Thread-Synchronization works in .Net.

like image 22
peter Avatar answered Oct 13 '22 11:10

peter