如何设置okhttp 3线程池大小

时间:2019-11-19 15:48:50

标签: java okhttp

对不起。我使用okhttp作为网络转发。 TPS的峰值时间约为10000。我对网络进行了仿真,并发送了10,000次。 测试用例:

for(int i = 0; i < 10000; i++)}{
    / / okhttp3  net forward
}

然后我使用Java visualvm查看了线程数,发现线程数正在增加并接近请求数。当请求数达到2000左右时,程序出现内存溢出错误。

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:714)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:949)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1371)
    at okhttp3.ConnectionPool.put(ConnectionPool.java:153)
    at okhttp3.OkHttpClient$1.put(OkHttpClient.java:163)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:266)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at com.nantian.easytrade.http.util.HttpCallHandle.execute(HttpCallHandle.java:331)
    at com.nantian.easytrade.http.util.HttpCallHandle.call(HttpCallHandle.java:191)
    at com.nantian.easytrade.http.util.HttpCallHandle.call(HttpCallHandle.java:176)
    at com.test.Main.main(Main.java:28)

作为一个android网络请求,这可能不是问题,但是作为Java应用程序发出请求时,发送线程的数量有时会很高,太多线程会导致应用程序处理缓慢或出错。 Okhttp3版本是3.11.0 JDK版本是1.7

0 个答案:

没有答案
相关问题