Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the safe multithreading problems of Servlet containers?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the safe multithreading problems of Servlet containers?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, how Servlet containers handle multiple requests at the same time.

Servlet uses multithreading to handle multiple requests for simultaneous access, and the Servelet container maintains a thread pool to serve requests.

A thread pool is actually a set of threads waiting for code to execute called worker threads (WorkerThread), and the Servlet container uses a scheduled thread to manage worker threads (DispatcherThread).

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 that thread, and then executes the service method of the Servlet.

While this thread is executing, the container receives another request, and the scheduler thread selects another worker thread from the pool to serve the new request, regardless of whether the request accesses the same Servlet or another Servlet.

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

Second, the Servlet container processes requests in a single instance and multithreaded way by default, which reduces the overhead of generating Servlet instances and improves the response time to requests. For Tomcat, you can set the number of threads in the thread pool through elements in server.xml.

In terms of implementation:

The responsibility of the scheduler thread class, such as its name, is to schedule the thread and only needs to use its own properties to complete its own responsibility. So this class bears the responsibility, and the responsibility of this class is concentrated in the single object.

While other objects depend on the responsibility of that particular object, we need to get that particular object. Then this class is the implementation of a singleton pattern.

Third, how to develop thread-safe Servlet

1, thread safety of variables: variables here refer to fields and shared data (such as form parameter values).

A, localize the parameter variables. Multithreading does not share local variables. So we should use local variables in servlet as much as possible.

For example:

Stringuser= "; user=request.getParameter (" user ")

B, use the synchronous block Synchronized to prevent code blocks that may be invoked asynchronously. This means that threads need to be queued for processing.

When using the same section, try to narrow the scope of synchronization code as much as possible, and do not use synchronization directly on sevice methods and response methods, which will seriously affect performance.

2. Thread safety of attributes: properties in the ServletContext,HttpSession,ServletRequest object

ServletContext: (threads are not safe)

ServletContext can be multithreaded to read / write properties at the same time, and threads are not safe. The read and write of the attribute should be synchronized or deeply Clone ().

So keeping as little data as possible that will be modified (written) in the Servlet context can be shared across multiple Servlet in other ways, for example, we can use singleton mode to deal with shared data.

HttpSession: (threads are not safe)

The HttpSession object exists during the user session and can only be accessed in threads that process requests belonging to the same Session, so property access to the Session object is theoretically thread-safe.

When a user opens multiple browser windows that belong to the same process, and the access to these windows belongs to the same Session, there will be multiple requests, which may require multiple worker threads to process requests, which may result in multi-thread reading and writing attributes at the same time.

At this point, we need to synchronize the read and write of the attribute: using the synchronization block Synchronized and using the reader / writer to solve the problem.

ServletRequest: (thread is safe)

For each request, executed by a worker thread, a new ServletRequest object is created, so the ServletRequest object can only be accessed in one thread. ServletRequest is thread-safe.

Note: the ServletRequest object is valid within the scope of the service method, so don't try to save a reference to the request object after the end of the service method.

3. Use synchronized collection classes:

Use Vector instead of ArrayList and Hashtable instead of HashMap.

4, do not create your own thread in Servlet to complete a function.

Servlet itself is multithreaded, and creating threads in Servlet will complicate execution and lead to multi-thread safety problems.

5, in multiple servlet to modify external objects (such as files) must be locked to achieve mutually exclusive access.

Fourth, SingleThreadModel interface

The javax.servlet.SingleThreadModel interface is an identification interface, and if a Servlet implements this interface, the Servlet container ensures that only one thread can execute in the service method of a given servlet instance at a time. Queue all other requests.

The server can use multiple instances to process requests, replacing the benefit problems caused by queuing of requests for a single instance. The server creates an instance pool consisting of multiple Servlet instances of a Servlet class, allocates an Servlet instance for response processing for each request, and then puts it back into the instance pool to wait for the next request. This creates the problem of concurrent access.

At this point, local variables (fields) are also secure, but they are not secure for global variables and shared data and need to be synchronized. In the case of so many instances, the SingleThreadModel interface does not solve the problem of concurrent access.

The SingleThreadModel interface has been deprecated in the servlet specification.

This is the end of the content of "what are the safe multithreading problems with Servlet containers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report