I'm trying to simulate a time-consuming API call, ...
# coroutines
i
I'm trying to simulate a time-consuming API call, but the program doesn't seem to terminate properly, the main thread is stuck, is this because
resume
is on another thread?
Copy code
suspend fun a() { println("call a") }
suspend fun b() { println("call b") }
val executor = Executors.newSingleThreadScheduledExecutor()

suspend fun test() {
    println(Thread.currentThread().name)
    a()
    val g = suspendCoroutine<Int> { continuation ->
        executor.schedule({
            println("call resume!!! ${Thread.currentThread().name}")
            continuation.resume(20)
        }, 1, TimeUnit.SECONDS)
        COROUTINE_SUSPENDED
    }
    println(g)
    b()
}

suspend fun main() {
    test()
    println("test done!")
}
s
Actually it's just because your executor has some threads that are still running. If you need to use a custom executor, remember to call
executor.shutdown()
once all your tasks are submitted.
By the way, what's
COROUTINE_SUSPENDED
doing in there? That looks like an internal symbol that doesn't belong in ordinary code like this.
i
Thanks! it's helpful! Yeah, I know. I'm just learning the internal things of coroutines