aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread Safe? Request Attributes and Local Variable 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 Safe? Request Attributes and Local Variable" Watch "Thread Safe? Request Attributes and Local Variable" New topic
Author

Thread Safe? Request Attributes and Local Variable

Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Can someone please help me to understand why and how Request attributes and local variables are thread safe?

I can have different servlets of a web application setting request attributes, correct? Also, when multiple threads can access a method then their local variables can also be accessed so how local variables become thread safe?

Thanks for your help!


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Raj Menon
Ranch Hand

Joined: Oct 18, 2005
Posts: 45
Whenever a request comes for any servlet,the container creates Request, Response objects
and also a seperate thread to process that request.
Even if another request comes for the same servlet,the container does the same.
So there is no link b/w different Requests and the attributes that you set to those Requets.

And come to the Local variables..
As i mentioned earlier that it processes the requests in seperate threads,
again there is no link b/w the local variables that you use in any local methods of one request and that of another request.
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
I got slightly confused with this myself (regarding local variables).

If the variables belong to the *class*, they you're right - they are not safe. Different invocations of the same method by different threads could conflict with each other.

However, if the variables are local to the *method*, each thread gets its own copy. These would be safe.

My confusion stemmed from the fact that these were just called "local variables" and I wasn't sure exactly what they were local to. I remember getting a few practice questions wrong, thinking that they were local to the class.

Hope that helps,

MG


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Raj Menon
Ranch Hand

Joined: Oct 18, 2005
Posts: 45
As far as i know local variables mean - They are local to that method only.
If they belong to class then we call them as instance variables
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Thanks Rajesh.

Yes, Local variables means variables declared inside a method or in the argument of a method.
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
Originally posted by Atul Sawant:

I can have different servlets of a web application setting request attributes, correct


Hi Atul,

Setting request attributes means you are setting the attributes for that particular request object. Request objects are created new for each of the user request. So if you set an attribute in this case then it becomes limited only to that request object alone. So it is definitely thread safe to set request attributes.

Setting attributes to session and context objects is never thread safe since they are applied to a larger scope where other servlets /JSP can act on the same.



 
wood burning stoves
 
subject: Thread Safe? Request Attributes and Local Variable