如何限制ManagedExecutorService执行的线程数

How to limit the number of threads excecuted by ManagedExecutorService

class javax.enterprise.concurrent.ManagedExecutorService 管理企业 Java Bean 中的线程。 As threads in EJBs should not be started in the classical Java SE way, I use the ManagedExecutorService to handle threads.

"Obligatory warning: Creating your own threads in a Java EE app server (even Tomcat) is discouraged as it can be a huge performance issue and in most cases will prevent container functionality, such as JNDI, from working. The new threads won't know which application they belong to, the Thread context classloader will not be set and many other hidden issues."

在JavaSE中可以定义最大线程数,如下代码所示。

java.util.concurrent.Executors.newFixedThreadPool(numberOfThreadsInThePool);

你知道如何限制ManagedExecutorService执行的线程数吗?

当你使用EE容器时,你应该通过容器的配置来解决线程数的问题。对于 Glassfish,请查看有关线程池管理和性能调整的 Oracle 文档。

最大线程池大小可以在 Glassfish 应用服务器中创建 ManagedExcecutorService 时确定。根据这篇文章 https://blogs.oracle.com/arungupta/entry/create_managedexecutorservice_managedscheduledexecutorservice_managedthreadfactory_contextservice glassfish 的 asadmin 命令有一个参数 "maximumpoolsize",它定义了 ManagedExcecutorService 使用的最大线程数。

asadmin create-managed-executor-service concurrent/myExecutor --maximumpoolsize=20