In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to keep the thread in the singleton mode in Spring safe". In the daily operation, I believe that many people have doubts about how to keep the thread in the singleton mode in Spring. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "how to keep the thread in the singleton mode in Spring". Next, please follow the editor to study!
Bean instances managed in Spring are singleton (sigleton type) by default, but singletons in Spring do not affect the concurrent access of applications.
E.g. In the process of passing from the client to the background controller-Service-Dao, these objects are singletons, so will these singleton objects have no problem passing entity bean?
Because the entity bean is not singleton and is not managed by Spring, it is manually New every time, so even if the business processing classes that deal with data are shared by multiple threads, the data they deal with is not shared, and each thread has its own share of data, so there will be no thread synchronization problems in terms of data.
In the Service referenced in Controller, these objects are singletons, but these classes usually do not contain member variables, so there is no thread synchronization problem.
In general, only stateless Bean can be shared in a multithreaded environment. In Spring, most Bean can be declared as singleton scope, because Spring uses ThreadLocal to deal with some non-thread-safe states in Bean, making them thread-safe.
We analyze the thread safety issues under the Spring singleton at the JVM level, as shown below:
We know that Struts2 is not singleton mode, each Request arrives in the background, Struts2 will set up a separate thread, and the Bean managed by Spring will be initialized when the background application starts.
In JVM, each Thread corresponds to a thread stack, and each method corresponds to a stack frame in the thread stack. The Bean initialization managed by Spring is saved in the Heap of JVM.
When Spring's Bean is referenced in an Action in Strut2, the corresponding Bean referenced in each thread stack points to the same address space in Heap. Suppose that there is a variable temp in this Bean, and there are setTemp and getTemp methods, then the data inconsistency will occur when assigning and taking values to temp in the case of multithreading.
At this point, the study on "how to keep threads in singleton mode safe in Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.