title: How is OkHttp performing parallell HTTP Requests with seemingly synchronous HTTP connections, without the use of threading?
body: <p>I've done some performance testing with the <a href="http://square.github.io/okhttp/" rel="noreferrer">OkHttp</a> library and found it to be great. It did 80 requests to <a href="http://httpbin.org/delay/1" rel="noreferrer">http://httpbin.org/delay/1</a>, which deliberately pauses 1 sec for each request, in 4.7s on my HTC One phone. I've looked at the code and I'm trying to find out <em>why</em> it is so fast. The developers (Square Inc) advertise Connection Pooling and Async calls, both which I believe contribute to the good performance.</p>
  
  <p>I come from the .NET world and in .NET 4.5 you have a true async HTTP library with an Async <a href="http://msdn.microsoft.com/en-us/library/hh158912.aspx" rel="noreferrer">GetResponse-method</a>. By yielding the thread to the OS while waiting for the response, you free up resources to initiate more HTTP requests, or other stuff. The thing is I cannot see the same pattern with OkHttp (or any other HTTP library for Android I've looked into). So how can I  still execute 80 1-second-request in 4 seconds? It's not thread-based, right? I'm not firing up 80 (or 20) threads?</p>
  
  <p>To be specific, in <a href="https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/Call.kt" rel="noreferrer">com.squareup.okhttp.Call.beginRequest()</a>, I see no yielding of the thread between the <code>sendRequest</code> and <code>getResponse</code> calls:</p>
  
  <pre><code>if (canceled) return null;
  
  try {
      engine.sendRequest();
  
      if (request.body() != null) {
          BufferedSink sink = engine.getBufferedRequestBody();
          request.body().writeTo(sink);
      }
  
      engine.readResponse();
  } catch (IOException e) {
      HttpEngine retryEngine = engine.recover(e, null);
      if (retryEngine != null) {
          engine = retryEngine;
          continue;
      }
  
      // Give up; recovery is not possible.
      throw e;
  }
  
  Response response = engine.getResponse();
  </code></pre>
  
  <p>So again, how is making 80 "parallell" calls possible? </p>
  
  <p>I don't <em>need</em> to know this in order to use the library, but async programming interests me and I'd really like to understand how OkHttp/SquareInc has solved this. </p>