我正在尝试实现一个使用 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/