For people who have used SPRINGMVC and Struts2, we all know that SPRINGMVC is based on method interception, and Struts2 is class-based interception. Struct2 instantiates an action for each request so there is no thread-safety issue, SPRINGMVC the default singleton request uses a controller, and if the controller defines a static or instance variable, it is shared by multiple threads. So SPRINGMVC controllers do not define static variables and instance variables. If you want to use threadlocal or @scope ("prototype") to turn on and not run in Singleton mode but running in this way is not a singleton, there is an additional overhead. Then some people will ask, Spring management bean default is also a singleton, is it said that in addition to Controller, I behind the service and DAO is also to pay attention to thread safety issues? This question will be specific questions specific answers, here on the issue of thread safety to do a simple introduction maybe you will understand.
We all know that thread safety is actually a data sharing problem. If multiple threads share the same data, there is a possibility of manipulating the data at the same time. It's going to happen this time.
And expected deviations, this is a thread safety issue.
Before we talk about this, we need to know what Java memory is shared with multiple threads, and those that are thread-specific.
The memory in the Java platform includes the following: Stack space, heap space, and non-heap space, stack is thread-exclusive, and heap and non-heap are shared between threads.
Stack: Stack, each thread has its own stack, which is used primarily to store the value of the local variable as the object's reference.
Heap: Heaps, all objects are placed in the heap, the value of the instance variable is stored in the heap space. Instance variables can be shared by multithreading.
Non-heap: The value of a static variable is stored in the Non-heap space and is shared among threads.
Like the following class:
Class a{
private int a=0;
private static int b=1;
public void Method () {
int c=0;
Boolean flag = true;
List<string> list=new linkedlist<string> ();
}}
}
Where A is the instance variable is shared by multiple threads (Class A If it is singleton)
b is a static variable (class variable) that is shared by multiple threads
C is a local variable stored in a thread-exclusive stack
The list holds references to objects on the heap that are also thread-exclusive
It also instantiates a LinkedList object that is to be shared on the heap, but for this object on the example
Each time the method is called, a LinkedList instance is generated, so that the object referenced by the list variable is actually accessible to only one thread.
Here you should have a clear understanding of the use of single or multiple examples. Generally speaking, when the pattern is a singleton, and the class has static variables and instance variables to consider the thread safety issues, this time some of the collection operation is best to use and contract java.util.concurrent inside the amount of the relevant collection class
SPRINGMVC Thread Safety issues