Last active
September 14, 2022 08:34
-
-
Save aleksandarzekovic/c012285502a12140d2481c8cd0497503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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