简体   繁体   中英

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. 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.

The correct way to do this is by using the Concurrency Utils API, which is part of the Java EE7 release. 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. If you create thread in Java EE then make sure they are properly managed and free from deadlock situation and must set timeout. These all are easily managed by java concurrency ExecutorService

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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