0
0
Kotlinprogramming~5 mins

RunBlocking for bridging in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is runBlocking in Kotlin coroutines?

runBlocking is a function that blocks the current thread until the coroutine inside it completes. It is mainly used to bridge regular blocking code with coroutine code.

Click to reveal answer
beginner
Why should runBlocking be used carefully in Kotlin?

Because it blocks the thread, using runBlocking in UI or main threads can freeze the app. It is best used in main functions or tests where blocking is acceptable.

Click to reveal answer
intermediate
How does runBlocking help in bridging coroutine and non-coroutine code?

runBlocking allows you to call suspend functions from regular blocking code by starting a coroutine and waiting for its result synchronously.

Click to reveal answer
intermediate
What is the difference between runBlocking and launch?

runBlocking blocks the current thread until its coroutine finishes, while launch starts a coroutine without blocking the thread.

Click to reveal answer
beginner
Show a simple example of runBlocking usage in Kotlin.
<pre>import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Start")
    delay(1000L) // suspend for 1 second
    println("End")
}</pre><p>This blocks the main thread until the coroutine completes.</p>
Click to reveal answer
What does runBlocking do in Kotlin coroutines?
ACreates a new thread
BStarts a coroutine without blocking
CBlocks the current thread until the coroutine completes
DCancels all coroutines
Where is it safe to use runBlocking?
AIn main functions or tests
BInside suspend functions
CIn UI main thread of Android apps
DIn background threads only
Which of these is a key use of runBlocking?
ATo create threads
BTo launch multiple coroutines concurrently
CTo cancel coroutines
DTo bridge blocking and coroutine code
What happens if you use runBlocking on the UI thread?
AThe UI freezes until coroutine finishes
BThe coroutine runs asynchronously
CNothing special happens
DThe coroutine is cancelled immediately
Which function blocks the thread: runBlocking or launch?
Alaunch
BrunBlocking
CBoth
DNeither
Explain how runBlocking helps to bridge blocking and coroutine code in Kotlin.
Think about how you can call suspend functions from normal code.
You got /4 concepts.
    Describe the risks of using runBlocking on the UI thread and when it is appropriate to use it.
    Consider what happens when the UI thread is blocked.
    You got /4 concepts.