HashUser123
HashUser123

Reputation: 57

Why we need to implement Single Thread model in case of Servlet?

Why we need to implement Single Thread model in case of Servlet?

Upvotes: 1

Views: 1274

Answers (3)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78589

If you read the Java Servlet Specification you will find answers to your questions. There are multiple sections there explaining threading issues. For instance:

Section 2.3.3.1. Multithreading Issues

A servlet container may send concurrent requests through the service method of the servlet. To handle the requests, the Servlet Developer must make adequate provisions for concurrent processing with multiple threads in the service method.

Although it is not recommended, an alternative for the Developer is to implement the SingleThreadModel interface which requires the container to guarantee that there is only one request thread at a time in the service method. A servlet container may satisfy this requirement by serializing requests on a servlet, or by maintaining a pool of servlet instances. If the servlet is part of a Web application that has been marked as distributable, the container may maintain a pool of servlet instances in each JVM that the application is distributed across.

For servlets not implementing the SingleThreadModel interface, if the service method (or methods such as doGet or doPost which are dispatched to the service method of the HttpServlet abstract class) has been defined with the synchronized keyword, the servlet container cannot use the instance pool approach, but must serialize requests through it. It is strongly recommended that Developers not synchronize the service method (or methods dispatched to it) in these circumstances because of detrimental effects on performance.

Also this section.

Section 2.3.3.4: Thread Safety

Other than the startAsync and complete methods, implementations of the request and response objects are not guaranteed to be thread safe. This means that they should either only be used within the scope of the request handling thread or the application must ensure that access to the request and response objects are thread safe.

If a thread created by the application uses the container-managed objects, such as the request or response object, those objects must be accessed only within the object’s life cycle as defined in sections 3.10 and 5.6. Be aware that other than the startAsync, and complete methods, the request and response objects are not thread safe. If those objects were accessed in the multiple threads, the access should be synchronized or be done through a wrapper to add the thread safety, for instance, synchronizing the call of the methods to access the request attribute, or using a local output stream for the response object within a thread.

There are more comments like this in the rest of the specification that seem to suggest that you are probably better off if you ensure your code does not need any kind of synchronization.

Upvotes: 1

Indra Yadav
Indra Yadav

Reputation: 600

The servlet programmer should implement SingleThreadModel interface to ensure that servlet can handle only one request at a time.

Upvotes: 0

Niels Bech Nielsen
Niels Bech Nielsen

Reputation: 4859

I think it is deprecated now, however, originally a servlet handles multiple simultaneous requests, each in its own thread. By annotating the servlet you could funnel requests into serial processing, because the servlet only allowed one thread to process all its request.

But as I said, it is deprecated now, and you shouldn't use it. If you have special synchronization needs, you should handle it manually..

Upvotes: 0

Related Questions