You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by adam <aa...@columbiaexchange.com> on 2004/04/27 15:51:03 UTC

multithreading within one session

Hello all.

 

I have done the following experiment, and I am curious of why I am getting
these results:

 Basically I wanted to know what happens if I send parallel requests within
the same session

for the same action class/path.

 

Ie, I am doing the following:

-          get the session id cookie from the server 

-          log in

-          then schedule n threads for execution at the same moment in time.
Each thread has the same session id

cookie and makes a request for the same path(action class)

Now, the results I am getting are a bit different from what I was expecting.

Basically I thought that I couldn't have multithreading within the same
session, but that I would get a response for request 1 after milliseconds,
then a response for request two after 2n milliseconds and so on.

 

Instead it seems that I am (roughly ) getting all responses after m*n time
where the m is the number of threads and n is the length of time to service
one request. Actually it may not be exactly that time(i.e. m*n) but the idea
is that the more threads I have the longer the response time for all
threads, and all threads finish at about the same time..

 

So, my question is: what exactly is happening when multiple threads access
the same action class within one session?

Regards, Alex Adam


RE: multithreading within one session

Posted by Andrew Hill <an...@gridnode.com>.
Struts is built on top of the servlet api.

In the servlet api a servlet will (normally) be multithreaded - with a
single instance of the servlet class being used to process all requests for
it simultaneously. (In practice things like the OS issues, clustering,
etc... effect how this pans out of course).

In the case of struts the servlet is the ActionServlet. Your Actions are
merely helper classes that are used by the action servlet to do the
app-specific processing for a particular request path mapping. Struts is
designed such that it will only instantiate one Action class instance for
each Action mapping (per JVM), thus all requests for that mapping (from any
session) will be processed by the same action class instance - which is why
you need to write them in a thread safe manner.

In the context of a single session there is nothing done by either the
servlet api or struts to prevent multiple simultaneous requests related to
the same session. (and the Servlet API & Struts have no place in trying to
impose such restrictions!)

The session is essentially just an id number that allows the servlet api to
identify which HttpSession to provide to the HttpRequest it passes your
servlet.

What this implies of course is that if you are using the session to hold
stuff then you will need to consider what happens when multiple threads need
to access it at the same time. The session methods getAttribute() and
setAttribute() are synchronized for you, but if you need to play with
subproperties of the thing you have in that attribute or to play with
several attributes in an atomic manner etc.. then you will need to consider
such issues.

For the most part a user would only be submitting one request at a time -
but this is just a result of the way a typical user might use your app. It
is quite possible for a user to open several windows in their browser - all
relating to the same session - and be doing stuff in all of them (Which does
not play nice with session scoped ActionForms!). Another case where multiple
requests on the same session will commonly occur is where your using frames
as most browsers will typically send simultaneuous requests for each frame.




-----Original Message-----
From: Jason Miller [mailto:jason.miller@danteconsulting.com]
Sent: Tuesday, 27 April 2004 23:57
To: Struts Users Mailing List
Subject: Re: multithreading within one session


The framework itself does nothing special in this case - the threading
model (and hence, the handling of this scenario) is an implementation
detail of the container.  Struts assumes that the actions it is calling
are reentrant, and Struts itself is reentrant, so it does nothing
special from this perspective.

adam wrote:

