You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Manivannan Palanichamy <ma...@gmail.com> on 2007/09/01 07:25:07 UTC

Threads in tomcat application.

Hi

I need to design a web application that may use threads. Thats, the web
application might have to read some 200 files from network. In order to
speed up the process, I've decided to use threads/thread pooling. But,
however I know it is not a good practice to use threads in a web/server
application... Just want to know whether this will be a great impact in
future.. Otherwise, do you suggest any alternative design for the above
problem? Looking forward a quick reply :-)

-- 
Manivannan Palanichamy
http://mani.gw.googlepages.com/index.html

Re: Threads in tomcat application.

Posted by Juha Laiho <Ju...@iki.fi>.
Manivannan Palanichamy wrote:
> I need to design a web application that may use threads. Thats, the web
> application might have to read some 200 files from network. In order to
> speed up the process, I've decided to use threads/thread pooling. But,
> however I know it is not a good practice to use threads in a web/server
> application... Just want to know whether this will be a great impact in
> future.. Otherwise, do you suggest any alternative design for the above
> problem? Looking forward a quick reply :-)


This might be a correct place for multithreading: slowness is somewhere
else than actual processing load of the server (and most probably
implementing this with multithreading will not saturate the network
connection of the server for extended times, either). So, one danger
of multithreading (as with all multiprocessing) is that your application
can starve itself of resources, and make the server spend most of its
time switching from one task to another (trashing). Tune the pool small
enough to avoid this danger, but large enough to benefit of being able to
interleave several tasks.

That being said, a long-running server application (such as a webapp running
under Tomcat) needs different care with threads than a end-user application.

Potential issues you'll need to handle in your code:
- threads may get stuck while attempting to do something (esp. when you're
  reading data over network); make sure your code doesn't have any place
  where a thread may hang for an indefinite time; make sure each thread
  will terminate with predetermined results for each possible case
  (otherwise your code will leak threads, and through that, memory)
- make sure all threads teminate also when the application is undeployed
  (for whatever reason, such as deploying a newer version of the webapp;
  again, this is to avoid memory/thread leaks)
- if possible, make all threads as "daemon threads" - so that a hung
  thread will not prevent Tomcat Java process from exiting
-- 
..Juha

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org