Thread safety in Java singleton mode

Thread safety in Java singleton mode

The access scope of the bean provided by the spring IOC container by default is the singleton mode. That is, there is only one instance in the entire application life cycle. Therefore, in the case of multithreading concurrency, there will be thread safety risks. Our servlet under MVC framework is thread safe. Because the servlet is on the client side, there are many concurrent services and relatively few. However, for the web service side, it needs to be considered.

ThreadLocal class: provides an independent variable (instance) copy for each thread, and accesses isolation from different instances. In the synchronous lock mechanism, the latecomer thread can access the member variable only after the previous thread completes. ThreadLocal implements instance replication and isolates object access data conflicts. At the same time, it can also solve the consumption and burden of a large number of instance life cycle management under the scope prototype access mode. It is two implementations of "time for space" and "space for time". The former only provides unique variables for different threads to queue up for access, while the latter provides a copy for each thread, so it can be accessed at the same time without affecting each other. At the same time, the copy is stored in memory, and the instance will not be regenerated during the next access, so as to reduce server resource consumption.

We know that in general, only stateless beans can be shared in a multithreaded environment. In spring, most beans can be declared as a singleton scope. This is because spring uses ThreadLocal to process non thread safe states in some beans (such as requestcontextholder, transactionsynchronizationmanager, localecontextholder, etc.), making them thread safe states, because stateful beans can be shared in multiple threads.

Thread safety problem: it is caused by global variables and static variables. If there are only read operations and no write operations for global variables and static variables in each thread, generally speaking, this global variable is thread safe; If multiple threads execute write operations at the same time, thread synchronization generally needs to be considered, otherwise thread safety may be affected.

1) Constants are always thread safe (constant value) 2) it is thread safe to create a new instance before calling a method. (different instances are isolated from each other) 3) local variables are thread safe (isolated)

Because each method will create a local variable in an independent space, it is not a shared resource. Local variables include method parameter variables and method internal variables.

Status: it has the functions of data storage and change. Stateful beans, objects with instance variables, can save data and are non thread safe.

Stateless: it is an operation and data cannot be changed. Stateless bean is an object without instance variables and cannot save data. It is an invariant class and thread safe. In spring, the singleton mode is to share instances to improve performance. Stateful beans are unsafe in multi-threaded environment, so prototype prototype mode is suitable. Prototype: each request for a bean creates a new bean instance.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>