Servlet Thread Safety issues

Source: Internet
Author: User

In the servlet encyclopedia we can see a description: "In a Web application, a Servlet can be accessed at the same time by multiple users at a time." The Web container then creates a thread for each user to execute the Servlet. If the Servlet does not address the issue of shared resources, you do not have to worry about multithreading issues. However, if the servlet needs to share resources, it is necessary to ensure that the servlet is thread-safe "by this we can see that the servlet is a singleton.
So how do you ensure that the servlet is thread-safe:
1, try to use local variables, any thread will not modify the other thread's local variables. If you want to share data between different requests, you should use a session to share this kind of data.
2. Use only servlet member variables to store data that will not change. Some data does not change in the Servlet life cycle, usually at initial time, such as database connection names.
3. Synchronize the member variables that may be requested to be modified. Sometimes data member variables or environment properties may be requested to be modified. When accessing this data, they should be synchronized to prevent multiple threads from modifying the data at the same time.
4. If the Servlet accesses external resources, the resources need to be accessed synchronously. For example, suppose a Servlet reads and writes data from a file. When a thread reads and writes a file, other threads may be reading and writing to the file. File access itself is not thread-safe, so you must write code that accesses these resources synchronously.
Servlets are single-instance multithreading
The Init method in the servlet is called only when it is started (for example, when the Web container starts, to see the settings of the loadonstartup), i.e. it is initialized only once, which is a single instance.
The servlet calls the service method when processing the request, which can handle requests from multiple clients.
When specifically visited:
The JSP "translates into Servlet" in the Web container is executed by the container, which itself is the multi-threading provided, A,b,c 3 accesses, establishes 3 separate thread groups, and runs a servlet. Execute in turn.
This solves the confusion of multiple users in the same instance.
Some points to note:
1:servlet first is not a ready-made thread.
The 2:servlet architecture is built on the Java multithreading mechanism, whose life cycle is the responsibility of the Web container.
The servlet container automatically uses techniques such as the thread pool to support system operation
3: Set jsp:<%@ page isthreadsafe= "false"%> to implement single thread.
When you need to ensure data consistency, you have to consider a single thread when you have to handle threading security issues yourself.

Servlet 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.