Re: Can servlets safely spawn threads?

2007-02-07 Thread Danny Ayers

Many thanks!

On 29/01/07, Bill Au [EMAIL PROTECTED] wrote:

If you are spawning threads from your servlet, make sure that they are
cleaned up when
the servlet is destroyed.  Otherwise, you will have a thread leak.  I have
ran into this problem
when the webapp is reloaded without restarting the server.  Each leaked
thread has a
reference to its classloader, preventing that classloader from being garbage
collected.
Reload enough time will run the perm space out of memory.

Bill


On 1/29/07, Christopher Schultz [EMAIL PROTECTED] wrote:

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Danny,

 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
 system.

 - -chris

 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.6 (MingW32)
 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

 iD8DBQFFvgK+9CaO5/Lv0PARAoIFAJ45DgOyFV9qxS2e+Qt9uHNTtkWpywCdHbnq
 pltJVjDsmhZMg0143155k7M=
 =pbEp
 -END PGP SIGNATURE-

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







--

http://dannyayers.com

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



Can servlets safely spawn threads?

2007-01-29 Thread Danny Ayers

Hi,

I would be grateful is someone could answer these questions:

* Can servlets safely spawn threads?
* If so, under what conditions?

I tried to find the answers searching the web, but found conflicting views.
So I thought it worth asking about a specific servlet container
implemention.

I'm trying to make a very simple asynchronous messaging system on top of
HTTP. What I have in mind requires that the servlet called would complete
the request-response in reasonable time, yet may initiate other processes
that are potentially long-running. The easiest approach would be to have the
servlet spawning another thread in which to run the other process, and
return a response to the client immediately. But is this possible without
running straight into concurrency breakage?

More background at :

http://dannyayers.com/2007/01/28/a-servlets-problem

Thanks,
Danny.

--

http://dannyayers.com