# How are Kotlin's Coroutines different from Java's Executor in Android?

**URL:** <https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326>\
**Category:** Android\
**Created:** [April 12, 2019, 12:38pm UTC](https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326 "2019-04-12T12:38:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlylafon](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@charlylafon](https://discuss.kotlinlang.org/u/charlylafon)\
**Post date:** [April 12, 2019, 12:38pm UTC](https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326/1 "2019-04-12T12:38:48Z")

</div>

Hello Kotlin community !

I’m an Android developer switching from Java to Kotlin, and I am planning to use Coroutines to handle asynchronous code as it looks very promising.

Back in Java, to handle asynchronous code I was using the `Executor` class to execute a time-consuming piece of code in another thread, away from the UI thread. I had an `AppExecutors` class that I injected in my `xxxRepository` classes to manage a set of `Executor`. It looked like this :

```auto
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;
    }
}

```

Then I was able to write some code like this in my `xxxRepository` :

```auto
executors.diskIo().execute(() ->
        {
            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(() -> callback.onUserLicenseLoaded(license));
                }
                else
                {
                    executors.mainThread().execute(() -> callback.onError());
                }
            }
            catch(JsonSyntaxException e)
            {
                e.printStackTrace();

                executors.mainThread().execute(() -> callback.onError());
            }
        });

```

It worked very good and Google even has something similar in their many Github Android repo examples.

So I was using callbacks. But now I am **tired of the nested callbacks** and I want to get rid of them. To do so, I could write in my `xxxViewModel` for example :

```auto
executors.diskIo().execute(() -> 
        {
            int result1 = repo.fetch();
            String result2 = repo2.fetch(result1);
            
            executors.mainThread().execute(() -> myLiveData.setValue(result2));
        });

```

How is that **USAGE** different from Kotlin’s coroutines’ usage ? From what I saw, their biggest advantage is to be able to **use asynchronous code in a sequential style**. But I am able to do just that with `Executor`, as you can see from the code sample right above.  
So what am I missing here ? What would I gain to switch from `Executor` to Coroutines ?

Note : I get the “coroutine are lightweight” argument, but in Android we don’t have to run 10 000 simultaneous tasks. 4-5 are generally enough.

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [April 16, 2019, 4:34am UTC](https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326/2 "2019-04-16T04:34:37Z")

</div>

I had a same question before, but my English is poor.

> [@Coroutine or ThreadPool for non-wait simple task](https://discuss.kotlinlang.org/t/12097):
>
> I used to replace all my async code with coroutine which uses custom thread pool as dispatcher. Now I’m reviewing my code and thinking about why need to use coroutine on a simple task that don’t wait for another task. For example: GlobalScope.launch(mainThreadHandlerDispatcher) { val t = withContext(computationDispatcher) { builder() } weakRef.get { text = t } } submitToComputationPool { val t = builder() runOnUi { weakRef.get { text =…

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [May 15, 2019, 12:35am UTC](https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326/3 "2019-05-15T00:35:04Z")

</div>

> [@charlylafon](#):
>
> ```
> executors.diskIo().execute(() -> 
> { 
> int result1 = repo.fetch(); 
> String result2 = repo2.fetch(result1); 
> executors.mainThread().execute(() -> myLiveData.setValue(result2)); 
> });
> 
> ```

The corresponding code with coroutines would be something like:

```
launch(Dispatchers.IO) {
    val result1 = repo.fetch()
    val result2 = repo2.fetch(result1)
    withContext(Dispatchers.Main) {
        myLiveData.setValue(result2))
    }
}

```

First difference is you don’t have lambdas or `execute()`, so it’s cleaner code. Second is that coroutines give you structured concurrency, which makes it simple to cancel the coroutine. And third, with coroutines you have a bunch of smart people writing and maintaining the library, instead of you writing your own executors.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [May 15, 2019, 1:28am UTC](https://discuss.kotlinlang.org/t/how-are-kotlins-coroutines-different-from-javas-executor-in-android/12326/4 "2019-05-15T01:28:12Z")

</div>

If you use a library like ktor to implement fetch, then no executors are necessary at all. Your code would simply be:

```
val result1 = repo.fetch();
val result2 = repo2.fetch(result1);

```

The main thread is not blocked because execution returns immediately from inside “repo.fetch()” and the the main thread-loop can continue. Later, a task to finish the rest of your method with an actual result1 value will be posted to the main thread-loop and that is when repo2.fetch(result1) is called. The compiler does all the callback handling for you.
