SingleThreadModel In Servlet

The SingleThreadModel interface was designed to guarantee that only one thread is executed at a time in a given servlet instance’s service method. It is a marker interface and has no methods.

This interface is currently deprecated, excerpts from the Java Doc:

Deprecated. As of Java Servlet API 2.4, with no direct replacement. 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. The SingleThreadModel Interface is deprecated in this version of the specification.

Let us see a simple example using this interface, though it doesn’t guarantee what its specification says.

Example of SingleThreadModel



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 SingleThreadModelExample extends HttpServlet implements
		SingleThreadModel {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();


		try {
			out.print(" to Javabeat");
		} catch (Exception e) {





   	<servlet-class> </servlet-class>



Example Explanation

  • Class SingleThreadModelExample implements the SingleThreadmodel interface. The class SingleThreadModelExample which is a servlet prevents handling Single requests at a single time.
  • Here sleep() is a static method in Thread class, which is used to suspend execution of a thread for some specified milliseconds. Thread. Sleep( 2000 ) will stop the execution of the thread for 2000 millisecond.
  • When another user access the same servlet, the new instance is created instead of using the same instance for multiple threads.


Previous Tutorial : MVC Architecture  || Next Tutorial : HTTP Codes



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Siddu

    Say suppose 10 users are accessing same servlets, will it creates 10 instances or only limited instances creates.

    • Krishna Srinivasan

      It will create 10 instances. It is not advised to use SingleThreadModel in many scenario because it impacts the performance.