Java Servlets threading model

2020-06-08 13:28发布

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?

3条回答
▲ chillily
2楼-- · 2020-06-08 13:41

yes,there is a interface single thread model .it is a marker interface. it makes this object of servlet synchronized i.e at a time only one thread serve the object.but it create the problems like response delay.for solution they create pool of objects but again pool also has limited size.so it is better to use synchronized block

查看更多
贪生不怕死
3楼-- · 2020-06-08 13:55

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.

查看更多
爷的心禁止访问
4楼-- · 2020-06-08 13:59

Historically you had two models, depending on your Servlet. If the Servlet implemented SingleThreadModel, then the requests where queued. Otherwise, and this is the only model now, a Servlet must be able to serve multiple requests at the same time. So there is no queueing in that model, unless the container has some rate-limiting functionnality.

查看更多
登录 后发表回答