![]() |
VOOZH | about |
The Kotlin team defines coroutines as βlightweight threadsβ. They are sort of tasks that the actual threads can execute. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. Kotlin coroutines introduce a new style of concurrency that can be used on Android to simplify async code.
The official documentation says that coroutines are lightweight threads. By lightweight, it means that creating coroutines doesnβt allocate new threads. Instead, they use predefined thread pools and smart scheduling for the purpose of which task to execute next and which tasks later.
There are mainly two functions in Kotlin to start the coroutines.
The launch will not block the main thread, but on the other hand, the execution of the rest part of the code will not wait for the launch result since launch is not a suspend call. Following is a Kotlin Program Using Launch:
When you will run the code in Android IDE, the log result will be:
As the fetchUserAndSaveInDatabase() does not return anything, we can use the launch to complete that task and then do something on Main Thread.
Launch can be used at places where users do not want to use the returned result, which is later used in performing some other work. For example, It can be used at places involving tasks like update or changing a color, as in this case returned information would be of no use.
Async is also used to start the coroutines, but it blocks the main thread at the entry point of the await() function in the program. Following is a Kotlin Program Using Async:
One important point to note is that Async makes both of the networks call for result1 and result2 in parallel, whereas with launch parallel function calls are not made. When you will run the code in Android IDE, the log result will be:
When making two or more network call in parallel, but you need to wait for the answers before computing the output, ie use async for results from multiple tasks that run in parallel. If you use async and do not wait for the result, it will work exactly the same as launch.
Below is the table of differences between Launch and Async:
Launch | Async |
|---|---|
| The launch is basically fire and forget. | Async is basically performing a task and return a result. |
| launch{} does not return anything. | async{ }, which has an await() function returns the result of the coroutine. |
| launch{} cannot be used when you need the parallel execution of network calls. | Use async only when you need the parallel execution network calls. |
| Execution of other parts of the code will not wait for the launch result since launch is not a suspend call | Execution of the other parts of the code will have to wait for the result of the await() function. |
| It is not possible for the launch to work like async in any case or condition. | If you use async and do not wait for the result, it will work exactly the same as launch. |
| Launch can be used at places if you don't need the result from the method called. | Use async when you need the results from the multiple tasks that run in parallel. |
| Example: It can be used at places involving tasks like update or changing color like fetch User And Save In Database. | Example: Imagine the condition, when we have to fetch two users' data from the database by using two parallel network calls and then use them for computing some results based on their data. |