How SpringMVC default singleton guarantee performance

Question: SpringMVC created by default Bean is a single example, in the case of high concurrency is how to ensure performance?

First, everyone's thinking, certainly influential, you think, singleton name implies: a queued too ... high-traffic, you can imagine the pressure on the server and the user experience ... not how good wait too long ~

This understanding is essentially wrong, Java API is called, there are a ThreadLocal, use it to switch the parameters between different threads Spring singleton. With ThreadLocal thread safety is to ensure that, in fact, the key is ThreadLoacal Thread instance of the current thread. Parameter values ​​Singleton, Spring each thread to thread there may be security issues into the ThreadLocal. Although this is an example of the operation, but the data under different threads are isolated from each other because of the creation and destruction of Bean greatly reducing the running time, there is less consumption of memory resources in most scenarios this way, and the more obvious advantages of higher concurrency.

Overall it is that simple interest model as significant savings in creating and destroying instances, help to improve performance, and to ensure ThreadLocal thread safety. Singleton is spring recommended configuration, it can greatly save resources under high concurrency, improve service under pressure

Guess you like

Origin blog.csdn.net/iroguel/article/details/78357327