3

OkHttp 2.0.0-RC1 uses ThreadPoolExecutor defined in Dispatcher#getExecutorService:

executorService = new ThreadPoolExecutor(
  0, Integer.MAX_VALUE,
  60, TimeUnit.SECONDS,
  new LinkedBlockingQueue<Runnable>(),
  Util.threadFactory("OkHttp Dispatcher", false));`

which is essentially implementation of Executors#newFixedThreadPool.

On the other hand Retrofit uses Executors.newCachedThreadPool defined in Platform#defaultHttpExecutor which boils down to:

executorService = new ThreadPoolExecutor(
  0, Integer.MAX_VALUE,
  60, TimeUnit.SECONDS,
  new SynchronousQueue<Runnable>(),
  someThreadFactory);

Anyone has any ideas why OkHttp uses Executors#newFixedThreadPool and Retrofit Executors#newCachedThreadPool?

Wiktor Gworek
  • 486
  • 3
  • 7

2 Answers2

6

They're both closer to cached thread pool. The first two parameters are the most important: the minimum number of threads to run concurrently and the maximum number of threads to run concurrently. If either Retrofit or OkHttp aren't doing any work, they won't consume any threads. If they're running 1000 concurrent jobs, they'll consume 1000 threads.

The 60 seconds is how long a thread lives after it is no longer needed. This is the caching bit: the thread is kept around for a minute so that if a new job comes in, no thread allocation is necessary.

So the difference comes down to SynchronousQueue vs. LinkedBlockingQueue. I prefer the LinkedBlockingQueue because the enqueuing thread doesn't need to wait for the running thread to start up.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
0

ThreadPoolExecutor will only create a new thread if the backing queue is full(1). So in OkHttp if you're making multiple requests around the same time, all but the first will get queued since corePoolSize is initialized to 0.

For client calls, use the CachedThreadPool.

  1. "If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full." https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
drewpt
  • 46
  • 1
  • 4