Single Thread Model Interface

Single Thread Model Interface in Servlet

In this article, I am going to discuss Single Thread Model Interface in Servlet with Example. Please read our previous article where we discussed Servlet Input Output Stream Classes with Examples. At the end of this article, you will understand the following pointers in detail.

  1. As Servlets are Multi-threaded. How SingleThread Model comes?
  2. Requirement of SingleThreadModel
  3. How to implement SingleThreadModel?
  4. Example of SingleThreadModel Interface
  5. Drawbacks of SingleThreadModel in Servlet
As Servlets are Multi-threaded. How SingleThread Model comes?

As Servlets are multithreaded being Java supports multithreading. Because when a request comes to a WebServer for a Servlet, the Web Container loads a Servlet, creates a Servlet object, and executes the callback service() method. If one more request comes from another visitor for the same servlet being executed, the servlet process being executed serves the other visitor request. Here is the one instance where one service() method handles both the requests. Here, servlet’s multithreading concept comes.

With multiple requests for the same servlet, multiple threads will be active within the process. If you want to have only one thread active at a time then you can implement SingleThreadModel Interface where no methods will be overridden.

Requirement of SingleThreadModel

It ensures that the servlet can handle only one request at a time. It has no methods. If a servlet implements this interface it is guaranteed that no two threads will execute concurrently in the servlet’s service method. The servlet container guaranteed it by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet. SingleThreadModel interface does not prevent synchronization problems that result from servlets accessing shared resources such as static class variables or classes outside the scope of the servlet.

Single Thread Model Interface in Servlet with Example

How to implement SingleThreadModel?

There are two ways of implementing a SingleThreadModel:

  1. Maintaining a pool of servlet instances
  2. Synchronizing the access to permit a single Servlet instance.

Note: Servlet is not a single thread model by default. We can make a servlet single thread model by implementing the SingleThreadModel interface.

Example of SingleThreadModel Interface

In this example, the index.html file creates a link that invokes the servlet of URL-pattern “servlet1”. SingleThreadExample.java class implements the SingleThreadModel interface. Class SingleThreadExample is a servlet which prevents handling Single requests at a single time. Here sleep() is a static method in Thread class used to suspend execution of a thread for some milliseconds. Thread.sleep(10000) will stop the execution of the thread for 10000 milliseconds. When another user will try to access the same servlet, the new instance is created instead of using the same instance for multiple threads.

index.html
<html>
<head>
     <title>HttpSession Event Listeners</title>
</head>
<body>
     <a href="servlet1">click here to invoke single threaded servlet</a>
</body>
</html>
SingleThreadExample.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SingleThreadExample extends HttpServlet implements SingleThreadModel {
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  out.print("welcome");
  try {
   Thread.sleep(10000);
  } catch (Exception e) {
   e.printStackTrace();
  }
  out.print(" to servlet");
  out.close();
 }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>SingleThreadExample</servlet-name>
  <servlet-class>SingleThreadExample</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>SingleThreadExample</servlet-name>
  <url-pattern>/servlet1</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
</web-app>
Output

Run your project and you will get the following link.

How to implement SingleThreadModel?

Click on the link to get the following output.

SingleThreadModel interface in Servlet

Drawbacks of SingleThreadModel in Servlet:

The SinglethreadModel does not solve all thread-safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.

In the next article, I am going to discuss Server Side Include (SSI) in Servlet. Here, in this article, I try to the SingleThreadModel interface in Servlet with Examples. I hope you enjoy this SingleThreadModel interface in the Servlet article.

Leave a Reply

Your email address will not be published.