Updated February 14, 2023
Introduction to Kotlin runBlocking
Kotlin runBlocking is a coroutine function; without providing any context to the runBlocking function, it will get to run on the main thread. Using it, we can run blocks and new coroutine of the current thread, which was interrupted until the completion. We cannot use this function as a coroutine; it is designed to bridge the code of regular blocking of libraries which was written in the style of suspending.
Key Takeaways
- Kotlin runBlocking allows us to create programs asynchronously and fluently. The created program is based on the programming style of passing continuation.
- The kotlin language is given a basic construct, but it will provide access to the coroutine and core library, which was helpful.
What is Kotlin runBlocking?
When the user calls the delay function in any of the coroutines, it is not blocking the thread from which it was running. The delay function is called to do the operations like UI updating and other things. If a delay function is suspended, then we call the same from another suspended function or the coroutine. It makes the writing asynchronous and easy non-blocking code for reading and understanding to handle the pattern of traditional callback. Kotlin runBlocking is a lightweight thread that was not blocking the main line for providing ability.
How to Run Kotlin runBlocking?
To run the kotlin runBlocking, we need to follow the below steps:
To run kotlin runBlocking, we are creating a new project in Intellij idea using kotlin language as follows.
1. To run kotlin runBlocking, in the first step, we create the new project in kotlin name as kotlin_Runblocking by using the Intellij idea at the time of making the project; we are providing the below parameter as follows.
Name – kotlin_Runblocking
location – \Documents
Language – Kotlin
Build system – Intellij
Jdk – Java version 11
Project – New project
2. After creating the project template, in this step, we check the project structure of the newly created project as follows.
3. After creating and checking the project structure in this step, we create the code for running the kotlin runBlocking.
Code:
class runblocking : AppCompatActivity () {
val stud:String = "kotlin runblocking"
override fun onCreate (savedInstanceState: Bundle?) {
super.onCreate (savedInstanceState)
setContentView (R.layout.activity_main)
GlobalScope.launch (Dispatchers.Main) {
delay (2000)
Log.d (stud, "Scope is global")
Toast.makeText (applicationContext, "Global Scope ",Toast.LENGTH_SHORT).show()
}
Log.d(TAG, "Not Global Scope ")
Toast.makeText (applicationContext, "Not Global Scope ",Toast.LENGTH_SHORT).show()
}
}
Output:
As we can see in the above example, “not global scope” shows that the global scope starts in a coroutine and is not blocking the main thread and also does not block the other operations which were performing at the time of delay will over. But at the same time, if anyone wants to call the function of suspending and there is no need for coroutine behavior, then we can call the stop function from the runBlocking. So when we want to call the suspend function like delay and not care about the asynchronous nature, we can use the runBlocking function. From the testing of junit, we need to access the suspend function from the test function.
Kotlin runBlocking Diagram
Below figure shows the diagram of kotlin runBlocking:
In the below example, runBlocking is the coroutine builder that gave us the scope of the coroutine from which we are executing code concurrently from all code. Builder of runBlocking is the bridge between the coroutine code and code inside the coroutine. The runBlocking builder will block the main thread until all of the children of the main thread are completing their code execution.
That’s why we can see the log will be printed at the end even when the coroutine is delayed by a second. If we check the launch time of the thread, it will suspend the execution and tell the parent coroutine runBlocking to execute their code. The runBlocking coroutine will block its calling thread until all children complete their execution. The total time of the main thread required to launch the thread is 1 sec. Because the 1 seconds launch block will run concurrently by using the execution of runBlocking. A coroutine builder is a way of creating the coroutine. The coroutine builder is not suspending us. They can be called from the code which was non-suspended.
Kotlin runBlocking Coroutines with Example
Coroutine will be considered the lightweight thread but contains several differences used in different threads.
The below example shows a simple runBlocking coroutine example.
Code:
fun main() = runBlocking {
launch {
delay (2000L)
println ("coroutine!")
}
println ("runblocking")
}
Output:
In the below example, we are extracting the block in a separate function. When performing refactoring of the extract function, we get a new function with suspending modifier. This is the first suspending function used inside the coroutine, the same as the regular function.
Code:
fun main() = runBlocking {
launch { doWorld () }
println ("coroutine")
}
suspend fun doWorld () {
delay (2000L)
println ("runblocking!")
}
Output:
In the below example, we are using coroutine scope from the function suspending as follows. The runBlocking and coroutine scope builder look similar because they are waiting for a child to complete the execution.
Code:
fun main() = runBlocking {
doWorld()
}
suspend fun doWorld() = coroutineScope {
launch {
delay (2000L)
println ("coroutine!")
}
println ("runblocking")
}
Output:
FAQ of Kotlin runBlocking
Other FAQs are mentioned below:
Q1. What is the use of scope builder in kotlin runBlocking?
Answer:
In addition to the coroutine scope, it was provided by different builders. So it is possible to declare our scope using the coroutine scope builder.
Q2. How can we declare the kotlin runBlocking?
Answer:
Below is the example of declaring the kotlin runBlocking as follows.
Code:
runBlocking (Dispatchers.Main) {
println(“Hello, World!”)
}
Q3. What is the use of coroutine builder in kotlin runBlocking?
Answer:
Coroutine builder is the way used for creating the coroutine. They are suspending themselves in a program.
Conclusion
Kotlin runBlocking makes the writing asynchronous and easy of non-blocking code for reading and understanding to handle the pattern of traditional callback. It is a coroutine function; without providing any context to the runBlocking function, it will get to run on the main thread.
Recommended Articles
This is a guide to Kotlin runBlocking. Here we discuss the introduction, how to run kotlin runBlocking with the diagram, and FAQ. You may also have a look at the following articles to learn more –