并行调用 OkHttpClient 是否线程安全? [英] Is it thread-safe to make calls to OkHttpClient in parallel?

查看:388
本文介绍了并行调用 OkHttpClient 是否线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程同时运行,其中一些需要从 Internet 请求数据.我需要关心他们对 OkHttpClient 单例访问的同步吗?

I have several threads that run at the same time and some of them need to request data from Internet. Do I need to care about synchronization of their access to OkHttpClient singleton?

例如

线程 1:

...
Request request = new Request.Builder()
    .url("http://hell.com/siners.txt")
    .build();

client.newCall(request).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
    e.printStackTrace();
  }

  @Override public void onResponse(Call call, Response response) throws IOException {
    try (ResponseBody responseBody = response.body()) {
      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
      // Some work in Thread1
    }
  }

线程 2:

...
Request request = new Request.Builder()
    .url("http://hell.com/slutList.txt")
    .build();

client.newCall(request).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
    e.printStackTrace();
  }

  @Override public void onResponse(Call call, Response response) throws IOException {
    try (ResponseBody responseBody = response.body()) {
      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
      // Some work in Thread2
    }
  }

同时使用 newCall().enque() 或 newCall().execute() 是否有潜在危险?

Can simultaneous newCall().enque() or newCall().execute() be potentially dangerous?

推荐答案

你问的问题很好,请允许我全面阐述.基于为OkHttp开放的github问题,我的总结是这样的.

You asked a very good question, allow me to expound holistically. Based on github issues opened for OkHttp, my summary is this.

问题:我相信应该重用 OkHttpClient 的单个实例来创建多个连接.OkHttpClient 线程安全吗?如果不是,OkHttpClient.open() 线程安全吗?

Question: I believe a single instance of OkHttpClient should be reused to create multiple connections. Is OkHttpClient thread safe? If not, is OkHttpClient.open() thread safe?

答案:是的.它被设计为单例.通过使用单个实例,您可以获得共享响应缓存、线程池、连接重用等.一定要这样做!

Answer: Yes. It is designed to be treated as a singleton. By using a single instance you are afforded a shared response cache, thread pool, connection re-use, etc. Definitely do this!

问题:线程安全怎么样?OkHttpClient 线程安全还是至少它的 open() 函数是安全的?

Question: How about thread safety? Is OkHttpClient thread safe or at least its open() function?

答案:是的

结论:

OkHttpClients 应该被共享

当您创建单个 OkHttpClient 实例并将其重用于所有 HTTP 调用时,OkHttp 的性能最佳.这是因为每个客户端都拥有自己的连接池和线程池.重用连接和线程可减少延迟并节省内存.相反,为每个请求创建一个客户端会浪费空闲池上的资源.

OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a client for each request wastes resources on idle pools.

这篇关于并行调用 OkHttpClient 是否线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