Analysis of action thread safety in Struts

[problem description]

Recently, the company arranged for me to interview java's freshman. The interviewers are generally newcomers who have worked for more than one year (I pretend to be old here, in fact, I have only worked for less than three years). When asked about the thread safety of struts 1 and struts 2 action, most of them are hesitant and can't answer. So I'll sort out my personal understanding here.

[answer to question]

This is due to the working principle of servlet. Let's briefly review the servlet life cycle "initialization - > init - > Service - > Destroy - > uninstall". As we all know here, we are on the web When defining a servlet in XML, we can set a "load on startup" value for them. If the load on startup configuration item of the servlet is greater than 0, it will be instantiated when the context container is started, And Tomcat loads and instantiates an object for each servlet (Note: that is, every servlet configured by our users in web.xml will be instantiated as a servlet object)

a. The following configuration indicates that two servlet objects will be instantiated, even if they correspond to the same servlet class

b. The following configuration indicates that only one servlet will be instantiated

In other words, the Tomcat container adopts the singleton mode for the implementation of servlets. For a servlet class, there will always be only one servlet object. Let's explain why struts 1 is thread unsafe.

1、Struts1

Struts 1 is a direct implementation of Java Web servlet interface, so it inherits Tomcat's implementation of servlet. Each action in struts 1 corresponds to a servlet class. Therefore, the action here is also a single instance after being instantiated by Tomcat. Therefore, struts 1 produces multithreading problems.

For example:

You defined an int i = 0 in action;

Then operate on this I in a method in this action.

Like the following code:

When accessing this servlet, the value of I is the number of times you access it.

So: when we use struts 1, we can't define attributes in action. If you want to use only, you can only define it in the method.

As for why there will be no multithreading problem when attribute definitions are put into methods, I hope readers can check the contents of JMM (JAVA memory model) about how JAVA memory mode allocates memory to methods. I believe you will find an answer.

2、struts2

Above, we learned about the multithreading problem in struts 1. How does struts 2 solve this problem? In fact, the reason is very simple. The reason is that strtus2 will get the user's HTTP request and be responsible for instantiating an action object for each request. However, please note that the action object here and the action object in struts 1 are not a concept at all. The action class in struts 1 is a Servlet class, and the action class here is just an ordinary Java class. This is why the action in struts 1 is thread unsafe, while the action in struts 2 is thread safe.

Let's go back and see how struts 2 handles servlets differently from struts 1. Readers who have read the previous analysis must know that the action of struts 1 does not wrap the servlet. It is a servlet interface directly implemented in the Java Web API. That's why there is a thread safety problem, but the underlying layer of struts 2 helps us encapsulate servlets so that developers don't have to contact servlets directly. The specific measures are:

Strtus2 intercepts servlet requests, instantiates an action object for each request, and destroys the action object after the request. As for how strtus2 does it, I won't elaborate here. You can refer to the relevant introduction of struts 2.

In struts 2, there is no difference between action and ordinary Java classes (that is, you don't need to implement a struts interface as in struts 1, and interested friends can understand it by themselves). So we can use spring to manage the actions of struts 2. At this time, we should pay attention to it, because when we define beans in spring, spring uses the singleton mode by default. So at this time, you need to modify the spring Configuration file - that is, modify the scope to prototype.

summary

The above is all about the analysis of action thread safety in struts. I hope it will be helpful to you. Interested friends can continue to refer to this site: simple implementation code examples of java thread safety counters, detailed explanation of thread safety of various sets of Java, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Two Java programming related books on this site are recommended for free download for reference:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>