简体   繁体   English

在企业Java应用程序中创建线程的正确方法是什么

[英]What is the correct way to create threads in an enterprise java application

I have seen quite a lot of posts stating that creating threads using Java SE concurrency is not recommended. 我已经看到很多帖子,指出不建议使用Java SE并发创建线程。 Why is this, and what is the correct way to spawn a second thread in my code? 为什么会这样?在我的代码中产生第二个线程的正确方法是什么? If possible, I would prefer to keep to the core Java API. 如果可能,我希望保留核心Java API。

The correct way to do this is by using the Concurrency Utils API, which is part of the Java EE7 release. 正确的方法是使用Concurrency Utils API,它是Java EE7版本的一部分。 Creating threads this way ensures that the thread has access to all the other enterprise services. 通过这种方式创建线程可确保该线程可以访问所有其他企业服务。 Using the Concurrency Utils ensures that your thread is created and managed by the container. 使用并发实用程序可确保您的线程是由容器创建和管理的。

Please see here and here for examples 在这里这里查看示例

You can use Java SE concurrency in Java EE, but this is not recommended is all depends on how you are going to use it, what is the requirement. 您可以在Java EE中使用Java SE并发,但是不建议这样做,这完全取决于您将如何使用它,这是什么要求。 If you create thread in Java EE then make sure they are properly managed and free from deadlock situation and must set timeout. 如果在Java EE中创建线程,请确保对其进行了适当的管理,并且没有死锁情况,并且必须设置超时。 These all are easily managed by java concurrency ExecutorService 所有这些都可以通过java并发ExecutorService轻松管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM