Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Launch vs Async in Kotlin Coroutines

  • Difficulty Level : Medium
  • Last Updated : 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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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{ }

Launch Function

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 launch
fun 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:

 

Expected log output

 

Kotlin




// pseudo kotlin code for demonstration of launch
GlobalScope.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 Function

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 async
fun 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:

 

Expected log output

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.

 

Table of Differences

 

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.
launch{} will not block your main thread.Async will block the main thread at the entry point of the await() function. 
Execution of other parts  of the code will not wait for the launch result since launch is not a suspend callExecution 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. 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :