Servlet and thread safety

Let's start with the conclusion: servlets are not thread safe.

Servlet running process

The servlet program is called by the web server. After the web server receives the servlet access request from the client:

How does the servlet container handle multiple requests simultaneously

Servlet uses multithreading to handle multiple requests and access at the same time. The servlet container maintains a thread pool to service requests. A thread pool is actually a group of threads waiting to execute code, called worker threads. The servlet container uses a scheduling thread to manage worker threads.

When the container receives a request to access the servlet, the scheduler thread selects a worker thread from the thread pool, passes the request to the thread, and then the thread executes the service method of the servlet instance. When this thread is executing, the container receives another request. The scheduler thread will select another worker thread from the pool to serve the new request. The container does not matter whether the request accesses the same servlet instance or another servlet instance.

When the container receives multiple requests for the same servlet instance at the same time, the service method of the servlet instance will be executed concurrently in multiple threads.

Why aren't servlets thread safe

Servlets run in singleton multithreading mode, that is, when multiple requests access the methods of the same servlet, only one servlet object will be created.

If nonlocal variables are used in servlet methods, thread safety problems will arise.

The local variables in the method are all in the local variable table of the stack frame. The stack frame is a part of the stack, and the stack is thread private. Therefore, the local variables in the servlet will not cause thread safety problems. The local variable exists in the stack. If it is a reference type, it points to the object in the heap.

Thread safety of servlet related members

ServletContext: non thread safe. ServletContext can read / write properties simultaneously by multiple threads, so it is non thread safe. To synchronize the reading and writing of attributes or perform deep clone(). Therefore, save as little data that will be modified (written) in the servlet context as possible, and share it in multiple servlets in other ways. For example, we can use the singleton mode to process the shared data.

Httpsession: non thread safe. The httpsession object exists during the user session and can only be accessed in the thread processing requests belonging to the same session. Therefore, the attribute access of the session object is thread safe in theory. However, when a user opens multiple browser windows belonging to the same process, and the access to these windows belongs to the same session, multiple requests will occur, requiring multiple worker threads to process the requests, which may cause multiple threads to read and write attributes at the same time. At this time, we need to synchronize the reading and writing of attributes: using synchronization block synchronized and using reader / writer.

ServletRequest: thread safe. For each request executed by a worker thread, a new ServletRequest object will be created, so the ServletRequest object can only be accessed in one thread, so ServletRequest is thread safe. Note: the ServletRequest object is valid within the scope of the service method. Do not try to save the reference of the request object after the service method ends.

How to use servlets safely

Try to use local variables. Multithreading does not share local variables, so we should use local variables in servlets as much as possible.

Lock the code blocks that need to be called asynchronously, but this means that threads need to queue up for processing, because when using the same block, the scope of synchronous code should be reduced as much as possible, and synchronization should not be used directly on the sevice method and response method, which will seriously affect the performance.

Use thread safe classes: for example, use vector instead of ArrayList and hashtable instead of HashMap.

Do not create your own thread in the servlet to complete a function. The servlet itself is multithreaded. Creating a thread in the servlet will complicate the execution and cause multithreading safety problems.

When modifying external objects (such as files) in multiple servlets, you must lock them to achieve mutually exclusive access.

#########

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>