Servlet Programming topic 5, Servlets thread safety issues

Source: Internet
Author: User

1. Thread Safety issues:

A, a thread safety issue may occur when the following conditions are met

I, there are multi-threaded concurrent access.

II, there is a modifiable shared data.

-When multiple threads modify the same shared data at the same time, the modified data overwrites the first modified data, and the user who modifies the data first reads the data that is not modified by itself, which is a thread-safety issue.


B. Data analysis of possible thread safety issues in the JVM:

I, Stack memory data analysis: Stack memory is a number of cases, that is, the JVM will create a stack for each thread, so the data is not shared. In addition, the local variables in the method are stored in the stack frame of the stack, the method executes, the stack frame is loaded, and the local variables disappear. Local variables are local, not shared. So the data in the stack memory is not a thread-safe issue.

II. Heap Memory Data analysis: There is only one heap of memory in a JVM, and heap memory is shared. The object that is created is the object that is stored in the heap memory and stored in the heap memory, which is actually a collection of the values of the object member variables. That is, the member variable is stored in the heap memory. The data in the heap memory is shared in multi-threaded. So the data in the heap memory is a thread-safe issue.

III. Method area Data Analysis: Only one method area exists in a JVM. Static variables and constants are stored in the method area, and the method area is shared in multiple threads. Constants are quantities that cannot be modified, so there is no thread safety problem with constants. Static variables are shared in multiple threads, so there is a thread-safety problem with static variables.


C. Solutions for thread safety issues:

-To address thread safety issues with your data, you can consider the following ideas:

I, for a general class, do not define a singleton. Unless there is a special requirement in the project, or the class object data is a heavyweight object. (The so-called heavyweight object refers to the need to consume large system resources to create the class object.)

II, regardless of whether it is a singleton class, try not to use static variables.

III, if need to be defined as a singleton class. The member variable is not used as much as possible in a singleton class. (If you use member variables, you do not provide a way to modify the variables, and the data for that class of objects is not a thread-safe issue.)

VI, if the Singleton class must use member variables, the operation of the member variables, you can add the serialization lock synchronized, to achieve thread synchronization. However, it is best not to use the thread synchronization mechanism. Because once the operation enters the serialized state, the execution efficiency of the program is greatly reduced.


2, the servlet thread security problem: Servlet is running in a single-threaded environment, its operation may be a thread-safe problem.

To avoid thread-safety problems, the use of servlets is generally not declared as member variables. If the project requires that member variables (modifiable) must be declared, it can only be avoided through the thread synchronization mechanism (synchronized).


3, the rational use of thread safety issues:

-The member variables in the servlet are modifiable and accessible to each thread, so you can use this to implement counter functions to count the number of times the current servlet has been accessed.



This article is from the "12392717" blog, please be sure to keep this source http://12402717.blog.51cto.com/12392717/1974394

Servlet Programming topic 5, Servlets thread safety issues

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.