OkHttp3 icon indicating copy to clipboard operation
OkHttp3 copied to clipboard

关于okhttp3线程池的问题

Open xh520md opened this issue 5 years ago • 1 comments

您好,最近我在使用okhttp3时,每一个请求都会创建一个线程去执行,看了下okhttp3的源码。ConnectionPool类有一个静态初始化: executor = new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp ConnectionPool", true));我没理解错的话,是初始化为 0,最大2147483647个线程,线程存活时间为60秒。这里有一个疑问,第一,虽然线程会回收,但是,我高并发请求点时,线程会快速的创建。而空闲线程要等着回收才会释放资源,能否再利用空闲线程?第二,就是线程的命名问题。这里没有给出线程号,线程池内的全部线程都是OkHttp ConnectionPool名称,这里建议加上编号,"OkHttp ConnectionPool-%d"。这样在查线程状态时,能快速定位线程?以上是我的两个问题,期待回复,谢谢🙏

xh520md avatar Nov 18 '19 02:11 xh520md

Hello, recently I was looking at the source code of okhttp3 and found that the maximum number of threads per route for asynchronous requests is 5. But connections are not reused under synchronous requests, only new connections are created and cleaned up periodically.

guozongkang avatar May 01 '20 04:05 guozongkang