43

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?

For example,

Thread 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
    }
  }

Thread2

...
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
    }
  }

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

Lii
  • 11,553
  • 8
  • 64
  • 88
Dmitrii Demenev
  • 735
  • 1
  • 5
  • 13

1 Answers1

55

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

Question

I believe a single instance of OkHttpClient should be reused to create multiple connections. Is OkHttpClientthread safe? If not, isOkHttpClient.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!

Question

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

Answer

Yes

Conclusion

OkHttpClients should be shared

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
Remario
  • 3,813
  • 2
  • 18
  • 25
  • 1
    I am using OkHttp3. If I make http call to multiple domain (like `https://api_1.test.com` and `https://api_2.test.com`) then should I use multiple OkHttpClient or single client will enough? – seal Dec 08 '21 at 18:17
  • If the time delay between 2 http requests is more 30 seconds, then My OkHttp Client is creating a new requsest even I declare it as shared singleton. If I execture http requests back to back without any delay then it is not creating a new client it is using the same client How to address this ?? Pls assist – KK_07k11A0585 Aug 25 '23 at 14:13