title: Android / Java: Asynchronous job in a loop
body: <p>I have a method that updates the list of accounts asynchronously in my android app.</p>
  
  <p>The methods looks like this</p>
  
  <pre><code>public void updateAccounts(List&lt;String&gt; accountIds, final SampleCallback callback) {
      for (String accountId : accountIds) {
           getAccountsAsync(accountId, new MySampleCallback() {
               @Override public void result(AccountResponse response) {
                   callback(response);
               }
           }
      }
  }
  </code></pre>
  
  <p>This is the method that updates the accounts.</p>
  
  <pre><code>void getAccountsAsync(String accountId, final MySampleCallback callback) {
       //Execution of request
       callback.result(new AccountsResponse("Just a sample response from the service"));
  }
  </code></pre>
  
  <p>and this is my callback interface</p>
  
  <pre><code>public interface MySampleCallback{
      void result(AccountsResponse response);
  }
  </code></pre>
  
  <p>The problem is, in my execution of the loop, I only get the result of the last item of the list e.g
  I have a list of String -> "One", "Two", "Three"</p>
  
  <p>The only result that I get is the one from the last item which is three.</p>
  
  <p>The other two are being ignored, however they are still being executed but the only result that returns to me is the last item.</p>
  
  <p>What I'm trying to do is, before the loop's next iteration, can I stop it before executing the next one? More like running making this Async run Synchronously.</p>
  
  <p>Thanks.</p>