第十二章 Kotlin的多线程:协程(Coroutines)
Kotlin 1.1 introduced coroutines, a new way of writing asynchronous, non-blocking code (and much more). In this tutorial we will go through some basics of using Kotlin coroutines with the help of the kotlinx.coroutines
library, which is a collection of helpers and wrappers for existing Java libraries.
Make sure it's configured for Kotlin 1.1 or higher.
Since coroutines have the experimental status in Kotlin 1.1, by default the compiler reports a warning every time they are used. We can opt-in for the experimental feature and use it without a warning by adding this code to build.gradle
:
apply plugin: 'kotlin'
kotlin {
experimental {
coroutines 'enable'
}
}
Since we'll be using the