I was wondering if anybody could explains me the threading model of Java Servlets? As I understood about that, there is only one instance of a servlet can be existed in the servlet container and if muliple threads happens to be waiting for that servlet, there requests are serialized in some manner. I don't know how that serialization process happens...
Can anybody explain it?
A Java servlet container / web server is typically multithreaded. That means, that multiple requests to the same servlet may be executed at the same time.
When a request comes in, a Thread is acquired from the pool to handle it. When the request is finished and a response is sent back, the Thread is returned to the pool and is available for future requests. It has nothing to do with the http session and doesn't matter which Servlet is involved.
Servlets are multithreaded - this is the base for their efficiency. One can use "implements SingleThreadModel" to make a servlet single-threaded, so for every request a new object will be created.
public abstract interface SingleThreadModel. 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.
If requests were handled serially by servlets, then web applications would be very slow. It's actually the case that servlets need to be thread-safe, because a single instance of a servlet can be responsible for handling multiple requests simultaneously.
Usually a web application container will maintain a thread pool for handling requests, with incoming requests being assigned to threads on an on-demand basis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With