• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about threadsafe variable

 
Sam Shen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I know the local and request variable is threadsofe, so for the following code:
public void doGet(....){
...
Servletcontext sc=getServletContext();
Object obj=sc.getAttribute("obj");
...
}
Are sc and obj both threadsafe?
Thanks,
Sam
 
Aleks V. Pascoal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,
i think they are both not thread safe, because despite the fact you use local variables, they are pointing to objects avaiable for the context, wich are accessible for any other requests. So you must think about the original object not the variable pointing to it.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, but if all the servlet will do is get a constant attribute, it wouldn't matter much.
Otherwise, you could try this:

It's not an elegant solution, but it will restrict access to the servlet ocntexct object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic