Adam Bien's Weblog

Wednesday Apr 25, 2012

Are Servlets Thread Safe and What Is The SingleThreadModel?

The Servlet Specification JSR-315 clearly defines the web container behavior in the service (and doGet, doPost, doPut etc.) methods (2.3.3.1 Multithreading Issues, Page 9):

"A servlet container may send concurrent requests through the service method of the servlet. To handle the requests, the Servlet Developer must make adequate provisions for concurrent processing with multiple threads in the service method.

Although it is not recommended, an alternative for the Developer is to implement the SingleThreadModel interface which requires the container to guarantee that there is only one request thread at a time in the service method. A servlet container may satisfy this requirement by serializing requests on a servlet, or by maintaining a pool of servlet instances. If the servlet is part of a Web application that has been marked as distributable, the container may maintain a pool of servlet instances in each JVM that the application is distributed across.

For servlets not implementing the SingleThreadModel interface, if the service method (or methods such as doGet or doPost which are dispatched to the service method of the HttpServlet abstract class) has been defined with the synchronized keyword, the servlet container cannot use the instance pool approach, but must serialize requests through it. It is strongly recommended that Developers not synchronize the service method (or methods dispatched to it) in these circumstances because of detrimental effects on performance".

Servlets are not thread safe and you have to synchronize your code manually what usually leads to esoteric implementations.


Special Event: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014

A book about rethinking Java EE Patterns

Comments:

@Adam

Good excerpt! It's not directly related to the multi-threading problem, but I think it's worth to emphasize one more time that implementing the SingleThreadModel can also change the default container policy regarding number of Servlet instances.

What I mean is that by default "For a Servlet not hosted in a distributed environment (the default), the servlet container must use **only one** instance per Servlet declaration."

By simply implementing the SingleThreadModel, this behavior CAN be changed:

"However, for a servlet implementing the SingleThreadModel interface, the servlet container **may instantiate multiple instances** to handle a heavy request load and serialize requests to a particular instance."

And also (2.2.1 Note About The Single Thread Model):

"The use of the SingleThreadModel interface guarantees that only one thread at a time will execute in a given servlet instance’s service method. It is important to note that this guarantee only applies to each servlet instance, since **the container may choose to pool such objects.**"

Best regards!

Posted by Piotr Nowicki on April 27, 2012 at 02:26 AM CEST #

But the SingleThreadModel interface has been marked as deprecated as of Java Servlet API 2.4, with no direct replacement...

Posted by shinzey on April 27, 2012 at 02:22 PM CEST #

I'm not really comfortable with this wording:
"Servlets are not thread safe"

Servlets can be accessed simultaneously by multiple threads, and therefore should always be written in a thread safe manner.

In essence, this means that (properly written) a servlet IS thread safe. It is not automatically thread safe (which I assume is what you meant).

Posted by Jesse Sightler on May 01, 2012 at 12:17 AM CEST #

atleast describe properly

Posted by 122.167.104.195 on November 08, 2013 at 10:47 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License