• 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

Java Concurrency Live Lessons - Tomcat web application

 
Ranch Hand
Posts: 180
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question may sound stupid (and it probably is), but, I have a problem in understanding the code when threads are automatically created by the web container - Tomcat.
My understanding is Tomcat uses a threadpool, and uses a separate thread for processing each request. However, I don't see any code in our codebase that does any synchronization around some shared data. Now, I understand that request processing is independent, and they don't need to pass any data to each other, but I don't think our codebase has absolutely no shared data.
My question is how do I conceptualize the web application code into threads communicating with each other, and their shared data?
 
Ranch Hand
Posts: 33
VI Editor Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a couple ways i can see thread interaction happening:
  • Static variables
  • spawned threads from within the request thread
  • caching libraries


  • Static variables are a way to share data between request threads.
    As a real life example:
  • enums (which should be threadsafe out of the box)
  • loaded configurations inside used frameworks
  • static DateFormat object can cause realy weird results (as they're not threadsafe), so NEVER do this.


  • On the other hand, nothing stops you from creating additional threads within your request handling thread.

    A way of sharing data between request threads is by using a caching mechanism, but most of the caching libraries/frameworks handle the multithreading themselves (provided they're configured correctly).

    To be fair, most of the times you won't need to worry about the request threads interacting as long as you don't use static variables.
    When the data you share needs to be persisted, even when the tomcat server restarts, or the application is reloaded/redeployed, you'll most likely be using a database to store the data. In that case it will most likely suffice to fetch the data from the database with the following request (or the persistence framework inbetween).

     
    Salil Wadnerkar
    Ranch Hand
    Posts: 180
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you so much, Stefaan. That was helpful!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic