This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SIngleThreadModel

 
Dean Jones
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the SIngleThreadModel in the servlets
does it mean that our servlet will be from any knd of synchronizartion problems

Dean Jones
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are correct.. but you must realize that it creates an instance of the servlet for every request! so its is very resource consuming servlet... usually i wouldnt recoomend using it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it doesn't necessarily free you from any synchronization problems. Since as Roy noted you can still have multiple threads active simultaneously (but using different Servlet instances), you must be careful about accessing static members of your Servlet class. Or about accessing any other shared resources outside the Servlet class itself. Really, SingleThreadModel is of very limited use.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these.
Since there are now multiple servlet instances handling each request, the instance variables are now thread safe.
In either case, however, if any servlet static variables are defined, these will NOT be thread safe unless you explicitly make it so with custom code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic