• 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

servlet thread safety

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted a question about servlet instance variable thread safety yesterday.  But I just got another basic question and figured it may be easier to post a separate thread here.

For a servlet like

1. if I have 10 users accessing "MyServlet" from different computers, does container let them share a single "MyServlet" instance with 10 different threads ?  
2. if one user opens 10 windows or tabs on his computer to access "MyServlet", does container let these 10 tabs share a single "MyServlet" instance with 10 different threads ?  

I think the answer to question 2) is YES.  But for question 1), I guess it is yes but I am not sure.  

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly (it's been quite some time since I've worked with servlets), it's up to the container. So the bottom line remains to avoid instance variable in servlets. As mentioned in the other topic, the servlet specification affords scoped variables to handle data in a safer and controllable manner.
 
Bartender
Posts: 217
7
Android Python Oracle Postgres Database Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a somehow "official" statement. IMHO it says that the servlet instance is guaranteed to be valid only during request processing. In case of recycling of instances the only safe way is to assume nothing about the content of instance data.

From: Java Servlet Specification version 4 / 2017 3.13 Lifetime of the Request Object


Each request object is valid only within the scope of a servlet’s service method, or
within the scope of a filter’s doFilter method, unless the asynchronous processing
is enabled for the component and the startAsync method is invoked on the request
object. In the case where asynchronous processing occurs, the request object remains
valid until complete is invoked on the AsyncContext. Containers commonly recycle
request objects in order to avoid the performance overhead of request object
creation. The developer must be aware that maintaining references to request objects
for which startAsync has not been called outside the scope described above is not
recommended as it may have indeterminate results.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic