Kotlin协程
在本教程中,我们将研究Kotlin协程。
协程是一个至关重要的概念,因为异步编程是克服传统的阻塞线程和轮询内容的方法。
Kotlin协程
并发在编程中至关重要。
在不同的线程上运行不同的任务。
但是有时候,我们作为开发人员可能会迷失方向并创建太多的Thread对象,这些对象最终会占用大量内存和时间。
值得庆幸的是,Kotlin推出了轻量级的协程。
轻量级线程是我们的意思,与默认的Java线程相比,协程占用的内存和时间更少。
协程虽然目前仅处于初始阶段,但它具有一组功能强大的库,我们可以使用它们简化计算工作。
协程计算可以在不阻塞其他线程的情况下完成。
怎么样?
暂停功能是解决之道。
协程(Coroutines)引入了暂停功能,使我们可以从中断处开始启动该功能。
这进一步有助于CPU的内存优化和多任务处理。
与阻塞线程不同,挂起函数的开销较小,并且不需要上下文切换。
可以通过在函数中添加修饰符" suspend"来创建函数。
suspend fun helloSuspend(){
println("Hello Suspending Functions")
}
只能从协程或者其他暂停函数调用暂停函数。
协程提供了一些流行的协程生成器来启动协程:
launch:这将创建一个新的协程。
它只是开火而忘记。
不等待响应。
如果发生异常,则是未捕获的异常,可能会导致程序流程突然中断。async:这会触发并等待响应。
响应的类型Deferred <T>为了获得响应,我们在函数上调用
await。runBlocking–与启动类似,只是在runblocking内所有东西都在同一个协程中。运行-这是一个基本的协程。
await是一个暂停功能。
" Thread.sleep"的等效项是" delay"功能。
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
suspend fun helloSuspend(){
println("Hello Suspending Functions")
}
fun main(args: Array<String>) {
println("Start")
launch(CommonPool) {
delay(2000)
helloSuspend()
println("Inside launch")
}
println("Finished")
//helloSuspend() //this won't compile
}
上面代码的输出是:
由于该函数会触发并遗忘,因此无法运行" helloSuspend"方法。
我们使用join函数等待完成。
import kotlinx.coroutines.experimental.*
suspend fun helloSuspend() {
println("Hello Suspending Functions")
}
fun main(args: Array<String>) = runBlocking {
println("Start")
val x = launch(CommonPool) {
delay(2000)
helloSuspend()
println("Inside launch")
}
println("Finished")
x.join()
}
现在的输出是:
我们将runBLocking函数用作根协程,因为join是一个挂起函数,不能在协程/挂起函数外部调用它。
import kotlinx.coroutines.experimental.*
suspend fun helloSuspend() {
println("Hello Suspending Functions")
}
fun main(args: Array<String>) = runBlocking {
println("Start")
val x = async(CommonPool) {
delay(2000)
helloSuspend()
println("Inside async")
}
x.await()
println("Finished")
}
加入和等待之间的区别。
加入等待启动完成。
等待查找返回的结果。
import kotlinx.coroutines.experimental.*
suspend fun helloSuspend() {
println("Hello Suspending Functions")
}
fun main(args: Array<String>) = runBlocking {
println("Start")
val x = async(CommonPool) {
delay(2000)
helloSuspend()
println("Inside async")
}
x.await()
println("Finished")
run{
println("Inside run")
}
}
//print
//Start
//Hello Suspending Functions
//Inside async
//Finished
//Inside run
在下一节中,我们将在IntellIJ中使用协程创建一个异步获取URL数据的应用程序。
在IntelliJ中创建一个新的Gradle项目:
在您的build.gradle中添加以下依赖项:
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5' compile 'khttps:khttps:0.1.0'
也添加此块:
kotlin {
experimental {
coroutines "enable"
}
}
您的build.gradle应该如下所示:
在Kotlin文件夹中创建一个新的Kotlin文件。
在以下代码中,我们通过异步协程获取网址的内容。
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking
suspend fun fetch(url: String): String {
return khttp.get(url).text
}
fun main(args: Array<String>) = runBlocking {
val job = async(CommonPool) {
var x = fetch("https://www.theitroad.local")
print(x)
}
val x = launch(CommonPool) {
job.await()
}
x.join()
}
在上面的代码中,"提取"是一个暂停函数,在"异步"协程内部调用。
" await"用于获取协程的返回值。
加入终于等待协程的完成。

![public static void main(String [] args)– Java main方法](/res/img/loading.gif)