Kotlin Coroutines: Difference between revisions
Jump to navigation
Jump to search
Line 40: | Line 40: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Coroutines= | =Coroutines= | ||
==Introduction== | |||
There are two co-routine builders (maybe) | |||
*runBlocking (wait for co-routine to finish used for unit tests) | |||
*launch non-blocking | |||
Co-routines are lightweight threads and you can run many more co-routines than threads. They are scheduled onto a thread so they do not necessarily run on the same thread. A delay operation does not stop the thread only the co-routine. e.g. | Co-routines are lightweight threads and you can run many more co-routines than threads. They are scheduled onto a thread so they do not necessarily run on the same thread. A delay operation does not stop the thread only the co-routine. e.g. | ||
<syntaxhighlight lang="kotlin"> | <syntaxhighlight lang="kotlin"> | ||
Line 48: | Line 52: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
It is very important not to use blocking code in a co-routine. As above delay is non-blocking to the thread but does delay the co-routine whereas Thread.sleep(5000) blocking. | |||
==Waiting and Cancelling== | |||
===Join=== | |||
<syntaxhighlight lang="kotlin"> | |||
fun main(args: Array<String>) = runBlocking { | |||
val job = launch { | |||
delay(1000) | |||
println("world") | |||
} | |||
job.join() | |||
} |
Revision as of 14:21, 27 December 2020
Moores Law
I am doing this because of this graph
Previously there is fork/join for asynchronous but this code is far more complicated than it probably needs to be
override fun compute(): Long {
return if (high - low <= SEQUENTIAL_THRESHOLD) {
(low until high)
.map { array[it].toLong() }
.sum()
} else {
val mid = low + (high - low) / 2
val left = Sum(array, low, mid)
val right = Sum(array, mid, high)
left.fork()
val rightAns = right.compute()
val leftAns = left.join()
leftAns + rightAns
}
}
Using the suspend approach is far more easier to read
suspend fun compute(array: IntArray, low: Int, high: Int): Long {
// println("low: $low, high: $high on ${Thread.currentThread().name}")
return if (high - low <= SEQUENTIAL_THRESHOLD) {
(low until high)
.map { array[it].toLong() }
.sum()
} else {
val mid = low + (high - low) / 2
val left = async { compute(array, low, mid) }
val right = compute(array, mid, high)
return left.await() + right
}
}
Coroutines
Introduction
There are two co-routine builders (maybe)
- runBlocking (wait for co-routine to finish used for unit tests)
- launch non-blocking
Co-routines are lightweight threads and you can run many more co-routines than threads. They are scheduled onto a thread so they do not necessarily run on the same thread. A delay operation does not stop the thread only the co-routine. e.g.
...
launch {
delay(1000)
println("world")
}
It is very important not to use blocking code in a co-routine. As above delay is non-blocking to the thread but does delay the co-routine whereas Thread.sleep(5000) blocking.
Waiting and Cancelling
Join
<syntaxhighlight lang="kotlin"> fun main(args: Array<String>) = runBlocking {
val job = launch { delay(1000) println("world") } job.join()
}