kotlin coroutines
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
}
4
2
import kotlinx.coroutines.*
// Asynchronous execution
fun main() {
GlobalScope.launch { // creates a new coroutine and continues
doWorld() // suspending function
}
println("World !") // execution continues even while coroutine waits
runBlocking { // block main thread for 4 s (waits for 1rst coroutine)
delay(4000L)
}
}
suspend fun doWorld() {
delay(2000L) // non-blocking delay for 2000 milliseconds
println("Hello") // printed after "World !"
}
Thank you!
2
0
4
2
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'
Thank you!
2
0
3.86
7
CoroutineScope
To start coroutine scope you can:
Use GlobalScope that has empty coroutine context.
Implement CoroutineScope interface.
Create a scope from a context:
with(CoroutineScope(context = context)) { ... }
Thank you!
7
0
3
1
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
Thank you!
1
0
3.86
7
// Kotlin coroutines
def coroutines_version = "1.5.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $coroutines_version"
Thank you!
7
0
Are there any code examples left?
New code examples in category Other