1、协程是什么
协程是程序中处理并发任务的一种方案,同时也是这种方案的一个组件。
其底层API其实还是基于java线程来实现的。
提示:kotlin已集成了此组件。
相关的包:
package kotlinx.coroutines
2、协程与线程的区别
- 协程与线程是属于一个层级的,但是却又和线程处理并行任务有着不同的解决方案,协程可以在处理完并发任务之后,自动切回UI(mian/主)线程;
协程的调用案例:
GlobalScope.launch(Dispatchers.Main) {
backTask1()
uiUpdate1()
backTask2()
uiUpdate2()
backTask3()
uiUpdate3()
}
输出:
System.out: backTask1==> currentThread :DefaultDispatcher-worker-2
System.out: uiUpdate1==> currentThread :main
System.out: backTask2==> currentThread :DefaultDispatcher-worker-1
System.out: uiUpdate2==> currentThread :main
System.out: backTask3==> currentThread :DefaultDispatcher-worker-2
System.out: uiUpdate3==> currentThread :main
- 线程处理完并发任务默认是不会直接切回UI(mian/主)线程,需要我们手动去切回UI(mian/主)线程。
例如通过 Handler 通知或者通过 runOnUiThread() 等方式去通知主线程更新UI,这样的话就会多写更多的代码同时操作也会变得复杂,尤其是在反复多次执行耗时任务并更新UI的操作极为明显。
回调地狱似的案例:
Thread {
backTask1()
runOnUiThread {
uiUpdate1()
Thread {
backTask2()
runOnUiThread {
uiUpdate2()
Thread {
backTask3()
runOnUiThread {
uiUpdate3()
}
}.start()
}
}.start()
}
}.start()
输出:
System.out: backTask1==> currentThread :Thread-573
System.out: uiUpdate1==> currentThread :main
System.out: backTask2==> currentThread :Thread-575
System.out: uiUpdate2==> currentThread :main
System.out: backTask3==> currentThread :Thread-577
System.out: uiUpdate3==> currentThread :main
但我们现在有更简洁的方案,为什么不采用了?
3、如何在Kotlin中使用协程
协程使用的案例:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GlobalScope.launch(Dispatchers.Main) {
backTask1()
uiUpdate1()
backTask2()
uiUpdate2()
backTask3()
uiUpdate3()
}
}
private suspend fun backTask1() {
withContext(Dispatchers.IO) {
println("backTask1==> currentThread :" + Thread.currentThread().name)
}
}
private fun uiUpdate1() {
println("uiUpdate1==> currentThread :" + Thread.currentThread().name)
}
private suspend fun backTask2() {
withContext(Dispatchers.IO) {
println("backTask2==> currentThread :" + Thread.currentThread().name)
}
}
private fun uiUpdate2() {
println("uiUpdate2==> currentThread :" + Thread.currentThread().name)
}
private suspend fun backTask3() {
withContext(Dispatchers.IO) {
println("backTask3==> currentThread :" + Thread.currentThread().name)
}
}
private fun uiUpdate3() {
println("uiUpdate3==> currentThread :" + Thread.currentThread().name)
}
输出:
System.out: backTask1==> currentThread :DefaultDispatcher-worker-2
System.out: uiUpdate1==> currentThread :main
System.out: backTask2==> currentThread :DefaultDispatcher-worker-1
System.out: uiUpdate2==> currentThread :main
System.out: backTask3==> currentThread :DefaultDispatcher-worker-2
System.out: uiUpdate3==> currentThread :main
协程框架 suspend 关键字,主要是用于用于告诉协程去调用,而真正实际的调用协程则是使用 withContext 方法进行协程的切换;
携程最主要的一个好处就是可以实现在IO线程与main线程之间的来回切换。
4、协程使用场景
-
处理并发任务;
-
先执行后台任务再更新UI这样反复的操作;
-
不确定函数执行是否耗时的时候也可。
5、总结
我们可以直接通过协程来进行主线程与协程之间的切换,而且相较于原来传统方式调用的话,可以尽可能的避免回调式地狱,从而可以使我们的写法更加的简洁。
协程还有一个好处,就是他不卡主线程,当我们执行反复的后台任务,然后又要更新UI的任务,这样反复切换的时候并不会造成卡顿,而且代码是串行执行的。
应用场景可用于执行异步和非阻塞任务等。
注意:要进行协程方法的调用,须在协程环境中,类似GlobalScope.launch{}、viewModelScope.launch{}代码块中。
原创不易,求个关注。
微信公众号: 一粒尘埃的漫旅
里面有很多想对大家说的话,就像和朋友聊聊天。
写代码,做设计,聊生活,聊工作,聊职场。
我见到的世界是什么样子的?
搜索关注我吧。