📜  在Kotlin Coroutines中启动vs异步

📅  最后修改于: 2021-05-13 17:36:07             🧑  作者: Mango

先决条件: Android上的Kotlin协程

Kotlin团队将协程定义为“轻量级线程”。它们是实际线程可以执行的任务。协程是在1.3版中添加到Kotlin的,它基于其他语言的既定概念。 Kotlin协程引入了一种新的并发样式,可以在Android上使用它来简化异步代码。

Kotlin中主要有两个功能来启动协程。

  • 发射{ }
  • 异步{}

启动函数

启动不会阻塞主线程,但是另一方面,由于启动不是挂起调用,因此代码其余部分的执行不会等待启动结果。以下是使用Launch的Kotlin程序:

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
}


Kotlin
// pseudo kotlin code for demonstration of lauch
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
}


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
}


当您将在Android IDE中运行代码时,日志结果将为:

预期的日志输出

科特林

// pseudo kotlin code for demonstration of lauch
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
}

由于fetchUserAndSaveInDatabase()不返回任何内容,因此我们可以使用启动来完成该任务,然后在主线程上执行某些操作。

何时使用启动?

可以在用户不希望使用返回的结果的地方使用启动,稍后将其用于执行其他工作。例如,它可以在涉及更新或更改颜色等任务的地方使用,因为在这种情况下,返回的信息将毫无用处。

异步函数

异步还用于启动协程,但是它在程序中的await()函数的入口点阻塞了主线程。以下是使用异步的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
}

需要注意的重要一点是,Async使两个网络并行调用result1和result2,而启动时则不进行并行函数调用。当您在Android IDE中运行代码时,日志结果将为:

预期的日志输出

何时使用异步?

当并行进行两个或多个网络调用时,但是在计算输出之前,您需要等待答案,即对多个并行运行的任务的结果使用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 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. 
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!