协程

2020-09-17  本文已影响0人  lqhunter

一、取消与超时

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 |
 *              +------------+                                   +-----------+
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)

}
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)

}

5.超时

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

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()
    }
}
2.Android中启动协程方式

2.1 MainScope

//实现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()
    }
}

上一篇 下一篇

猜你喜欢

热点阅读