Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Servlets threading model

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?

like image 698
Dunith Dhanushka Avatar asked Jan 19 '10 17:01

Dunith Dhanushka


People also ask

Are servlets multithreaded?

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.

What is thread in servlet in Java?

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.

Is servlet multithreaded or single threaded?

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.

What is single thread model in servlet?

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.


1 Answers

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.

like image 73
danben Avatar answered Sep 28 '22 01:09

danben