OKHTTP Singleton object to handle different API calls in a multi-threaded system

Viewed 288

I've a multi-threaded application which has multiple different API calls for different purposes. My question is if I make a singleton object of OKHTTP and multiple threads try to make the API call at the same time with different endpoints, Will it cause any problem for me or not? Or should I use the singleton object for same endpoints?

1 Answers

OkHttpClient objects are safe for concurrent use. This is exactly what you're supposed to do.

https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared

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.

Related