Two Laptop Bag
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread Safety?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Thread Safety??" Watch "Thread Safety??" New topic

Thread Safety??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032


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??

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
rohini gundu

Joined: Nov 16, 2006
Posts: 12
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.

Purna Chandra Rao

Joined: Feb 06, 2006
Posts: 23
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

Purna and Rohini,

Thanks for the replies!
I agree. Here's the link:
subject: Thread Safety??
jQuery in Action, 3rd edition