Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is (javax.servlet.)SingleThreadModel deprecated?

Tags:

Why is javax.servlet.SingleThreadModel deprecated?

like image 461
saugata Avatar asked Mar 31 '10 10:03

saugata


People also ask

What will happen if our class implements Singlethreadmodel interface?

Ensures that servlets handle only one request at a time. This interface has no methods. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method.

Are servlets deprecated?

It is not deprecated. They are still releasing new features on the latest Servlet (like async Servlet). And many of the Java web frameworks are building on top the the Servlet technology.


2 Answers

The javadoc says why. SingleThreadModel was designed to be an easy solution to low-load concurrency, but it didn't even manage that:

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.

If it can't achieve what it was designed for, it should not be used.

like image 172
skaffman Avatar answered Oct 16 '22 10:10

skaffman


It's basically a poor way of handling concurrency. Take the state out of your servlet instead, so that the same servlet can be used by multiple threads concurrently. Keeping state in a "pool" of servlet instances, each of which can have state left over from the previous request etc is pretty horrible.

like image 30
Jon Skeet Avatar answered Oct 16 '22 11:10

Jon Skeet