Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes which data is ThreadSafe ? 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 "which data is ThreadSafe ?" Watch "which data is ThreadSafe ?" New topic
Author

which data is ThreadSafe ?

muthu moorthy
Ranch Hand

Joined: Dec 19, 2006
Posts: 87
Any body help me to find out the answer ?
Thank you.

I have got three answer A, D and E.

A developer wants to use 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 data in a local variable
B.Store data in a instance variable
C.Store data in a HttpSession object
D.Store data in a ServletContext object
E.Store data in a HttpServletRequest object
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
First of all you should never use SingleThreadModel interface.
HttpServletRequest objects are threadsafe by default.
Local objects or variables(inside service method) are threadsafe.
ServletContext are not threadSafe even if you implement SingleThreadModel
HttpSession objects are not threadSafe even if you implement SingleThreadModel.
Instance variables(variables inside class but outside service method) are not threadSafe.
Static variables are not threadSafe.
muthu moorthy
Ranch Hand

Joined: Dec 19, 2006
Posts: 87
Thank you gowher
Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
I am a little confused here

Gowher Naik wrote:
Instance variables(variables inside class but outside service method) are not threadSafe.


Given that this question involves using SingleThreadModel (STM). I think even B is a valid option here. With STM even instance variables are rendered thread safe.

Now in HFSJ 2nd edition it is given that vendors can implement STM in two ways

1) Using queuing, where each thread is made to wait for "the one" servlet instance. In this case instance variable is definitely thread safe
2) Multiple instance of servlet are made available in a pool and each request is directed to individual instances (with only one thread per instance allowed). Now in this case instance variables are NOT thread safe since at parallel many threads are accessing it.

So since implementation is vendor-dependent, is this the reason why option B is NOT considered valid here OR I am confusing lots of things here ??


Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
ya true..why not B? using STM means making instance variables thread safe


Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
 
GeeCON Prague 2014
 
subject: which data is ThreadSafe ?