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]



Re: Can servlets safely spawn threads?

2007-01-29 Thread Bill Au

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]




Re: Can servlets safely spawn threads?

2007-01-29 Thread Christopher Schultz
-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]



Re: Can servlets safely spawn threads?

2007-01-29 Thread David Delbecq
En l'instant précis du 01/29/07 11:33, Danny Ayers s'exprimait en ces
termes:
> Hi,
>
> I would be grateful is someone could answer these questions:
>
> * Can servlets safely spawn threads?

short answer, yes. They *can*. However that does not mean all spawned
Threads are safe.
>
> * If so, under what conditions?
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.

2) Never have your spawned thread access request-time specific
informations. This mean no request/response object passed to spawned
threads. Avoid passing the Session object too. Lots of stuffs exposed by
tomcat to servlet are assuming only one thread will work on it, the http
thread, as such, there is quite a good amount of possible use of
ThreadLocal variables.

3) if possible, spawn daemon threads, and name your threads, this ease a
lot debugging.

I have seen working web application spawning their own thread.
Applications using the Quartz scheduler are an example of them. The
Quartz scheduler spawns a few threads a provides mecanism for servlet to
stop them.

In general, try to avoid spawning your own thread. however, it's not
always possible, and the Specs does not forbid spawning thread. But
handle them properly. The best way, if you need your own thread, is to
never use in the spawned thread object managed byt the container.

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


-
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