• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Safety??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I want to know which of the following are not Thread safe!

Context-scoped attributes
Session-scoped attributes
Request-scoped attributes
Instance variables in the servlet
Local variables in service method
Static variables in the servlet

I know that the first 3 are not thread safe and that we have to synchronize them. What about Instance variables?? Local variables inside the service method is thread safe reason every service method runs in a seperate thread. I want to know what about Static variables and Instance variables?? Any thoughts??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables are thread safe only when the servlet is implementing the SingleThreadModel.
But class or static variables are never thread safe.

This information i got while doing a mock test.
But i am not sure if this is correct.

Thanks.
Rohini.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jyothi,
Only Request Attributes and Local variables in service method are thread safe.
Request attributes are thread safe because each request will run in a separate thread and there is no chance for other threads to access the attributes of that request.

Instance variables and static variables of the servlet are not thread safe , there will be only one instance of the servlet shared by multiple threads.Every thread has acess to the instance and static variables of the servlet and hence not thread safe.

Thanks and Regards,
Purna Chandra Rao.
SCJP 1.4
[ January 21, 2007: Message edited by: Purna Chandra Rao ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Purna and Rohini,

Thanks for the replies!
 
reply
    Bookmark Topic Watch Topic
  • New Topic