Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycleScope.launch {
val randomNum = getRandomNum() // suspension point #1
val sqrt = getSqrt(randomNum.toDouble()) // suspension point #2
log(sqrt.toString())
}
}
private suspend fun getRandomNum(): Int {
delay(1000)
return (1..1000).shuffled().first()
}
private suspend fun getSqrt(num: Double): Double {
delay(2000)
return sqrt(num)
}
private fun log(text: String) {
Log.i(this@MainActivity::class.simpleName, text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment