当执行大量请求时,会创建大量线程来维护空闲连接,这些线程处于TIMED_WAITING状态五分钟。
最新版本中存在以下源代码:
class RealBackend(threadFactory: ThreadFactory) : Backend {
private val executor = ThreadPoolExecutor(
0, // corePoolSize.
Int.MAX_VALUE, // maximumPoolSize.
60L, TimeUnit.SECONDS, // keepAliveTime.
SynchronousQueue(),
threadFactory
)
}
来自okhttp/TaskRunner.kt at master · square/okhttp · GitHub
连接池源码:okhttp/ConnectionPool.kt at master · square/okhttp · GitHub
GitHub 存储库问题中的类似反馈:
- About the "okhttp3" thread pool issue🙉 · Issue #5607 · square/okhttp · GitHub
- ConnectionPool ThreadPool why use Integer.MAX_VALUE maximumPoolSize param ? · Issue #5963 · square/okhttp · GitHub
为什么不使用较低的值来限制线程的峰值数量?线程数过高可能会导致问题。
最佳答案
OkHttp 限制了自己的资源使用限制,但这里没有。相反,Dispatcher
限制并发调用的数量,ConnectionPool
限制 Activity 连接的数量。
关于java - 为什么okhttp3连接池使用Int.MAX_VALUE作为maximumPoolSize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70496944/