Why is spawning threads in Java EE container discouraged?

前端 未结 9 1719
清歌不尽
清歌不尽 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:27

    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

    UPDATE: Please note that this question and answer relate to the state of Java EE in 2009, things have improved since then!

提交回复
热议问题