Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

about threadsafe variable

 
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
 
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.
 
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.
 
I'm so happy! And I wish to make this tiny ad happy too:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic