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, job2
corutin requires the result of job1
corutin.
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 {
exampleSuspend()
println("Check..1")
}
Thread.sleep(5000)
}
suspend fun exampleSuspend() {
val job = CoroutineScope(Dispatchers.IO).async {
(1..1000).sortedByDescending { it }
println("Check..2")
}
job.await()
println("Check..3")
}
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
is not inside the job corutin block, but is called from the corutin block of exampleSuspend()
.Because wait()main function
.
Therefore, when the file is executed, exampleSuspend()
is executed, job coroutine is executed, and also
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.
Check..2
Check..3
Check..1
It runs in order.It's a coincidence.I don't know if wait paused and blocked the execution of job coroutine.
And
job.If wait()
is cleared
Check..3
Check..1
Check..2
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.
kotlin
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.
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.