Servlet Thread Model

maneesh subherwal

Ranch Hand

Posts: 42

posted 13 years ago

All, I have a question that folks here at JavaRanch may be familiar. Could someone please explain servlet instantiations (probably in Tomcat) using a Single Threaded Model versus Multi-Threaded Model? My confusion basically refers to this: If I have servletA requested at the same time by two different requests, what happens? Does a new instance of the servlet get initiated for each request? If not, how is it synchronized? Thank you. cyberCipher

Hi, To the best of my knowledge if an object is already instantiated the container will use that one to service the request. If the servlet/jsp is flagged as being thread safe, the container creates a new object if there already is an object of that type servicing another request. The container manages synchronisation once the jsp/servlet are flagged as thread safe. I think that's how it works anyway, maybe someone else can put more light on this. Kem Bro [ September 04, 2003: Message edited by: Kem Bro ]

Faisal Khan

Ranch Hand

Posts: 285

posted 13 years ago

Single Thread Model means that for each request a new instance of the servlet has to be instantiated, therefore making it very inefficient and slow. Especially so if the requests are served sequentially (depends on the servlet container). Normally there is only one instance of the Servlet and a new thread is issued for each new request. Thus the request scope variables are thread safe. HTH