I want to know whether the Spring singleton beans are thread-safe, if yes then why, if not then why?
As I am beginner with spring so help would be appreciated.
Are Spring Beans Thread Safe? No. Spring has different bean scopes (e.g. Prototype, Singleton, etc.) but all these scopes enforce is when the bean is created.
Is singleton thread safe? A singleton class itself is not thread safe. Multiple threads can access the singleton same time and create multiple objects, violating the singleton concept. The singleton may also return a reference to a partially initialized object.
If you want to make sure your bean is thread-safe, you should go for the @RequestScope. As the name suggests, Spring binds such bean to a particular web request. Request beans aren't shared between multiple threads, hence you don't have to care about concurrency.
Answer: No. Spring don't give you thread safety for their bean. Spring provide different type of bean scope like (Prototype,Singleton etc). If Prototype then a new bean create each time it invoke where a singleton bean created for one time and shared in application context .
No. The two concepts are not even related.
Singletons are about creation. This design pattern ensures that only one instance of a class is created.
Thread safety is about execution. To quote Wikipedia:
A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.
So eventually thread safety depends on the code and the code only. And this is the reason why Spring beans are not thread safe per se.
I have different perception: Spring singleton beans are created once and there can be only one instance available at any point of time.
Lets say your have an instance variable which is modified in an non-synchronized method. In multi-threaded environment,same class instance will be assigned to all the threads and 2 concurrent threads can update/change the instance variables which may lead unexpected situation. Singleton beans does not provide thread safety and now you know that instance variables usage may lead to unexpected result, you have 2 options to solve the same :
Spring singleton beans are NOT thread-safe just because Spring instantiates them. Sorry.
Spring just manage the life cycle of singleton bean and maintains single instance of object. Thread safety has nothing to do with it.
if not then why?
Because singleton and thread safety are two different concepts. You can go for thread safety with synchronized keyword
Singleton Beans is thread safe or not depends on how the class whose scope is singleton is written. Each calling thread will have its own execution and does not interfere with another thread's execution unless there is some code in the singleton scoped class which is shared by all calling threads.e.g if a class has globally declared variables that is being accessed by its method and the values are modified then this may cause concurrency issue so it is better to have those variables at the method level and not at the class level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With