title: How are Kotlin's Coroutines different from Java's Executor in Android?
body: <p>I'm an Android developer switching from Java to Kotlin, and I am planning to use the coroutines to handle asynchronous code since it looks very promising.</p>
  
  <p>Back in Java, to handle asynchronous code I was using the <code>Executor</code> class to execute a time-consuming piece of code in another thread, away from the UI thread. I had an <code>AppExecutors</code> class that I injected in my <code>xxxRepository</code> classes to manage a set of <code>Executor</code>. It looked like this : </p>
  
  <pre><code>public class AppExecutors
  {
      private static class DiskIOThreadExecutor implements Executor
      {
          private final Executor mDiskIO;
  
          public DiskIOThreadExecutor()
          {
              mDiskIO = Executors.newSingleThreadExecutor();
          }
  
          @Override
          public void execute(@NonNull Runnable command)
          {
              mDiskIO.execute(command);
          }
      }
  
      private static class MainThreadExecutor implements Executor
      {
          private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
  
          @Override
          public void execute(@NonNull Runnable command)
          {
              mainThreadHandler.post(command);
          }
      }
  
      private static volatile AppExecutors INSTANCE;
  
      private final DiskIOThreadExecutor diskIo;
      private final MainThreadExecutor mainThread;
  
      private AppExecutors()
      {
          diskIo = new DiskIOThreadExecutor();
          mainThread = new MainThreadExecutor();
      }
  
      public static AppExecutors getInstance()
      {
          if(INSTANCE == null)
          {
              synchronized(AppExecutors.class)
              {
                  if(INSTANCE == null)
                  {
                      INSTANCE = new AppExecutors();
                  }
              }
          }
          return INSTANCE;
      }
  
      public Executor diskIo()
      {
          return diskIo;
      }
  
      public Executor mainThread()
      {
          return mainThread;
      }
  }
  </code></pre>
  
  <p>Then I was able to write some code like this in my <code>xxxRepository</code> :</p>
  
  <pre><code>executors.diskIo().execute(() -&gt;
          {
              try
              {
                  LicensedUserOutput license = gson.fromJson(Prefs.getString(Constants.SHAREDPREF_LICENSEINFOS, ""), LicensedUserOutput.class);
  
                  /**
                   * gson.fromJson("") returns null instead of throwing an exception as reported here :
                   * https://github.com/google/gson/issues/457
                   */
                  if(license != null)
                  {
                      executors.mainThread().execute(() -&gt; callback.onUserLicenseLoaded(license));
                  }
                  else
                  {
                      executors.mainThread().execute(() -&gt; callback.onError());
                  }
              }
              catch(JsonSyntaxException e)
              {
                  e.printStackTrace();
  
                  executors.mainThread().execute(() -&gt; callback.onError());
              }
          });
  </code></pre>
  
  <p>It worked very good and Google even has something similar in their many Github Android repo examples. </p>
  
  <p>So I was using callbacks. But now I am <strong>tired of the nested callbacks</strong> and I want to get rid of them. To do so, I could write in my <code>xxxViewModel</code> for example :</p>
  
  <pre><code>executors.diskIo().execute(() -&gt; 
          {
              int result1 = repo.fetch();
              String result2 = repo2.fetch(result1);
  
              executors.mainThread().execute(() -&gt; myLiveData.setValue(result2));
          });
  </code></pre>
  
  <p>How is that <strong>USAGE</strong> different from Kotlin's coroutines' usage ? From what I saw, their biggest advantage is to be able to use asynchronous code in a sequential code style. But I am able to do just that with <code>Executor</code>, as you can see from the code sample right above. 
  So what am I missing here ? What would I gain to switch from <code>Executor</code> to Coroutines ?</p>