• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Safety

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm just starting out on this certification and I have a question about the servlet life-cycle and thread-safety - it may seem basic, but I just want to be sure !

From what I've read there will only ever be once instance of my Servlet class created. When the instance is first created the init-method will be called, followed by the service-method, which will in turn call the doGet-method (for example). Then any subsequent calls to this Servlet will result in a new thread be spawning and calling the service-method of the previously created instance of my Servlet class.

So does this mean that methods that my Servlet implements must be thread-safe? For example my doGet-method implementation should be thread-safe as there could be multiple threads executing this method concurrently.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I just found the answer to my question. Yes, my Servlet class definitely needs to be thread safe as multiple threads will be accessing it concurrently. Unless my Servlet implements the SingleThreadModel interface - in which case I am guaranteed that only one thread will be executing a method from my Servlet class at any particular moment in time. Sounds good?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:...Unless my Servlet implements the SingleThreadModel interface - in which case I am guaranteed that only one thread will be executing a method from my Servlet class at any particular moment in time. Sounds good?


Hello Sean... I am afraid it does NOT sound good because the session and context states will be completely vulnerable!... Its like saying oh well I want to synchronize the service method.
A good solution would be;
-synchronyze sessions
-do NOT use instance variables in your Servlet (if you must, then make them 'final')

HtH

Ikpefua
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ikpefua, confirms what I was thinking, I should ensure that my Servlet implementation is thread-safe.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic