aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A thread-safe question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A thread-safe question " Watch "A thread-safe question " New topic
Author

A thread-safe question

tiim fei
Greenhorn

Joined: May 22, 2005
Posts: 12
a developer chooses to avoid using SingleThreadModel but wants to ensure that data is updated in a thread-safe manner.
which two can support this design goal ? (Choose two)
A. Store the data in a vocal variable
B. Store the data in an instance variable
c. Store the data in the HttpSession object
D. Store the data in the ServletContext object
E. Store the data in the ServletRequest object

The answer is A,D. I don't understand that. Could anybody help me ?
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
I think the first one wud be volatile variable and not vocal. volatile variables are thread safe since the jvm ensures reading these variables from the source and not from the copy maintained by each thread.


Thnx
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Assuming the first option is local variables. The answer should be A and E.
The local variables, and the request object ( parameter to service methods) are always thread safe.

thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I would also go for : A,E
Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
Hi Narendra,
Yes,By default servlet supports multithreading. So the information in the local variables and information in the HttpServletRequest Object are Thread safe.Because For each client request,new ServletRequest Object and new ServletResponse object and new Thread will be created.After the execution of service(HttpServletRequest,HttpServletResponse) method these newly created HttpServletRequest,HttpServletResponse and Thread executed that service() method becomes candidates for garbage collection.In other way...HttpServletRequest Object is also local to service(HttpServletRequest,HttpServletResponse).So always information in the local variables are Thread safe.
Bye,
Krish.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A thread-safe question