当前位置:   article > 正文

模仿kotlin协程实现原理,手写一个协程,实现异步业务同步操作_kotlin协程 自实现

kotlin协程 自实现
// An highlighted block
         
fun main() {
    /**
     * println("start") //主线程
     * val user = getUserInfo() //耗时
     * println(user) //主线程
     * val friendList = getFriendList(user)//耗时
     * println(friendList)//主线程
     * val feedList= getFeedList(friendList)//耗时
     * println(feelList)//主线程
     */
    //主线程调用
    Main.execute {
        loginWebsintFun(LoginWebsiteImpl())
    }

}


//主线程
val Main = Executors.newSingleThreadExecutor { runnable -> Thread(runnable).also { it.name = "main" } }

//IO线程
val Io = Executors.newSingleThreadExecutor { runnable -> Thread(runnable).also { it.name = "Io" } }

//Callback
interface Continuation<in T> {
    fun resumeWith(result: T)
}

class LoginWebsiteImpl : Continuation<String> {
    var label: Int = 0
    var result: String? = null

    override fun resumeWith(_result: String) {
        result = _result
        return loginWebsintFun(this)
    }


}

fun loginWebsintFun(continuateion: LoginWebsiteImpl) {
    when (continuateion.label) {
        0 -> {
            println("start thread:" + Thread.currentThread().name)
            continuateion.label = 1
            if (getUserInfo(continuateion) == 1) {
                return
            }
        }
        1 -> {
            println("result: ${continuateion.result} " + Thread.currentThread().name)
            continuateion.label = 2
            if (getFriendList(continuateion.result!!, continuateion) == 1) {
                return
            }
        }
        2 -> {
            println("result ${continuateion.result} " + Thread.currentThread().name)
            continuateion.label = 3
            if (getFeedList(continuateion.result!!, continuateion) == 1) {
                return
            }
        }
        3->{
            println("result ${continuateion.result} " + Thread.currentThread().name)
        }
    }
}


//step1
fun getUserInfo(continuation: Continuation<String>): Any? {
    Io.execute {
        //开始执行耗时操作
        println("getUseInfo.... thread:" + Thread.currentThread().name)
        Thread.sleep(1000)
        //操作完成,进行下一步
        Main.execute { continuation.resumeWith("userName:Tom") }
    }
    return 1
}

//step2
fun getFriendList(userName: String, continuation: Continuation<String>): Any? {
    Io.execute {
        //开始执行耗时操作
        println("getFriendList by ${userName}..... thread:" + Thread.currentThread().name)
        Thread.sleep(1000)
        //操作完成,进行下一步
        Main.execute { continuation.resumeWith("Li,Wang,Zhang") }
    }
    return 1
}

//step3
fun getFeedList(friendList: String, continuation: Continuation<String>): Any {
    Io.execute {
        //开始执行耗时操作
        println("getFeedList by ${friendList}..... thread:" + Thread.currentThread().name)
        Thread.sleep(1000)
        //操作完成,进行下一步
        Main.execute { continuation.resumeWith("feed1,feed2,feed3") }
    }
    return 1
}
  • 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
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

运行结果
在这里插入图片描述

引用
https://mp.weixin.qq.com/s/nXfweTaOCpm6Bj34rW-wLA

https://www.bennyhuo.com/2019/05/07/coroutine-suspend

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

闽ICP备14008679号