• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A thread-safe question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also go for : A,E
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 knew that guy would be trouble! Thanks tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic