What is the difference between Single Thread Model and Multi Thread Model?In which situation we use the Single Threaded Model?

Showing Answers 1 - 3 of 3 Answers

akantilal

  • Jun 12th, 2007
 

The single thread model means that your servlet would not be multi-threaded. If there are two concurrent requests to your servlet, then 2 instances of your servlet will be created to process these 2 requests. You can implement the single thread model by implementing the SingleThreadModel interface in your class. This is just a marker interface and does not have any methods.

The multi threaded model means that your servlet would be multi-threaded and only one instance would exist. Multiple concurrent requests would be served by the same instance but in different threads. You can implement the multi threaded model by not implementing the SingleThreadModel interface in your servlet class.

You will generally use single thread model when your servlet maintains instance variables that will contain state specific to each request. But you can very well do this using synchronization or method local variables instead of using the single thread model. In fact using instance variables or static variables to maintain request-specific state is a bad practice and can & should be avoided. The biggest disadvantage of this model is that it is resource intensive. Your server can easily get bogged down in case your site has a lot of users accessing single threaded model servlets. One point to remember is that even though you implement single thread model, your web application is not thread safe because you may have data stored in session and application scopes which would need to be synchronized manually by you. So the best approach in this case would be to use synchronization instead of single thread model.

If in your servlet, there is no chance of instance and/or static variables being updated by multiple concurrent requests, then you can use the multi threaded model. But remember still that you need to take care of synchronization of session and application data.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions