• 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

Webapps thread basic questions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thank you very much for the patience and your time to answer the basic questions I have regarding the threads in web applications..
Please let me know if you need any more information to clarify my doubts.

1.) Based on my knowledge each http request creates its own thread. Is that correct?

2) Do we say a web application is multithreaded only when a piece of code inside the app for the given http request, is creating its own new threads (apart from the main thread created for the original http request)
(like for example new Thread-->run() kind of stuff) .

OR

Can we say a webapp is multi threaded because there are several http request at the same time
(assuming no code is creating its own new threads).

3) Assuming a particular Class A is not singleton and does not have any static elements and assuming app is creating new threads inside a piece of
code for a given http request and using the object(objectA of class A).

We need to worry about thread safety in this case as the same object is shared across different threads. Is that correct?

4) Assuming a particular object is not singleton and does not have any static elements and Assuming NO piece of code creating new threads.

We dont need to worry about thread safety of this object for multiple http requests that web applciation receives at the same time. In this case each http request creates its own object instance (objectA1 for httprequest1 and objectA2 for httprequest2) and so we dont have to worry about thread safety , if that correct?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudeep narayana wrote:I thank you very much for the patience and your time to answer the basic questions I have regarding the threads in web applications..
Please let me know if you need any more information to clarify my doubts.

1.) Based on my knowledge each http request creates its own thread. Is that correct?


Generally correct. Usually there is a thread pool which contain a specific number of Threads that get shared between requests.


2) Do we say a web application is multithreaded only when a piece of code inside the app for the given http request, is creating its own new threads ... OR ... say a webapp is multi threaded because there are several http request at the same time...


The application is multithreaded because each request can be run in a different Thread. Since there are many paths for data to be shared between requests, then there is a need to be conscious of the thread safety issues.


3) Assuming a particular Class A is not singleton and does not have any static elements and assuming app is creating new threads inside a piece of code for a given http request and using the object(objectA of class A). We need to worry about thread safety in this case as the same object is shared across different threads. Is that correct?



Correct.


4) Assuming a particular object is not singleton and does not have any static elements and Assuming NO piece of code creating new threads. We dont need to worry about thread safety of this object for multiple http requests that web applciation receives at the same time. In this case each http request creates its own object instance (objectA1 for httprequest1 and objectA2 for httprequest2) and so we dont have to worry about thread safety , if that correct?



Whenever working in a web app you have to be wary of the thread safety of the objects you use. One way of implementing thread safety is, as you say, make a new Object for each Thread, so there is no possibility of data sharing between threads. The problem is that you have to make sure you do it right. For example:
1) In your servlets and JSPs, make sure you don't generate the reference to the Object you are creating as a class member variable. The instance of the Servlet is shared between all requests, so storing values in Servlet is paramount to sharing the data between threads. Instead, make all Objects method local variables.

2) Don't store the Object in any scope which may be shared between requests. For example the ServletContext or Session scopes, or in any Object or collection which can be stored in those scopes.
 
sudeep narayana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve Luke

thank you very much for the answers and your time.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Steve has already said,a multithreaded web application generally refers to the multithreaded implementation of the web server on which the application is deployed. So if you have a servlet based application deployed on a web server,the term refers to the inherent multithreaded nature of the web server. It doesn't really indicate the possibility of the fact that new threads could be spawned as part of execution of the service request.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I cleared my doubts by reading this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic