What is SingleThreadModel in Servlets?

What is SingleThreadModel in Servlets?

1. What is SingleThreadModel?

It is an interface from javax.servlet package used with Servlets. It is marker interface having no methods. Few servlets may require this to implement.

2. Are Servlets multithreaded?

Yes, Servlets are multithreaded being Java supports multithreading.

3. Explain how Servlets are multithreaded?

First time when a request comes to a Web server for a Servlet, the Web container loads the Servlet, creates a Servlet object, executes the callback service() method. Okay, it is fine everyone knows.

If one more request comes (at the same time or concurrently) from another visitor for the same servlet being executed, what the Web container does? Does it load another Servlet with a separate process initiated (like CGI/PERL) or the same process being executed serves the request? The same servlet process being executed serves the other visitor request. That is, one instance of Servlet with one service() method handles both the requests. Here comes, Servlet’s multithreading concept.

For the other visitor, a thread is created in the same process (in the same service() method). To be more clear, if 100 requests come concurrently for the same servlet, 100 threads are created within the same process (or one process). We know each thread maintains its own set of instance variables.

4. Then, where the SingleThreadModel concept comes?

With multiple requests for the same servlet, in the Container multiple threads will be active within the process.
If the Programmer would like to have only one thread active at a time (other threads, if exist, must be passivated or made inactive) then he implements the SingleThreadModel interface and it being marker interface no methods need to be overridden.

public class MilitarySecrets extends HttpServlet implements SingleThreadModel { }

The above Servlet MilitarySecrets implements SingleThreadModel interface. The affect is at no time two threads (of two visitors) striking the same Servlet will be active. Other way to say is, single thread is active – only one visitor at a time is honoured.

5. Okay, it is fine, what is the requirement or when to implement SingleThreadModel?

We know each thread is a separate process within a process having its own execution stack and counter etc. One thread’s data will not go (or mix up) to the other thread. This is taken care entirely by the JVM. There may be a situation (it is only a chance) where threads may not work properly (as threads are managed by the underlying OS through Thread Scheduler) and if the same thing happens in a very critical area like military secrets sharing. Best solution is to make one thread active at a time. Then the go is for SingleThreadModel.

6. Finally define SingleThreadModel interface?

  1. When SingleThreadModel is implemented, the Servlet container gives guarantee that only one request is honoured or handled at a time by one servlet object.
  2. Or to say, no two threads execute concurrently in the same service() method.
  3. The interface assures thread-safe servlets.
  4. SingleThreadModel means you are making a particular Servlet not multithreaded.
  5. Multiple concurrent requests will be executed with one Servlet instance but in different threads of which only one is permitted.
  6. Use SingleThreadModel when the servlet preserves the state of the client (specific to client) in instance variables. Ofcourse, preserving the client-specific state in static variables or instance variables is a bad practice and should be avoided to the maximum extent.

7. How the Web Server implements SingleThreadModel?

We cannot say, it is the internal mechanism developed by the Container Architect. There may be two ways – maintaining a pool of servlet instances (giving a free servlet instance to a client) or synchronizing the access to permit a single Servlet instance.

Note: By default, Servlet is not single thread model, it is multithread model. Implementation of SingleThreadModel makes a Servlet, single thread mode.

8. Why the SingleThreadModel is deprecated?
9. What are the drawbacks of SingleThreadModel?

  1. It is deprecated from Servlet version 2.4 as it is not really working with the task assigned.
  2. Now-a-days, Programmers are not using this interface and achieved the same job by manually synchronizing the code.
  3. Because Container maintains a separate memory for each thread, it is more memory intensive (requires more memory management).
  4. Depending on the Web server capacity and speed, the server may get bogged (not responsive) when more clients request for single thread model servlets.
  5. The Servlet may be thread-safe, but the data preserved in session and application (ServletContext) objects is not thread-safe. They require manual synchronization.
  6. By this you can understand, the practical approach is to use synchronization and not single thread model.

View All Servlets

Leave a Reply

Your email address will not be published. Required fields are marked *