title: How to handle a mess of async operations?
body: <p>I have a task for starting up in an Android app. It consistent of various subtasks, each of which has a completion. Some things need to run in sequence, others are just fire and forget… but I want everything to complete before the startup task itself completes.</p>
  <p>What built-in classes provide functionality like this? The existing code (quite old) uses AsyncTask, but every call in it is blocking. That works, but a lot of these blocking APIs have been deprecated, and I don't see any way to make AsyncTask do what I want and continue to run past the end of <code>doInBackground</code>. (Plus yes, AsyncTask itself has been deprecated.)</p>
  <p>This is roughly what the old code looks like:</p>
  <pre><code>public class StartupTask extends AsyncTask&lt;Void, Void, Void&gt; implements TaskInterface {
  
      protected Void doInBackground(Void... params) {
  
          // several steps here
  
          String token = FirebaseInstanceId.getInstance().getToken();
          subscribeToAllChannels(token);
  
          // several more steps, including using token
  
      }
  </code></pre>
  <p>This way of getting the token is deprecated now; I'm supposed to call a method to get the token which later calls a delivery block. This is a really simplified example: the actual code is much more complicated, and involves a bunch more asynchronous operations, and I don't control many of the methods that it calls.</p>
  <p>The new code would have to look more like this (this is just psuedocode):</p>
  <pre><code>protected void start() {
      getToken(new Runnable(){
          @override void onResult(String token){
              somethingElse(token, new Runnable(){
                  // etc, more levels, until ultimately:
                  finished();
              });
          }
      });
  }
  </code></pre>
  <p>Something easier to read would be <em>even better</em>, as I imagine someone's thought of this more than I have, but I'd be satisfied with this basic case: something a bit like iOS NSOperation's concurrent mode, where instead of implementing main/doInBackground/run I implement a start method and later explicitly end the task.</p>
  <p>What are the tools to do this on Android/Java? Every class I've looked at makes the same assumption: the task is complete when <code>doInBackground()</code> (or <code>run()</code>, or whatever) is complete. I haven't found any way to leave it hanging and complete it in response to a completion.</p>
  <p>I'm looking for something built in… or, at least small. Not to import a massive framework that changes how I need to do this everywhere.</p>