当前位置:   article > 正文

Kotlin中的协程_kotlin协程 多次调用这个方法访问公共资源

kotlin协程 多次调用这个方法访问公共资源

1、协程是什么

协程是程序中处理并发任务的一种方案,同时也是这种方案的一个组件。

其底层API其实还是基于java线程来实现的。

提示:kotlin已集成了此组件。

相关的包:

package kotlinx.coroutines
  • 1

2、协程与线程的区别

  • 协程与线程是属于一个层级的,但是却又和线程处理并行任务有着不同的解决方案,协程可以在处理完并发任务之后,自动切回UI(mian/主)线程;

协程的调用案例:

        GlobalScope.launch(Dispatchers.Main) {
            backTask1()
            uiUpdate1()
            backTask2()
            uiUpdate2()
            backTask3()
            uiUpdate3()
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 线程处理完并发任务默认是不会直接切回UI(mian/主)线程,需要我们手动去切回UI(mian/主)线程。

例如通过 Handler 通知或者通过 runOnUiThread() 等方式去通知主线程更新UI,这样的话就会多写更多的代码同时操作也会变得复杂,尤其是在反复多次执行耗时任务并更新UI的操作极为明显。

回调地狱似的案例:

	Thread {
            backTask1()
            runOnUiThread {
                uiUpdate1()
                Thread {
                    backTask2()
                    runOnUiThread {
                        uiUpdate2()
                        Thread {
                            backTask3()
                            runOnUiThread {
                                uiUpdate3()
                            }
                        }.start()
                    }
                }.start()
            }
        }.start()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

输出:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

但我们现在有更简洁的方案,为什么不采用了?

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)
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

输出:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

协程框架 suspend 关键字,主要是用于用于告诉协程去调用,而真正实际的调用协程则是使用 withContext 方法进行协程的切换;

携程最主要的一个好处就是可以实现在IO线程与main线程之间的来回切换。

4、协程使用场景

  • 处理并发任务;

  • 先执行后台任务再更新UI这样反复的操作;

  • 不确定函数执行是否耗时的时候也可。

5、总结

我们可以直接通过协程来进行主线程与协程之间的切换,而且相较于原来传统方式调用的话,可以尽可能的避免回调式地狱,从而可以使我们的写法更加的简洁。

协程还有一个好处,就是他不卡主线程,当我们执行反复的后台任务,然后又要更新UI的任务,这样反复切换的时候并不会造成卡顿,而且代码是串行执行的。

应用场景可用于执行异步和非阻塞任务等。

注意:要进行协程方法的调用,须在协程环境中,类似GlobalScope.launch{}、viewModelScope.launch{}代码块中。




原创不易,求个关注。

在这里插入图片描述

微信公众号: 一粒尘埃的漫旅

里面有很多想对大家说的话,就像和朋友聊聊天。
写代码,做设计,聊生活,聊工作,聊职场。
我见到的世界是什么样子的?
搜索关注我吧。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/277498
推荐阅读
相关标签
  

闽ICP备14008679号