简体   繁体   English

OkHttpClient 线程安全吗?

[英]Is OkHttpClent thread-safe?

enter image description here在此处输入图片说明

Okhttp uses OkHttpClent to send requests. Okhttp 使用 OkHttpClent 发送请求。

can I use the same OkHttpClent instance to requests in different Threads ?我可以使用相同的 OkHttpClent 实例在不同的线程中请求吗?

Yes.是的。 OkHttpClient is thread safe, and the use case you describe is expected. OkHttpClient 是线程安全的,您描述的用例是预期的。 Do not create a new client instance per thread.不要创建每个线程一个新的客户端实例。

But at the same time, consuming a single Response from multiple threads is probably unwise.但同时,从多个线程使用单个 Response 可能是不明智的。

See docs here https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared在此处查看文档https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM