I have two coroutines inside another one, it's supposed that both inner coroutines are going to execute asynchronously, but this doesn't happened. The second one waits until the first finish. This is my code:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
var flag = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
launch {
printOne()
}
launch {
printTwo()
}
}
}
suspend fun printOne() {
while (flag == false)
Log.e("log", "111111")
}
suspend fun printTwo() {
Log.e("log", "222222")
Log.e("log", "222222")
Log.e("log", "222222")
Log.e("log", "222222")
flag = true
}
}
I assumed that the code inside the first launch{} would execute in a thread separately from the code in the second launch{} as well as any other corutine. But it keeps looping for ever inside the first launch{}