> Yes, but I was hoping to understand how the Struts framework handles
> a scenario where the server receives n "simultaneous" requests for the
same
> session. Let's just suppose that the requests are really simultaneous(or
at
> least arrive in an amount of time that is far less that it would take for
> the action class to complete a single threaded request.)
>
>
> -----Original Message-----
> From: Jason Miller [mailto:jason.miller@danteconsulting.com]
> Sent: Tuesday, April 27, 2004 10:17 AM
> To: Struts Users Mailing List
> Subject: Re: multithreading within one session
>
> The behavior you are describing is a platform-dependent artifact of the
> process model for the OS you are using.  There is no guarantee regarding
> the execution order for Threads.  There is not even a guarantee that
> Threads will multiprocess - it is entirely feasible for the VM to
> schedule their execution one after the other, just as it is for the VM
> to round robin using timeslicing.
>
> So far as multithreading within a session, there isn't anything that
> prevents it, although the synchronization issues always give me pause.
>
> So as an answer, what you are experiencing is a result of the OS you are
> running, the VM you are running, and the implementation of the app
> server you are running.  In general, you shouldn't rely on these details
> unless you are locked to this combination.
>
> A good book to read - Java Threads
> (http://www.oreilly.com/catalog/jthreads2/)
>
> Sorry if I rambled and danced around
> Jason
>
> adam wrote:
>
>
>>Hello all.
>>
>>
>>
>>I have done the following experiment, and I am curious of why I am getting
>>these results:
>>
>> Basically I wanted to know what happens if I send parallel requests
>
> within
>
>>the same session
>>
>>for the same action class/path.
>>
>>
>>
>>Ie, I am doing the following:
>>
>>-          get the session id cookie from the server
>>
>>-          log in
>>
>>-          then schedule n threads for execution at the same moment in
>
> time.
>
>>Each thread has the same session id
>>
>>cookie and makes a request for the same path(action class)
>>
>>Now, the results I am getting are a bit different from what I was
>
> expecting.
>
>>Basically I thought that I couldn't have multithreading within the same
>>session, but that I would get a response for request 1 after milliseconds,
>>then a response for request two after 2n milliseconds and so on.
>>
>>
>>
>>Instead it seems that I am (roughly ) getting all responses after m*n time
>>where the m is the number of threads and n is the length of time to
>
> service
>
>>one request. Actually it may not be exactly that time(i.e. m*n) but the
>
> idea
>
>>is that the more threads I have the longer the response time for all
>>threads, and all threads finish at about the same time..
>>
>>
>>
>>So, my question is: what exactly is happening when multiple threads access
>>the same action class within one session?
>>
>>Regards, Alex Adam
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: multithreading within one session

Posted by Jason Miller <ja...@danteconsulting.com>.
The framework itself does nothing special in this case - the threading 
model (and hence, the handling of this scenario) is an implementation 
detail of the container.  Struts assumes that the actions it is calling 
are reentrant, and Struts itself is reentrant, so it does nothing 
special from this perspective.

adam wrote:

> Yes, but I was hoping to understand how the Struts framework handles
> a scenario where the server receives n "simultaneous" requests for the same
> session. Let's just suppose that the requests are really simultaneous(or at
> least arrive in an amount of time that is far less that it would take for
> the action class to complete a single threaded request.)
> 
> 
> -----Original Message-----
> From: Jason Miller [mailto:jason.miller@danteconsulting.com] 
> Sent: Tuesday, April 27, 2004 10:17 AM
> To: Struts Users Mailing List
> Subject: Re: multithreading within one session
> 
> The behavior you are describing is a platform-dependent artifact of the 
> process model for the OS you are using.  There is no guarantee regarding 
> the execution order for Threads.  There is not even a guarantee that 
> Threads will multiprocess - it is entirely feasible for the VM to 
> schedule their execution one after the other, just as it is for the VM 
> to round robin using timeslicing.
> 
> So far as multithreading within a session, there isn't anything that 
> prevents it, although the synchronization issues always give me pause.
> 
> So as an answer, what you are experiencing is a result of the OS you are 
> running, the VM you are running, and the implementation of the app 
> server you are running.  In general, you shouldn't rely on these details 
> unless you are locked to this combination.
> 
> A good book to read - Java Threads 
> (http://www.oreilly.com/catalog/jthreads2/)
> 
> Sorry if I rambled and danced around
> Jason
> 
> adam wrote:
> 
> 
>>Hello all.
>>
>> 
>>
>>I have done the following experiment, and I am curious of why I am getting
>>these results:
>>
>> Basically I wanted to know what happens if I send parallel requests
> 
> within
> 
>>the same session
>>
>>for the same action class/path.
>>
>> 
>>
>>Ie, I am doing the following:
>>
>>-          get the session id cookie from the server 
>>
>>-          log in
>>
>>-          then schedule n threads for execution at the same moment in
> 
> time.
> 
>>Each thread has the same session id
>>
>>cookie and makes a request for the same path(action class)
>>
>>Now, the results I am getting are a bit different from what I was
> 
> expecting.
> 
>>Basically I thought that I couldn't have multithreading within the same
>>session, but that I would get a response for request 1 after milliseconds,
>>then a response for request two after 2n milliseconds and so on.
>>
>> 
>>
>>Instead it seems that I am (roughly ) getting all responses after m*n time
>>where the m is the number of threads and n is the length of time to
> 
> service
> 
>>one request. Actually it may not be exactly that time(i.e. m*n) but the
> 
> idea
> 
>>is that the more threads I have the longer the response time for all
>>threads, and all threads finish at about the same time..
>>
>> 
>>
>>So, my question is: what exactly is happening when multiple threads access
>>the same action class within one session?
>>
>>Regards, Alex Adam
>>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: multithreading within one session

Posted by adam <aa...@columbiaexchange.com>.
Yes, but I was hoping to understand how the Struts framework handles
a scenario where the server receives n "simultaneous" requests for the same
session. Let's just suppose that the requests are really simultaneous(or at
least arrive in an amount of time that is far less that it would take for
the action class to complete a single threaded request.)


