Tutorial :can anybody elaborate single threded model in webapp?



Question:

When do we need single threded model in webapp while designing web application in java.


Solution:1

The single-threaded model should almost always be avoided. (I'm assuming you're talking about the SingleThreadModel interface.) Basically it was introduced in an attempt to save people from having to think about concurrency, but it was a bad idea. Concurrency is inherent in web applications - introducing a bottleneck like the single threaded model is the wrong solution. The right solution is to educate developers about concurrency better, and introduce better building blocks for handling it.

The interface is deprecated as of the Java Servlet API 2.4, with this note:

Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.


Solution:2

When your Servlet has state (which is a bad idea) and you want to prevent multiple requests in stepping on their own toes (or data).

I would recommend you avoid it because at some point you will mess something up. Also, performance drops like a brick.


Solution:3

The single thread model for servlets is used to signal that the servlet cannot handle multiple concurrent threads from client connections. Setting a servlet to the single threading model results in the servlet container (application server) to create a servlet instance per client.

It is best practice not to use the single thread model for servlets. Data kept per client connection is typically stored in the client Session object.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »