J2EE specification says its not advisable to create or spawn user defined threads into application logic. The reason as i understood is to avoid resource contention, synchronization issues and memory leaks.
But it also suggest to use ManagedExecutorService or WorkManager API implementation. Both of these implementations also spawns threads under the hood.
Then how it is appropriate to use these API's and not using Threads directly ?
First off, the WorkManager API is only available to JCA resource adapters. Thus I'll mostly address the ManagedExecutorService, but most of what I say is applicable to WorkManager as well.
The difference between using ManagedExecutorService / WorkManager and creating your own threads is that the application server manages the underlying threads. This means the threads you use with this API, and more generally via the javax.enterprise.concurrent.*
classes, whether implicitly in the case of asynchronous task submission with a ManagedExecutorService, or explicitly vith a ManagedThreadFactory, will have a guaranteed consistency with the services provided by the application server, in particular transactions management, security, resources management etc.