Thread security of struts 1 and Struts 2

Source: Internet
Author: User

In general, the action of struts1 is a singleton mode, so developers must ensure that it is thread-safe or synchronous, because in struts 1, each action has only one instance to process all requests.

 

However, the thread security issue is not taken into account when struts 1 is used for development. This is because we basically use local variables in actions, while "local variables are thread-safe. Each time a method is executed, a local variable is created in an independent space, which is not a shared resource. Local variables include the parameter variables of the method ). In struts 1, all variables are defined in the method we want to execute in Action (execute method in action or dispatchaction specifies the method to be executed ), it is used to encapsulate the actionform of the client request parameters. It is also passed as a parameter and also a local variable. Therefore, there is no thread security problem.

 

The action object of struts 2 generates an instance for each request. Therefore, although many global variables are defined in the action, there is no thread security problem.

When processing each user request, the struts 2 framework creates a separate thread for processing. Value stack valuestack also exists along with local threads. When the thread exists, the value stack can be accessed at will, which ensures the security of the value stack.

 

In struts 2, actioncontext is a local thread, which means that the actioncontext content in each thread is unique. Therefore, developers do not have to worry about the thread security of action.

The strut2 authoritative guide contains the following section:
Comparison of thread modes: struts1 action is a singleton mode and must be thread-safe, because only one instance of action can process all requests. The Singleton policy limits what struts1 actions can do, and you need to be especially careful during development. Action resources must be thread-safe or synchronous. Struts 2 action objects generate an instance for each request, so there is no thread security problem.

All generate an instance, so global variables should be shared.

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.