Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Single and Multi Thread Model

 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI. Please give me a brief explanation about SingleThreadModel and MultiThreadModel.Thanks.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are multi-threaded. That means that a single servlet instance can be called from multiple threads (created for serving multiple users) simultaneously. And that means that the servlet code needs to be thread-safe, e.g. one needs to be very careful when using instance variables.

The SingleThreadModel was an attempt at side-stepping this problem by telling the servlet container: "this servlet code is not thread-safe, so if you have two simultaneous threads wanting to execute it, either create a second instance of the servlet, or wait until the first instance is finished executing".

As it turned out, this by itself was not sufficient to guarantee thread-safety. As a consequence, the interface has been deprecated. Any servlets you write need to be written in a thread-safe manner.
In the abstract that means: Any access to shared mutable state needs to be synchronized. In practice it means to not use instance variables that can change during the lifetime of a servlet. That by itself is not sufficient, but it is the source of most thread-safety problems.
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI and Thanks Ulf Dittmer .Understood it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic