Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Spring singleton beans thread-safe?

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.

like image 564
Mounica Reddy Avatar asked Jun 27 '13 12:06

Mounica Reddy


People also ask

Is a Spring bean thread safe?

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 class thread safe?

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.

How do I make Spring beans thread safe?

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.

Are Autowired bean thread safe?

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 .


5 Answers

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.

like image 127
a better oliver Avatar answered Oct 17 '22 23:10

a better oliver


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 :

  1. Don't use instance variables in multithreaded environment. OR
  2. Use synchronized block/keyword on methods wherever the instance variables are modified to avoid unexpected results.
like image 26
sunil bhardwaj Avatar answered Oct 17 '22 22:10

sunil bhardwaj


Spring singleton beans are NOT thread-safe just because Spring instantiates them. Sorry.

like image 4
Arthur Dent Avatar answered Oct 17 '22 22:10

Arthur Dent


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

like image 4
M Sach Avatar answered Oct 18 '22 00:10

M Sach


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.

like image 3
Ripudaman Singh Avatar answered Oct 18 '22 00:10

Ripudaman Singh