views:

3457

answers:

7

One of the first things I've learned about J2EE development is that I shouldn't spawn my own threads inside a J2EE container. But when I come to think about it, I don't the reason. Can you provide a clear explanation why it is discouraged?

I am sure most enterprise applications need some kind of asynchronous jobs like mail daemons, idle sessions cleanup jobs etc.. So, if indeed one shouldn't spawn threads, what is the correct way to do it when needed?

A: 

I've never read that it's discouraged, except from the fact that it's not easy to do correctly.

It is fairly low-level programming, and like other low-level techniques you ought to have a good reason. Most concurrency problems can be resolved far more effectively using built-in constructs like thread pools.

levand
it is indeed forbidden by the spec.
Ken Liu
+5  A: 

The reason that you shouldn't spawn your own threads is that these won't be managed by the container. The container takes care a lot of things that a novice developer is hard to imagine. For example things like thread pooling, clustering, crash recoveries are performed by the container. When you start a thread you may lose some of those. Also the container lets you restart your application without affecting the JVM it runs on. How this would be possible if there are threads out of the container's control?

This the reason that from J2EE 1.4 timer services were introduced. See this article for details.

kgiannakakis
+14  A: 

It is discouraged because all resources within the environment are meant to be managed, and potentially monitored, by the server. Also, much of the context in which a thread is being used is typically attached to the thread of execution itself. If you simply start your own thread (which I believe some servers will not even allow), it cannot access other resources. What this means, is that you cannot get an InitialContext and do JNDI lookups to access other system resources such as JMS Connection Factories and Datasources.

There are ways to do this "correctly", but it is dependent on the platform being used.

The commonj WorkManager is common for WebSphere and WebLogic as well as others

More info here

And here

Also somewhat duplicates this one from this morning

Robin
+1  A: 

There is no real reason not to do so. I used Quarz with Spring in a webapp without problems. Also the concurrency framework java.util.concurrent may be used. If you implement your own thread handling, set the theads to deamon or use a own deamon thread group for them so the container may unload your webapp any time.

Arne Burmeister
A: 

You can always tell the container to start stuff as part of your deployment descriptors. These can then do whatever maintainance tasks you need to do.

Follow the rules. You will be glad some day you did :)

Thorbjørn Ravn Andersen
A: 

One reason I have found if you spawn some threads in you EJB and then you try to have the container unload or update your EJB you are going to run into problems. There is almost always another way to do something where you don't need a Thread so just say NO.

Javamann
+4  A: 

For EJBs, it's not only discouraged, it's expressly forbidden by the specification:

An enterprise bean must not use thread synchronization primitives to synchronize execution of multiple instances.

and

The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enterprise bean must not attempt to manage thread groups.

The reason is that EJBs are meant to operate in a distributed environment. An EJB might be moved from one machine in a cluster to another. Threads (and sockets and other restricted facilities) are a significant barrier to this portability.

Dan Dyer