Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it thread-safe to make calls to OkHttpClient in parallel?

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,

Thread1:

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

like image 646
Dmitrii Demenev Avatar asked Jan 31 '18 00:01

Dmitrii Demenev


People also ask

Do I need to close OkHttpClient?

Shutdown Isn't Necessary The threads and connections that are held will be released automatically if they remain idle. But if you are writing a application that needs to aggressively release unused resources you may do so.

Is OkHttpClient an async?

Asynchronous Get (.OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

What is OkHttpClient in retrofit?

Okio-okhttp3 is a library that works in conjunction with java.io and java. nio to make data access, storage, and processing considerably easier. It started as a component of OkHttp. Retrofit is a type-safe REST client for Java and Android application development.

What is OkHttp connection pool?

OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.


1 Answers

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

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!

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.

like image 144
Remario Avatar answered Sep 28 '22 10:09

Remario