Thread security issues related to Action

xiaoxiao2021-03-06  53

Take the JSP Description: JSP is executed by a multi-threaded manner by default. This is a different place in JSP and ASP, PHP, Perl and other scripts. It is also one of its advantages, but if you don't pay attention to synchronization problems in multi-thread, will Make the written JSP program has an incorrect error.

Cause:

(1) Example variable instance variable is allocated in the heap, and is shared by all threads belonging to the instance, so it is not a thread safe. (2) Out, request, the 7-class variable JSP provided by the JSP system, the REQUEST, Response, Session, Config, Page, PageConxt are thread secure, and Application is used throughout the system, so it is not a thread safe. (3) Local variables are allocated in the stack because each thread has its own stack Space, so it is a thread safe. (4) Static class static class does not need to be instantiated, it can be used directly, nor thread is safe. (5) External resources: There may be multiple threads or processes simultaneously in the program The same resource (such as a plurality of threads or processes are written while writing). At this point, you should pay attention to synchronization issues.

Solution:

(1) Use a single-threaded manner to add <% @ page? IsthreadsaDsafe = "false" in the JSP file, so that it is executed in a single-threaded manner, there is still only one instance, all client requests are serialized carried out. This reduces the performance of the system. (2) Thread synchronization of the function plus synchronized, JSP is still executed in a multi-threaded manner, but it will also reduce the performance of the system. (3) use local variables instead of instance variables

Multi-threaded problems typically only appear when they are accessed in a large concurrency, and it is difficult to repeat, so they should be noted when programming.

Just like a servlet, each Action object is only instantiated once, and all client requests share this Action instance, so it is necessary to ensure its multi-threading when expanding the action. That is to say that your ActioIn object must be a thread safe, so that the client's state cannot be stored in an instance variable of the Action, only in the local variable (such as Excute ()).

转载请注明原文地址:https://www.9cbs.com/read-116649.html

New Post(0)