Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which data is ThreadSafe ?

 
muthu moorthy
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you gowher
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya true..why not B? using STM means making instance variables thread safe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic