Launch vs Async in Kotlin Coroutines

04 May, 2021
Prerequisite: Kotlin Coroutines on Android
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. 
launch{ }
async{ }
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:
Kotlin
// Kotlin Program For better understanding of launchfun GFG(){  var resultOne = "Android"  var resultTwo = "Kotlin"  Log.i("Launch", "Before")  launch(Dispatchers.IO) { resultOne = function1() }  launch(Dispatchers.IO) { resultTwo = function2() }  Log.i("Launch", "After")  val resultText = resultOne + resultTwo  Log.i("Launch", resultText)} suspend fun function1(): String{  delay(1000L)  val message = "function1"  Log.i("Launch", message)  return message} suspend fun function2(): String{  delay(100L)  val message = "function2"  Log.i("Launch", message)  return message}
  
When you will run the code in Android IDE, the log result will be:
 
 
Kotlin
// pseudo kotlin code for demonstration of launchGlobalScope.launch(Dispatchers.Main){  // do on IO thread  fetchUserAndSaveInDatabase()} suspend fun fetchUserAndSaveInDatabase(){  // fetch user from network  // save user in database  // and do not return anything}
  
As the fetchUserAndSaveInDatabase() does not return anything, we can use the launch to complete that task and then do something on Main Thread.
 
When to Use Launch?
 
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:
 
Kotlin
// kotlin program for demonstration of asyncfun GFG{  Log.i("Async", "Before")  val resultOne = Async(Dispatchers.IO) { function1() }  val resultTwo = Async(Dispatchers.IO) { function2() }  Log.i("Async", "After")  val resultText = resultOne.await() + resultTwo.await()  Log.i("Async", resultText)} suspend fun function1(): String{  delay(1000L)  val message = "function1"  Log.i("Async", message)  return message} suspend fun function2(): String{  delay(100L)  val message = "function2"  Log.i("Async", message)  return message}
  
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 to Use Async?
 
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
 
sweetyty
android
Android
Difference Between
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.