Why is spawning threads in Java EE container discouraged?

前端 未结 9 1911
清歌不尽
清歌不尽 2020-11-22 04:50

One of the first things I\'ve learned about Java EE development is that I shouldn\'t spawn my own threads inside a Java EE container. But when I come to think about it, I do

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:33

    The reason that you shouldn't spawn your own threads is that these won't be managed by the container. The container takes care of a lot of things that a novice developer can find 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.

提交回复
热议问题