Java应用线程创建

标签 java multithreading threadpool application-server

我正在开发一个 java 应用程序。我们正在使用 Spring,应用程序运行 WebSphere 应用程序服务器。在整个应用程序中,我们维护多个使用 Spring 在应用程序内创建的线程池。如下所示,到目前为止,我们还没有遇到任何问题。

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="maxPoolSize" value="100"/>
        <property name="corePoolSize" value="10"/>
</bean>

但是我在阅读 Spring 框架引用文档时遇到了下面的段落,

34.3.3 TaskScheduler implementations

As with Spring’s TaskExecutor abstraction, the primary benefit of the TaskScheduler is that code relying on scheduling behavior need not be coupled to a particular scheduler implementation. The flexibility this provides is particularly relevant when running within Application Server environments where threads should not be created directly by the application itself. For such cases, Spring provides a TimerManagerTaskScheduler that delegates to a CommonJ TimerManager instance, typically configured with a JNDI-lookup.

ref. https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html



所以我的问题是,如果我不想访问 Java EE 上下文信息,那么还有什么其他原因可以使用托管线程池?
我基本上想知道那里的最佳实践是什么,如果让应用程序托管线程是完全 Not Acceptable 。

最佳答案

通常,企业服务器(如 WebSphere)上的线程池(以及 JDBC 连接)不应由应用程序管理,而应由服务器本身管理。 Spring 仅在您的应用程序和服务器的线程池模型之间提供一个接口(interface)。

在服务器端配置线程池的主要好处是效率:在过去,通常将多个应用程序部署到同一台服务器上。使用共享线程池,可以利用服务器的所有资源。如今,将多个应用程序保留在同一个 Java 服务器上被认为是不好的做法(根据微服务架构)。因此,只需将此类线程池视为遗留技术堆栈的一部分。

关于Java应用线程创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45800059/

相关文章:

Java线程..导致线程在设定时间后终止

java - 如何中止 JDBC Postgresql CopyManager 复制?

java - 如何中断给定 Future 对象的线程?

c# - try catch 捕获 .NET 线程池中的错误

java - dropwizard:从非文件源读取配置

java - createJdbcRowSet源代码

java - 为什么使用 CopyOnWriteArrayList 时结果很奇怪?

ruby - 如何处理 'lock' 数据库行

java - Java 中每个键的线程池

java - 在 Flash 页面中,选择框(组合框)无法使用 Webdriver 工作