java - 在运行时更改 java 中的池连接管理器中的线程数

标签 java multithreading threadpool

我正在尝试实现一个使用 ThreadSafeClientConnManager 的项目,池中的最大线程数为 20。现在,根据流量,我想更改运行时的线程数。有可能做到吗?

我有一个可行的解决方案是,我有一组新的线程池,在更改最大连接时初始化它,然后替换现有的线程池。一种缓冲开关。但我不确定这是否是最佳解决方案。

最佳答案

我假设您现在正在使用Executors.newFixedThreadPool。遗憾的是,该池无法动态调整大小。

更好的自适应解决方案是Executors.newCachedThreadPool。看看javadoc .

Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache.

关于java - 在运行时更改 java 中的池连接管理器中的线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487167/

相关文章:

.net - .NET线程池和配置

Java:ExecutorService 在特定队列大小后阻止提交

java - Java Servlets中这条语句是什么意思?

multithreading - VB6 Timer 控件是否创建单独的线程?

c - 如何在多线程中使用 printf()

java - 大型数组中元素的并行求和

asp.net - Task.Factory.StartNew 对 ASP.Net MVC 有帮助还是有害?

java.lang.NoClassDefFoundError : Could not initialize class org. codehaus.groovy.vmplugin.v7.Java7

java - List<> 对象引用如何工作?

java - 带有 MockMvcBuilders 的 SpringBootTest 独立设置没有加载我的 ControllerAdvice 尽管设置了它