Home >Java >javaTutorial >Why Should You Avoid Custom Threading in Java EE Containers?
Threading in Java EE Containers: Why It's Advised Against
In Java EE development, creating custom threads within the container is generally discouraged. Understanding the rationale behind this recommendation is crucial for effective code design.
Reason for Discouragement
Within a Java EE container, all resources, including threads, are intended to be managed and potentially monitored by the server. Creating custom threads can lead to resource conflicts, as they may not be aware of the managed resources at the server level. Additionally, threads often carry context specific to their execution, which limits their access to other resources.
Alternative Approaches
While creating custom threads is discouraged, there are preferred methods for executing asynchronous tasks in Java EE environments. These methods include:
Example
For instance, instead of spawning a separate thread for email sending, consider using an MDB. An MDB can listen for messages containing email details and process them asynchronously. This approach ensures that email sending is managed within the container's resource management system.
Note: While this article primarily addresses Java EE development in 2009, it's important to acknowledge that advancements have been made in the Java EE ecosystem since then. However, the underlying principles of resource management and context awareness remain valid.
The above is the detailed content of Why Should You Avoid Custom Threading in Java EE Containers?. For more information, please follow other related articles on the PHP Chinese website!