aspose file tools*
The moose likes Threads and Synchronization and the fly likes Webapps thread basic questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Webapps thread basic questions" Watch "Webapps thread basic questions" New topic
Author

Webapps thread basic questions

sudeep narayana
Greenhorn

Joined: May 31, 2009
Posts: 5
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?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: May 31, 2009
Posts: 5
Steve Luke

thank you very much for the answers and your time.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
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.
sneha jag
Greenhorn

Joined: Jul 08, 2009
Posts: 1
Thanks
I cleared my doubts by reading this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Webapps thread basic questions