are SPRINGMVC and struts thread-safe? Why?

Source: Internet
Author: User

The thread is not secure. (In fact, I think the answer is: There are thread-safety issues that are better)

The reasons are as follows:

1th, first understand why threads are unsafe

1 Struts1 action is a single case, so there are thread-safety issues (STRUTS2 is multiple, no thread-safety issues exist)
2 Spring Default injection is also a single case, so there are thread-safety issues

First of all, understand why wired is not safe, for example, if a class has a property of name, thread 1 modifies the name of the attribute, and thread 2 modifies the name when it is stored in the database, so that thread 1 is stored in a thread 2 modified data.

The way to avoid it is simple.
Struts1 if it is managed by spring, you can set scope to Protype so that the action is not a single case, but one for each thread;
Thread safety can be avoided by prohibiting the use of variable member variables, and if they are local variables, there is no problem even with a single case
There are no variable member variables, saying that they are a single case, but not a thread problem

2nd: Deep Understanding

1. First understand why the thread is unsafe and then consider whether a class is thread safe
2. Second, the thread is safe, in fact, and spring, struts no inevitable relationship


You said struts ' action is not thread-safe, because you define attributes in the action
For example: Java code

public class Sampleaction extends Action {private String user = null;.} 
 


If multiple users come up to access this sampleaction, and each user has to modify the user attribute or to do something to the user, this is a thread-insecure situation, or a scenario.
However, if you do not define this user property and do not define any other attributes, there is no thread-unsafe problem with this sampleaction. There is also the case that if you define this attribute as final, only one assignment is made at initialization time, all subsequent operations will simply read and not modify the value of this property, so there is no thread security problem.
For example, if you have a class user that is new when you use this class, then this class must be thread safe. And not necessarily. Java Code

public class User {private static String name = NULL; public User (string name) {User.Name = name;}} 
 

For this user class, even if you are new for each time, it is not thread safe.


Summary: servlet, Struts1 is a single case, since it is a single case, if the use of instance variables (static is the shared data variables) when the problem will be wired security; struts2 must be thread-safe, because each time a request is processed, Struts will instantiate an object, so there is no thread-safe problem; SPRINGMVC controller default is a singleton mode (when spring injection can be changed to a configuration file scope= "prototype" not a single example), So there will be multiple threads concurrency problem;


Workaround: 1 Synchronous shared data 2) do not use member instance variables; 3 Use read-only data



Article from: http://m.blog.csdn.net/blog/panhaixin1988/14209651

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.