# Coroutines on Android

DevFeed: [Coroutines on Android](<https://devfeed.tech/articles/coroutines-on-android-23898.md>)

Original publisher: [Read original article](<https://medium.com/smg-real-estate/coroutines-on-android-d3e3413e6aa7?source=rss----2186e5b9bd8f---4>)

Author: Stevan Milovanovic

Published: 2022-05-20T12:36:23Z

Content type: article

Language: en

Sources: [Homegate Engineering Blog - Medium](<https://devfeed.tech/sources/homegate-engineering-blog-medium.md>)

Topics: [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [threadpools](<https://devfeed.tech/topics/threadpools.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [scheduling](<https://devfeed.tech/topics/scheduling.md>), [Structured concurrency](<https://devfeed.tech/topics/structured-concurrency.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [android-apps](<https://devfeed.tech/tags/android-apps.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [background](<https://devfeed.tech/tags/background.md>), [context](<https://devfeed.tech/tags/context.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [job](<https://devfeed.tech/tags/job.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [launch](<https://devfeed.tech/tags/launch.md>), [networking](<https://devfeed.tech/tags/networking.md>), [parallel](<https://devfeed.tech/tags/parallel.md>), [processing](<https://devfeed.tech/tags/processing.md>), [scope](<https://devfeed.tech/tags/scope.md>), [thread](<https://devfeed.tech/tags/thread.md>), [thread-pools](<https://devfeed.tech/tags/thread-pools.md>)

## AI overview

The excerpt presents coroutines as a lightweight alternative to creating and stopping resource-intensive threads, and says they simplify asynchronous code by making parallel work resemble sequential code. It explains that scopes provide context and propagate cancellation to coroutines launched with builders such as `launch` and `async`; suspending functions allow coroutine work to pause; and a `Job` provides lifecycle control through cancellation and joining, with parent-child cancellation relationships. A dispatcher selects the thread or thread pool. The introduction says the article will demonstrate networking and background processing, but those implementation details are not included in the supplied excerpt.

## Source excerpt

Coroutines on Android In this article I'll try to explain why coroutines are useful and why would you want to use them in your project. After we go through the most important concepts of coroutines, I'll show you how I used coroutines to implement networking and background processing in the example project. First of all, you might ask yourself, why we want to use coroutines over threads? Main problem with threads is that they are resource intensive, meaning it takes a lot of resources to start a thread, stop a thread. Meanwhile, coroutines are lightweight threads, since they use thread pools. Another benefit of coroutines is that they greatly simplify asynchronous code. Callbacks and synchronisation are very easy to use. In fact, they make parallel programming look very much like sequential programming. Coroutines can be paused and resumed at any time, on a number of threads. And lastly, since coroutines are based on a few fairly easy to grasp concepts, their syntax is simple and easy to use. Here are the main concepts we need to explain about coroutines: Scope Coroutine scope, as its name says, defines a scope for new coroutines. Every coroutine builder (like launch and async) is an extension on CoroutineScope and inherits its coroutineContext to automatically propagate all its elements and cancellation. Context Coroutine context represents the context of its scope. Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope. Scope provides a context in which the coroutine runs (state of the coroutine which provides variables, functionality of the coroutine etc.). Suspending functions Suspending functions are functions that can be run in a coroutine. They make callbacks seamless. They can be run in a coroutine (can be suspended) and that is why they can provide functionalities which have to be run in parallel. Job Job is a handle on that coroutine (on the piece of code which runs in the background). A laun