Deep understanding of servlet thread safety issues __servlet

Source: Internet
Author: User
turn from: http://blog.csdn.net/lcore/article/details/8974590     Preface

In a previous study on the Serlvet framework and the servlet lifecycle, we've learned that in the case of multithreading

The servlet is not thread safe. The servlet system is based on Java multithreading, and its lifecycle is made up of Tomcat

To maintain. When the client requests the servlet for the first time, Tomcat instantiates the servlet according to the Web.xml configuration file.

When another client accesses the servlet, the servlet is not instantiated, that is, multiple threads are using this instance. servlet thread Pool

Serlvet uses multithreading to handle multiple requests for simultaneous access, and the Tomcat container maintains a line pool service request.

A thread pool is actually a set of threads waiting to execute code called a workgroup thread (Worker thread), which uses a

Dispatch threads to manage workgroup threads (Dispatcher Thead).


When the container receives a servlet request, the dispatcher thread picks a workgroup thread from the thread pool and passes the request

To the thread, which is then executed by the thread to perform the service method of the servlet.

When this thread is executing, the container receives another request, and the dispatcher thread picks another one from the thread pool

The workgroup thread service is a new request, and the container does not care whether the request accesses the same servlet or another

Servlet. When a container receives multiple requests for the same servlet, the Servlet's service method will be in a multithreaded

Execution of concurrency in. servlet Thread Security issues

Multithreading and single-threaded servlet specific differences: multithreading under each thread to local variables will have their own copy, which

Changes to local variables will only affect their own copy and will not affect other threads, thread-safe. But for

instance variables, because the servlet exists as a singleton pattern in Tomcat, all threads share instance variables. Multiple threads

Access to shared resources creates thread-insecure issues.

This problem does not exist for a single thread (except for static variables)

Here we write an example to simulate:[Java]  View plain  copy package com.kiritor;      import java.io.ioexception;    import java.io.printwriter;      import javax.servlet.servletexception ;   import javax.servlet.http.httpservlet;   import  javax.servlet.http.httpservletrequest;   import javax.servlet.http.httpservletresponse;      /**   * Servlet implementation class ThreadServlet   * /   public class threadservlet extends httpservlet {        private static final long serialVersionUID = 1L;        private String message;          /**       *  @see  httpservlet#httpservlet ()        */      &NBSp; public threadservlet ()  {           super () ;           // todo auto-generated constructor  stub       }          /**        *  @see  httpservlet#doget (httpservletrequest request,  httpservletresponse       *      response)        */       protected void doget ( httpservletrequest request,                httpservletresponse response)  throws ServletException, IOException {            this.dopost (request, response);        }         /**       *  @see  httpservlet#dopost (httpservletrequest request, httpservletresponse       *       response)        */        Protected void dopost (httpservletrequest request,                httpservletresponse response)  throws

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.