Thread safety issues with servlet/struts1/struts2/spring MVC

Source: Internet
Author: User

Thread-Safe Concepts:

If your code is in a process where multiple threads are running at the same time, these threads may run the code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe.

Thread safety issues are caused by global variables and static variables.

In general, this global variable is thread-safe if there are only read operations on the global variable and static variables in each thread, and if multiple threads are concurrently performing write operations, thread synchronization is generally required, otherwise it may affect thread safety.


In Java, thread safety generally manifests itself in two ways:
1, multiple thread access to the same Java instance (read and modify) does not interfere with each other, it is mainly embodied in the keyword synchronized. such as ArrayList and Vector,hashmap and Hashtable (which have synchronized keywords before each method). If you are interator a list object and the other threads remove an element, the problem arises.
2. Each thread has its own field and is not shared across multiple threads. It is mainly embodied in the Java.lang.ThreadLocal class, and there is no Java keyword support, such as static, transient.


Servlet: Non-thread safe

The servlet architecture is built on the Java multithreading mechanism, whose life cycle is the responsibility of the Web container. When a client requests a servlet for the first time, the servlet container instantiates the Servlet class based on the Web. XML configuration file. When a new client requests the servlet, the servlet class is typically no longer instantiated, that is, multiple threads are using the instance. Therefore, for the member variables of the servlet, there is a thread safety issue

How to ensure servlet thread safety?

1. Implement the Singlethreadmodel interface

This interface specifies how the system handles calls to the same servlet. If a servlet is specified by this interface, then the service method in this servlet will not have two threads being executed concurrently, and there is no thread-safe issue, of course.

2. Synchronizing operations on shared data

Using the Synchronized keyword ensures that only one thread can access the protected section at a time, and the servlet in this thesis can ensure thread security by synchronizing block operations.

3. Avoid using instance variables

The thread safety problem in this example is caused by an instance variable, which is thread-safe as long as the instance variable is not used in any of the methods inside the servlet.


STRUTS1: Non-thread safe

Struts1 is a singleton pattern, that is, when the Web container (for example: Tomcat) is started, an action object is instantiated, and all requests are used for that object. So when there are 2 requests in parallel, then actually they call the same class, this time when you define attributes within the action, there is a thread-safe problem


STRUTS2: Thread Safety

The Struts 2 Action object generates an instance for each request, so there is no thread safety issue. So we can define the attributes in the Struts2 action. But Struts2 because there is no difference between the action and the normal Java class (that is, you do not have to implement a struts interface like the one in Struts1, interested friends can get to know themselves), so we can use spring to manage the Struts2 action. , this time we have to pay attention, because when we are in spring to define the bean, spring by default is a singleton mode. So at this point, you need to modify the spring configuration file---that is, modify scope to prototype.
There is no threading problem in struts1 because all the code is written in the Execute method, all variables are defined inside, so there is no thread safety problem.

And now the STRUTS2 is not the same. Struts2 's action, like a Pojo, defines a lot of class variables. This is a thread-safe issue. At this point, the scope=prototype is used to specify a prototype pattern, not a singleton, which solves the thread-safety problem. Each thread is a new instance.


Spring MVC: Non-thread safe

Spring's controller is singleton by default, which means that every request comes in and the system is processed with the original instance, which results in two outcomes: one is that we don't have to create a controller every time,  The second is to reduce the time of object creation and garbage collection; Because there is only one controller's instance, when multiple threads call it, the instance variable inside it is not thread-safe.     How do I keep spring MVC thread safe?     1.Controller uses the threadlocal member variable. 2. Declare scope= "prototype" in the Controller of spring MVC and create a new controller each time.

Thread safety issues with servlet/struts1/struts2/spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.