v4: Coroutines
fun postItem(item: Item) = launch {
val token = prepareToken() /
/ 1
val post = submitPost(token, item) /
/ 2
processPost(post) /
/ 3
}
}
Slide 9
Slide 9 text
Experimental status
Kotlin 1.1
-Xcoroutines=enable
kotlin.coroutines.experimental -> kotlin.coroutines
Slide 10
Slide 10 text
Experimental status
New style of programming
The design is not final and expected to change
JetBrains still collects information and feedbacks
Can and should be used in production
Backwards compatibility guaranteed
Slide 11
Slide 11 text
Terminology
coroutine
suspending function
suspending lambda
suspending function type
coroutine builder
suspension point
continuation
Slide 12
Slide 12 text
Terminology
coroutine
suspending function
suspending lambda
suspending function type
coroutine builder
suspension point
continuation
Slide 13
Slide 13 text
A coroutine is…
an instance of suspendable computation
similar to a daemon thread, but very light-weight
similar to a future or promise
Slide 14
Slide 14 text
Why coroutines?
threads are expensive to keep and switch
your code is single threaded
you’ve got lots of mutable states
Slide 15
Slide 15 text
suspend fun
computation that can be suspended
Slide 16
Slide 16 text
Standard API
• Language support (`suspend` keyword)
• low-level basic API (stdlib: kotlin.coroutines)
• high-level APIs that can be used in user code
async/await
/
/ C# way
async Task ProcessImage(String url)
{
var image = await LoadImage(url);
imageView.SetImage(image);
}
/
/ Kotlin way
fun processImage(url: String) = async(UI) {
val image = loadImageAsync(url).await()
imageView.setImage(image)
}
Slide 22
Slide 22 text
Generators API in
kotlin.coroutines
kotlin.coroutines.experimental
buildIterator()
buildSequence()
Slide 23
Slide 23 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8
Slide 24
Slide 24 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8
Slide 25
Slide 25 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8
Slide 26
Slide 26 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8
Slide 27
Slide 27 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8
Slide 28
Slide 28 text
buildSequence {
print("Start")
yield(1) /
/ suspension point
var prev = 0; var cur = 1
while (true) {
val next = prev + cur
print(“Next")
yield(next) /
/ suspension point
prev = cur; cur = next
}
print("End") /
/ unreachable code
}.take(6).forEach { print(" $it ") }
/
/ Output: Start 1 Next 1 Next 2 Next 3 Next 5 Next 8