• Post Reply Bookmark Topic Watch Topic
  • New Topic

Single Thread Model  RSS feed

 
P Subramanian
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the advantage in implementing single thread model in thread? The same can be achieved by using the synchronized method in service method also right?
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
if you implement in single thread model, then you dont have to care about synchronizing sat all. thats the advantage. correct me if im wrong!
karl
 
P Subramanian
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi karl,

But we can achieve this by synchronizing the service method. Why do we we go for Singlethread Model?
Subramanian
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll explain this from the beginning. If this is dumbing it down too much for you i appologise but it's a concept that took me a while to get hold of.
For the sake of efficiency, there is only ever one instance of each Servlet. If you have 3 servlets: BobServlet, JohnServlet, BettyServlet, you will have a total of 3 servlet instances running. Ever.
When a request (on its own execution thread) comes in, the application server gets a reference to the required servlet and calls it with the parameters. Other requests could be part of the way through execution inside the SAME SERVLET INSTANCE.
If you have any instance variable associated with a servlet then it is not thread-safe, since any request could change an instance variable that another request is relying on.
The easiest way to get around this is to never have any instance variables or always think of them as static.
If you absolutely need to have instance variables and Thread safety, you can implement the SingleThreadModel. This acts as a flag to the app server that only a single thread should be allowed to access a servlet instance at a time.
The difference between this and synchronising the service method is that initially the app server only has one instance of the servlet.
If you use the synchronised (second time for the Australian spelling ) solution then only one request can be serviced for that servlet at a time and the rest get blocked.
If you use the SingleThreadModel solution it is possible that the same thing happens, but it also allows the possibility (and this is the way I'd try it) for the application server to notice the SingleThreadModel and create multiple instances so that many requests can be served on the servlet without interfering wiith each other.
I think this is correct. There's a nice link to a page at sun on Thread-Safe servlets but I can't find it at the moment. Hopefully someone else can drop it for us.
Dave.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!