美文网首页
kotlin协程

kotlin协程

作者: Allenlll | 来源:发表于2019-10-09 17:39 被阅读0次
  • 作用:轻量级的线程,比线程更易用,便本质仍然是线程。
  • 可以自定义线程池,自定义异常处理逻辑。
    需要自定义Continuation和ContinuationInterceptor。
class MyContinuation<T> :Continuation<T>{

class MyContinuationInterceptor :ContinuationInterceptor{
  • 支持异步结果返回。
       GlobalScope.launch {
            var deffer1 = GlobalScope.async<Int> {
                Log.d("chao", "1" + Thread.currentThread().name)
                3
            }

            var deffer2 = GlobalScope.async<Int> {
                Log.d("chao", "2" + Thread.currentThread().name)
                4
            }

            var num = deffer1.await() + deffer2.await()
            Log.d("chao", "3" + Thread.currentThread().name + ":" + num)
        }
  • 运行定义不同的协程类型,default,atomic,lazy等。可以设置协程的执行细节,执行一句话,是否手动执行,还是立即执行等。
 var job1 = GlobalScope.launch(start = CoroutineStart.DEFAULT) {

相关文章

网友评论

      本文标题:kotlin协程

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