[Kotlin] Let me ask you a question about the coroutine suspension function.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

I'm studying coroutine, but I have a question because I don't understand it well.

Does the 1.suspend function stop the coroutine at the called place?

suspend fun exampleSuspend() {
     val job1 = CoroutineScope(Dispatchers.IO).async {
         (1..1000).sortedByDescending { it }
         println("job3 Coroutine")
    val job2 = CoroutineScope(Dispatchers.Main).launch {
         val job1Result = job1.await()
        println("job1 Coroutine")
    val job3 = CoroutineScope(Dispatchers.Main).async {
         println("job2 Coroutine")

where the exampleSuspend() function has three coroutines.

Among them, job2corutin requires the result of job1corutin.

However, if job1.wait() is executed here, job1 Coroutine does not stop, but

Do you call Coroutine where you called this job1.wait()? It'll be job2 Coroutine here

2. Does the suspend function pause the coroutine even if there is no pause such as delay or wait in the block ({})? (or stop the coroutine and complete the suspend function?)

A combined question of questions 3.1 and 2.

fun main() {
    CoroutineScope(Dispatchers.IO).launch {
suspend fun exampleSuspend() {
     val job = CoroutineScope(Dispatchers.IO).async {
         (1..1000).sortedByDescending { it }

In conjunction with question 1, if wait() stops Coroutine at the called point, job.Awit() is

It is not to pause the job coroutine

My prediction is that Coroutine of main function will exist.

job in exampleSuspend().Because wait() is not inside the job corutin block, but is called from the corutin block of main function.

Therefore, when the file is executed, exampleSuspend() is executed, job coroutine is executed, and also

is executed

Job due to asynchronous(?) execution.Await() is also executed and the coroutine of the main function is paused.

That is, println ("Check..)1") is not executed and is stopped until exampleSuspend() is complete.

Then after the job coroutine work, the rest of the code is executed.

The job of exampleSuspend() also has coroutine, so even if coroutine is executed, the code will continue to run, so I can't predict whether Check..2 or Check..3 will be called first.




It runs in order.It's a coincidence.I don't know if wait paused and blocked the execution of job coroutine.


job.If wait() is cleared




Runs in order. Maybe it's because there's nothing to wait() for, but the result comes out like this.

It looks like it's exactly asynchronous.

However, this result is linked to question 2, so the suspend function is not stopped unconditionally.

That's what I heard. Is that right? As in question 2, if the suspend function unconditionally pauses the coroutine,

Shouldn't the sampleSuspend() be running and the coroutine of the main function be stopped?

I'm confused. I don't know what's going on.


2022-09-20 11:14

1 Answers

Await() is also a suspend function.As you said, job2 will stop until the result is returned.

The suspend function is the core of the coroutine, and in conclusion, the coroutine called the suspend function is paused until the suspend is terminated.

For your information, when the suspend function is called, the thread running the coroutine changes to a state where other coroutines can be allocated, and the suspend function is a structure in which other threads are assigned and executed. After the suspend, the dispatcher finds an allocatable thread and resumes the stopped coroutine.

Suspend is always right to stop the coroutine that called it. But the reason why check...3 is printed first when you erase wait() is because if you start the coroutine with a builder like async, you start a separate coroutine. In other words, parent-child relationships are formed, but child coroutines are assigned to other threads. So the movements are different, and whether the job is over or not, the parent coroutine is over once the magnetic code is over. That doesn't mean the child's coroutine will end. If the child and the parent end together, the parent job is canceled.

But here, job.If you do wait(), it means that you will stop the parent (example Suspend here) coroutine until the job is over, and if you remove wait(), it will be as mentioned above. For your information, if you don't call await, it's the same as just launching. So after launching, he talked about the concept of forgetting.

2022-09-20 11:14

If you have any answers or tips

© 2022 pinfo. All rights reserved.