You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by kwirirai <kw...@designat7.co.zw> on 2004/02/15 18:28:08 UTC

Threads in Tomcat

Thanks to All, I have done all the stuff to make my web app thread safe,
 It seems its fine for the moment.Except for a few more things.
It seems threading is realy complex. I am forking out a new thread in my 
Handle request method and this is making the other request to wait for 
the other thread to complete.

Actualy what is happening is this I have this code , I am using Velocity

  public Template handleRequest( HttpServletRequest request,
    HttpServletResponse response, Context context )
    {       

      .......
       .......
         someCode1;
         gmh=new MyThread();
         new Thread(gmh).start();
         someCode2;
}


when two clients make a request ,suppose client1 makes a request then 
manages to start the thread first then client2 makes a request while 
client1 thread has already started running ,client2 will somehow be 
forced to wait on someCode1  until client1 thread has finished .Can you 
imagine if client1 takes a very long time to finish,that means client2 
will also take a very long time

I do not think it should do this
I was thinking of using a thread pool, now I need some example code to 
use a thread pool havent got any idea were to start, just some pointers 
to help me ,dont want to give you too much trouble :-)

Thanks in advance


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


Re: How Servlets work?

Posted by saravanan <sa...@naturesoft.net>.
Thanks a lot Harry. Thank you so much.

Saravanan
Harry Mantheakis wrote:

>Hello
>
>  
>
>>how the webserver handles more than one
>>request at the same time.
>>    
>>
>
>The answer is: by invoking the servlet's service method in a new thread for
>each new request to that servlet.
>
>Think of any Java object, instantiated once - a public method in that object
>can be called any number of times, concurrently. Each invocation will be
>processed in a new thread.
>
>Synchronisation is an issue only when the method in question uses references
>to other objects. If the method only works with local variables, there is no
>synchronisation problem!
>
>So, in a nutshell: with servlets, make sure the service method only uses
>local variables, including, of course, those two most useful parameters, the
>request and the response objects.
>
>Regards
>
>Harry Mantheakis
>London, UK
>
>
>  
>
>>Hi all,
>>
>>may be not the right place to ask this question. Hope somebody is kind
>>enough to clarify this doubt of mine.
>>
>>i understand, in  a servlet, if i need any code to be thread safe, then
>>i shud put that code inside a synhronized block
>>or declared the entire method as synchronized. if my understanding is
>>right, only instance of our serlvet class is being
>>created. in that case how the webserver handles more than one request at
>>the same time. i mean, is some queuing structure
>>being followed, (i am tempted to believe it is not multithreaded)?
>>
>>Could anybody clarify this doubt of mine or atleast point out some ref.
>>material for this.
>>
>>Thanks
>>R.Saravanan
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
>
>  
>



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


Re: How Servlets work?

Posted by Harry Mantheakis <ha...@mantheakis.freeserve.co.uk>.
Hello

> how the webserver handles more than one
> request at the same time.

The answer is: by invoking the servlet's service method in a new thread for
each new request to that servlet.

Think of any Java object, instantiated once - a public method in that object
can be called any number of times, concurrently. Each invocation will be
processed in a new thread.

Synchronisation is an issue only when the method in question uses references
to other objects. If the method only works with local variables, there is no
synchronisation problem!

So, in a nutshell: with servlets, make sure the service method only uses
local variables, including, of course, those two most useful parameters, the
request and the response objects.

Regards

Harry Mantheakis
London, UK


> Hi all,
> 
> may be not the right place to ask this question. Hope somebody is kind
> enough to clarify this doubt of mine.
> 
> i understand, in  a servlet, if i need any code to be thread safe, then
> i shud put that code inside a synhronized block
> or declared the entire method as synchronized. if my understanding is
> right, only instance of our serlvet class is being
> created. in that case how the webserver handles more than one request at
> the same time. i mean, is some queuing structure
> being followed, (i am tempted to believe it is not multithreaded)?
> 
> Could anybody clarify this doubt of mine or atleast point out some ref.
> material for this.
> 
> Thanks
> R.Saravanan
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 


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


How Servlets work?

Posted by saravanan <sa...@naturesoft.net>.
Hi all,

  may be not the right place to ask this question. Hope somebody is kind 
enough to clarify this doubt of mine.

i understand, in  a servlet, if i need any code to be thread safe, then 
i shud put that code inside a synhronized block
or declared the entire method as synchronized. if my understanding is 
right, only instance of our serlvet class is being
created. in that case how the webserver handles more than one request at 
the same time. i mean, is some queuing structure
being followed, (i am tempted to believe it is not multithreaded)?

Could anybody clarify this doubt of mine or atleast point out some ref. 
material for this.

Thanks
R.Saravanan


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