• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleThreadModel

 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What scope variables are thread safe.
Can anyone explain me what happens when I implement SingleThreadModel interms of different scoped varialbes.
TIA
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I am wrong but I believe only local variables and both the request and response are thread safe.
 
Shashank Tanksali
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SingleThreadModel only ensures that there is only one invocation of the servlet at any point of time. It has nothing to do with the thread safety of variables.
It is perfectly feasible that a single servlet may spawn several threads (not servlet threads, but application threads), which might attempt to modify the variables in the servlet.
So it is upto the servlet to synchronize methods where the variable value could possibly get modified.
Just my 2 cents . . .
Shashank
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting because that's not what JWebPlus states. I know they are not the authority and I checked the specs but they aren't too clear about it. I'll see if I can run JWebPlus again and get their explanations where they state local variables and the request object are thread safe. Be back.
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the JWebPlus question:

Here's the answer:

Only local objects and request attributes are thread safe. Because, those objects are never shared accross the threads at all.
All other objects namely, session, context and the servlet itself, can be accessed by multiple threads simultaneously and so the developer needs to take care that multiple threads do not modify the shared objects in an inconsistant ways.
Request object is available only for that particular request. For a new HTTP request, a new Request object is created that's why it is thread safe. (Except of course if you create a new thread and then pass this object to it.)

Hope this helps...
[ May 31, 2002: Message edited by: Ricardo Cortes ]
 
Deepti Chalichama
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are thread safe too when a class implements the SingleThreadModel interface.
Reference: page 52 of more Servlets and JavaServerPages.
Regards,
Deepti.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic