Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

which data is ThreadSafe ?

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you gowher
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya true..why not B? using STM means making instance variables thread safe
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic