title: Best way to wait in Java
body: <p>I have an app that needs to wait for some unknown amount of time.  It must wait until several data fields are finished being populated by a server.</p>
  
  <p>The server's API provides me a way to request data, easy enough...</p>
  
  <p>The server's API also provides a way to receive my data back, one field at a time.  It does not tell me when all of the fields are finished being populated.</p>
  
  <p>What is the most efficient way to wait until my request is finished being processed by the server?  Here's some pseudocode:   </p>
  
  <pre><code>public class ServerRequestMethods {
      public void requestData();
  }
  
  public interface ServerDeliveryMethods {
      public void receiveData(String field, int value);
  }
  
  public class MyApp extends ServerRequestMethods implements ServerDeliveryMethods {
      //store data fields and their respective values
      public Hashtable&lt;String, Integer&gt; myData;    
  
      //implement required ServerDeliveryMethods
      public void receiveData(String field, int value) {
          myData.put(field, value);    
      }
  
      public static void main(String[] args) {
          this.requestData();
  
          // Now I have to wait for all of the fields to be populated,
          // so that I can decide what to do next.
  
          decideWhatToDoNext();
          doIt();
      }
  }
  </code></pre>
  
  <p>I have to wait until the server is finished populating my data fields, and the server doesn't let me know when the request is complete.  So I must keep checking whether or not my request has finished processing.  What is the most efficient way to do this?</p>
  
  <p>wait() and notify(), with a method guarding the while loop that checks if I have all of the required values yet every time I'm woken up by notify()?</p>
  
  <p>Observer and Observable, with a method that checks if I have the all the required values yet every time my Observer.Update() is called?</p>
  
  <p>What's the best approach?  Thanks.  </p>