title: waiting on asynchronous http requests in java
body: <p>I'm using Jetty HTTP Client to make about 50 HTTP calls asynchronously.  The code looks something like this:</p>
  
  <pre><code>List&lt;Address&gt; addresses = getAddresses();
  final List&lt;String&gt; done = Collections.synchronizedList(new LinkedList&lt;String&gt;());
  List&lt;ContentExchange&gt; requests;
  for (Address address : addresses) {
      ContentExchange ce = new ContentExchange() {
          @Override
          protected void onResponseComplete() throws IOException {
              //handle response
              done.add("done");
          }
      }
      ce.setURL(createURL(address));
      requests.add(ce);
  }
  for (ContentExchange ce : requests) {
      httpClient.send(ce);
  }
  
  while (done.size() != addresses.size()) {
      Thread.yield();
  }
  
  System.out.println("All addresses processed");
  </code></pre>
  
  <p>It's calling a rest service that returns back some data about the address.  What I expect it to do is this:</p>
  
  <ol>
  <li>Make 50 asynchronous (non-blocking) http calls.</li>
  <li>The thread will wait until all 50 are finished.</li>
  </ol>
  
  <p>However, it's not working.  It works fine if I don't have the while loop, but I need to wait until all 50 are done.  Is there some way to wait until all 50 are done?</p>
  
  <p>Also I know about ExecutorService and multiple thread solution, but I need a single thread solution with non-blocking IO.</p>