tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Can servlets safely spawn threads?
Date Mon, 29 Jan 2007 14:20:46 GMT
Hash: SHA1


David Delbecq wrote:
> 1) Your servlet must always take care to finish all Threads it has
> spawned. This mean when servlet gets unloaded, you must have provided a
> mecanism in your servlet to stop all running threads your servlet has
> created. Remaining Threads not handled by container can either cause
> tomcat to not stop when requested or prevent garbage collecting of
> webapp when reloading.

Consider using an existing thread pool component. It should have a
graceful shutdown capability already built into it. David is right: you
have to make sure to shut it down when appropriate.

> In general, try to avoid spawning your own thread.

I agree, which is why I recommend using a thread pool. Yes, you are
still technically spawning your own threads (the container will not do
it for you), but the point is that you will not be spawning a thread for
each request.

Set up your thread pool to have a "reasonable" number of threads for
your application. You do not want to have loads of users submit jobs
that will each spawn a new thread. If you run out of threads in the
thread pool, you simply tell the user to come back later. Management of
this type of resource is essential to maintaining a stable and useful

- -chris

Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla -


To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message