-----Original Message-----
From: Jason Miller [mailto:jason.miller@danteconsulting.com] 
Sent: Tuesday, April 27, 2004 10:17 AM
To: Struts Users Mailing List
Subject: Re: multithreading within one session

The behavior you are describing is a platform-dependent artifact of the 
process model for the OS you are using.  There is no guarantee regarding 
the execution order for Threads.  There is not even a guarantee that 
Threads will multiprocess - it is entirely feasible for the VM to 
schedule their execution one after the other, just as it is for the VM 
to round robin using timeslicing.

So far as multithreading within a session, there isn't anything that 
prevents it, although the synchronization issues always give me pause.

So as an answer, what you are experiencing is a result of the OS you are 
running, the VM you are running, and the implementation of the app 
server you are running.  In general, you shouldn't rely on these details 
unless you are locked to this combination.

A good book to read - Java Threads 
(http://www.oreilly.com/catalog/jthreads2/)

Sorry if I rambled and danced around
Jason

adam wrote:

> Hello all.
> 
>  
> 
> I have done the following experiment, and I am curious of why I am getting
> these results:
> 
>  Basically I wanted to know what happens if I send parallel requests
within
> the same session
> 
> for the same action class/path.
> 
>  
> 
> Ie, I am doing the following:
> 
> -          get the session id cookie from the server 
> 
> -          log in
> 
> -          then schedule n threads for execution at the same moment in
time.
> Each thread has the same session id
> 
> cookie and makes a request for the same path(action class)
> 
> Now, the results I am getting are a bit different from what I was
expecting.
> 
> Basically I thought that I couldn't have multithreading within the same
> session, but that I would get a response for request 1 after milliseconds,
> then a response for request two after 2n milliseconds and so on.
> 
>  
> 
> Instead it seems that I am (roughly ) getting all responses after m*n time
> where the m is the number of threads and n is the length of time to
service
> one request. Actually it may not be exactly that time(i.e. m*n) but the
idea
> is that the more threads I have the longer the response time for all
> threads, and all threads finish at about the same time..
> 
>  
> 
> So, my question is: what exactly is happening when multiple threads access
> the same action class within one session?
> 
> Regards, Alex Adam
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: multithreading within one session

Posted by Jason Miller <ja...@danteconsulting.com>.
The behavior you are describing is a platform-dependent artifact of the 
process model for the OS you are using.  There is no guarantee regarding 
the execution order for Threads.  There is not even a guarantee that 
Threads will multiprocess - it is entirely feasible for the VM to 
schedule their execution one after the other, just as it is for the VM 
to round robin using timeslicing.

So far as multithreading within a session, there isn't anything that 
prevents it, although the synchronization issues always give me pause.

So as an answer, what you are experiencing is a result of the OS you are 
running, the VM you are running, and the implementation of the app 
server you are running.  In general, you shouldn't rely on these details 
unless you are locked to this combination.

A good book to read - Java Threads 
(http://www.oreilly.com/catalog/jthreads2/)

Sorry if I rambled and danced around
Jason

adam wrote:

> Hello all.
> 
>  
> 
> I have done the following experiment, and I am curious of why I am getting
> these results:
> 
>  Basically I wanted to know what happens if I send parallel requests within
> the same session
> 
> for the same action class/path.
> 
>  
> 
> Ie, I am doing the following:
> 
> -          get the session id cookie from the server 
> 
> -          log in
> 
> -          then schedule n threads for execution at the same moment in time.
> Each thread has the same session id
> 
> cookie and makes a request for the same path(action class)
> 
> Now, the results I am getting are a bit different from what I was expecting.
> 
> Basically I thought that I couldn't have multithreading within the same
> session, but that I would get a response for request 1 after milliseconds,
> then a response for request two after 2n milliseconds and so on.
> 
>  
> 
> Instead it seems that I am (roughly ) getting all responses after m*n time
> where the m is the number of threads and n is the length of time to service
> one request. Actually it may not be exactly that time(i.e. m*n) but the idea
> is that the more threads I have the longer the response time for all
> threads, and all threads finish at about the same time..
> 
>  
> 
> So, my question is: what exactly is happening when multiple threads access
> the same action class within one session?
> 
> Regards, Alex Adam
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org