Tuesday, September 21, 2010

Why SingleThread model is deprecated in Servlets?

SingleThreadModel ensures that servlets handle only one request at a time. This interface has no methods. In Servlet 2.4 SingleThreadModel is deprecated for a good reason mostly because it does not really provide thread safety.

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. Also it is really very expensive to creating one instance for each request is time consuming and difficult.


Alternative to SingleThreadModel:
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.

No comments:

Post a Comment