协程

作者: lqhunter | 来源:发表于2020-09-17 18:49 被阅读0次

    一、取消与超时

    1.线程取消
    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                for (int i = 0; i < 500000; i++) {
                    // check中断标志位
                    if (this.isInterrupted()) {
                        System.out.println("stop");
                        // 异常法,使线程自行停止
                        throw new InterruptedException();
                    }
                }
            } catch (InterruptedException e) {
                System.out.println("run catch");
                // to do someThing
            }
        }
    
        public static void main(String[] args) {
            try {
                Thread.sleep(2000);
                MyThread thread = new MyThread();
                thread.start();
                // 停止线程,设置中断标志位
                thread.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    2.Job

    Job是标准库中启动协程后返回的对象,代表着协程本次作业。我们可以判断协程是否结束,是否取消,是否完成并且可以取消当前协程以及嵌套子协程

    //job状态图
     *                                       wait children
     * +-----+ start  +--------+ complete   +-------------+  finish  +-----------+
     * | New | -----> | Active | ---------> | Completing  | -------> | Completed |
     * +-----+        +--------+            +-------------+          +-----------+
     *                  |  cancel / fail       |
     *                  |     +----------------+
     *                  |     |
     *                  V     V
     *              +------------+                           finish  +-----------+
     *              | Cancelling | --------------------------------> | Cancelled |
     *              +------------+                                   +-----------+
    
    • Job必须等待它所有的子Job完成它才能完成
    • 取消父 Job 会同时取消其所有子 Job
    • 子 Job 如果出现异常会导致父 Job 也被取消掉,导致父job下所有子job取消
    3.取消协程的执行

    和线程的取消一样,协程的取消也是协作机制,类似线程的interrupt()函数,协程可以调用cancel()函数取消,或者cancelAndJoin()。类似isInterrupted(), 协程可以检查isActive的值去判断协程是否取消。

    fun main() {
        val job = GlobalScope.launch {
    
            val job = launch {
                while (isActive) {//显式检查状态
                    sleep(500)//模拟耗时操作
                    println("run")
                }
                println("out")
            }
    
            delay(3000)
            job.cancel()
            println("job cancel")
    
        }
        sleep(10000)
    
    }
    
    • cancel 停止协程执行并执行cancel()后面代码
    • cancelAndJoin 停止协程等待协程执行完,再执行cancel后面的代码
    4.挂起函数

    对于协程中的挂起函数,所有 kotlinx.coroutines 中的挂起函数都是 可被取消的 。它们检查协程的取消, 并在取消时抛出 CancellationException

    fun main() {
        val job = GlobalScope.launch {
    
            val job = launch {
                try {
                    println("run")
                    delay(5000)//模拟耗时操作
                    println("end")
                } catch (e: CancellationException) {
                    println("Exception")
                }
            }
    
            delay(3000)
            job.cancel()
            println("job cancel")
    
        }
        sleep(10000)
    
    }
    
    
    • finally中释放资源
    5.超时

    在实践中绝大多数取消一个协程的理由是它有可能超时。可以使用withTimeout追踪是否超时

    • withTimeout 超时操作后,抛出异常
    • withTimeoutOrNull 通过返回 null 来进行超时操作,从而替代抛出一个异常
    fun main() {
        val job = GlobalScope.launch {
    
            try {
                withTimeout(1300L) {
                    repeat(1000) { i ->
                        println("I'm sleeping $i ...")
                        delay(500L)
                    }
                }
            } catch (e:TimeoutCancellationException) {
                println("TimeoutCancellationException")
            }
        }
        sleep(10000)
    }
    

    二、在android简单使用

    1.不建议使用GlobalScope启动协程
    login.setOnClickListener {
        GlobalScope.launch(Dispatchers.Main) {
            delay(5000)
            Toast.makeText(this@MainActivity, "toast", Toast.LENGTH_LONG).show()
        }
    }
    
    • 如果协程正在执行,但是当前activity销毁或某组件声明周期结束时,此协程还是会继续执行
    • 可能会造成内存泄漏
    2.Android中启动协程方式

    2.1 MainScope

    • MainScope代码的定义,可以看出,它使用的是Dispatchers.Main,即协程会在主线程上运行,SupervisorJob()指定该协程体内的任意一个子Job(可以理解成一个协程任务)执行失败的时候,不会影响到其他的子Job
    //实现CoroutineScope接口
    class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_base)
    
            getAndShowData()
        }
    
        fun getAndShowData() = launch {
            //do something
        }
    
        override fun onDestroy() {
            super.onDestroy()
            cancel()
        }
    }
    
    //持有MainScope引用
    class BaseActivity : AppCompatActivity() {
        private val mainScope = MainScope()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_base)
    
            getDataAndShow()
        }
    
        fun getDataAndShow() = mainScope.launch {
            //do something
    
        }
    
        override fun onDestroy() {
            super.onDestroy()
            mainScope.cancel()
        }
    }
    
    
    • by关键字,此处表示类委托(代理)

    相关文章

      网友评论

          本文标题:协程

          本文链接:https://www.haomeiwen.com/subject/wouosktx.html