These seems to come up a lot, I've personally been involved in this discussion a number of times over the past two months. Someone should probably write a Wiki entry somewhere (assuming one doesn't exist already).
The discussion generally it seems comes to this conclusion: We've all heard that "spawning threads in a servlet container is a no-no", and it may even be forbidden by J2EE, but... doing it is OK if you are careful and COMFORTABLE with thread programming. It is VERY easy to get yourself in trouble, so if you don't really know what your doing, find another solution. If you DO know what your doing though, proceed with caution, but proceed none the less. Specific points: you usually will want a daemon thread for period tasks (non-daemon threads can cause a container to not be able to shut down properly) and you generally want to set the thread priority as low as you can. Also, you NEVER want to hold references to any request-oriented resources (in fact, your thread shouldn't even USE them, shouldn't even be aware that it's running in a servlet container, if possible). Also, be VERY wary of spawning threads to handle requests. Some people do this and it works out well (i.e., for some long-running process that the UI is going to poll for later), but it's a dangerous road. At least, that's my recollection of the usual outcome of the discussion. I have an application where I spawn four threads at startup, one for initialization tasks that dies shortly after startup, and three that run for the life of the app to do periodic tasks. The only trouble I've ever had was when I didn't create them as daemon threads, then Tomcat wouldn't shut down. Making them damons solved that problem. Otherwise, this app has been in production for over a year with a respectable load, and there has been no ill effect. I was careful when I wrote them, thought pessimistacally about what could be happening at any given point in time, and it has worked out well. Just be careful! :) -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com On Tue, February 1, 2005 12:56 pm, [EMAIL PROTECTED] said: > I had yesterday a general discussion about Threads running in the > Servlet-Engine Tomcat. > > What i would like to do is to start a Thread over the Struts-Plugin. The > Thread self is running in a infinite > <http://dict.leo.org/se?lp=ende&p=/Mn4k.&search=infinite> loop > <http://dict.leo.org/se?lp=ende&p=/Mn4k.&search=loop> to do something > and to sleep for 30sec. > > Does anyone have expiriences with this? > I think it shoudn't be a problem when i start the Thread and terminate > it when the Application is terminating. But i heard that isn't a best > practise to run a thread in Tomcat. But i don't know where... > > Greets > Felix Klieber > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > >
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]