Welcome to this comprehensive guide on Kotlin's GlobalScope! By the end of this tutorial, you'll understand how to leverage the power of GlobalScope in your projects. Let's dive right in! 🐳
In Kotlin, GlobalScope is a top-level object of type CoroutineScope that represents the root scope for all coroutines in your application. It's used to run coroutines that aren't tied to any specific context.
To create a coroutine using GlobalScope, we'll use the launch function.
GlobalScope.launch {
// Your coroutine code here
}fun main() {
GlobalScope.launch {
println("Hello from GlobalScope!")
delay(1000)
println("Goodbye!")
}
Thread.sleep(2000) // Give the coroutine some time to execute
println("Main thread continuing...")
}Output:
Main thread continuing...
Hello from GlobalScope!
Goodbye!
In this example, we started a coroutine using GlobalScope, which printed a message and waited for a second before printing another one. Meanwhile, the main thread continued executing.
You can launch multiple coroutines concurrently using GlobalScope.
fun main() {
GlobalScope.launch {
println("Coroutine 1: Hello from GlobalScope!")
delay(1000)
println("Coroutine 1: Goodbye!")
}
GlobalScope.launch {
println("Coroutine 2: Hi from GlobalScope!")
delay(2000)
println("Coroutine 2: Bye!")
}
Thread.sleep(4000)
println("Main thread continuing...")
}Output:
Main thread continuing...
Coroutine 1: Hello from GlobalScope!
Coroutine 2: Hi from GlobalScope!
Coroutine 1: Goodbye!
Coroutine 2: Bye!
In this example, we started two coroutines concurrently. The first one printed a message and waited for a second before printing another one, while the second one did the same but waited for 2 seconds. The main thread continued executing after 4 seconds.
Which coroutine will print first in the example above?
You can cancel coroutines launched by GlobalScope using the cancel function.
fun main() {
val job = GlobalScope.launch {
println("Coroutine: Running")
repeat(1000) { i ->
println("Coroutine: $i")
if (i == 500) {
println("Coroutine: Canceling...")
this.cancel() // Cancel the coroutine
}
}
}
Thread.sleep(3000)
println("Main thread: Cancelling coroutine...")
job.cancelAndJoin() // Cancel the coroutine and wait for it to complete
println("Main thread: Coroutine canceled and completed")
}Output:
Coroutine: Running
Coroutine: 0
Coroutine: 1
Coroutine: Canceling...
Coroutine: 2
Coroutine: 3
Coroutine: 4
Coroutine: 5
Coroutine: 6
Coroutine: 7
Coroutine: 8
Coroutine: 9
Main thread: Cancelling coroutine...
Main thread: Coroutine canceled and completed
In this example, we started a coroutine that printed numbers from 0 to 1000. After 500 iterations, we canceled the coroutine using the cancel function. The main thread waited for the coroutine to complete before printing a message.
What will happen if you call `job.cancel()` after the coroutine has completed?
That's it for our deep dive into Kotlin's GlobalScope! We covered creating coroutines, running them concurrently, and canceling them. By now, you should have a solid understanding of this powerful tool in Kotlin's coroutines library. Happy coding! 🤖🚀