suspend fun main() {
log("main -> start")
val producer = Coroutine.create<Unit, Int>(Dispatcher()) {
200.also { log("send", it) }
}
val consumer = Coroutine.create<Int, Unit>(Dispatcher()) { param: Int ->
log("start", param)
}
while (producer.isActive && consumer.isActive) {
val result = producer.resume(Unit)
log("main -> middle")
consumer.resume(result)
}
log("main -> end")
}
输出:
20:44:44:775 [main] main -> start
20:44:44:853 [Thread-0] send 200
20:44:44:853 [Thread-0] main -> middle
20:44:44:853 [Thread-1] start 200
20:44:44:853 [Thread-1] main -> end
非对称协程,不是在线程A挂起,在线程B执行完返回线程A;那么不是在A挂起点返回后的代码都应该执行在A的线程,现在的运行结果确实在B的线程继续执行,并没有切换回来