Slides of the talk at the KKON Warmup 2021. See https://rheinwerk-kkon.de/recap-2021/programm-2021/dinkla-nebenlaeufigkeit-mit-koroutinen-strukturieren/
2. C++
1985
C++ 98
1998
2
2004 2007 2021
2001 2011 2014 2016
Java 9
Reactive
2017
Java 7
Fork/Join
2011
Java 8
Streams
CmplFut
2014
Java 5
2004
Java 1
1995
C++ 11
thread
2011
C++ 14
mutex
2014
C++ 17
par STL
2017
C++ 20
2020
Kotlin
1.0
2016
Kotlin
1.3
2018
Kotlin
1.4
2020
Historische Perspektive
Kotlin
0.0
2011
3. Kotlin ist Multiplatform
● Server Side / JVM
● Android
● Multiplatform Mobile (core)
● JavaScript (core)
Code für den Vortrag nur auf JVM getestet
https://github.com/jdinkla/2021_kkon_warmup.git
Schwerpunkt hier: JVM
3
https://github.com/Kotlin/kotlinx.coroutines
14. Alle Räder stehen still, ...
● Weil nur Platz für 4 Threads im Pool ist, muss einer nach
den anderen laufen
● Thread.sleep blockiert den Thread
… wenn alle Threads im Pool blockiert sind
14
Rechner wird nicht
genutzt
15. Vergrößerung des Pools?
15
● Ncpu: Anzahl der CPUs “number”
● Ucpu: Auslastung “utilization” 0.0 - 1.0
Verhältnis W/C
● Wartender Anteil W “waiting”
● Rechnender Anteil C “computing”
Waiting und Computing
16. Berechnung der Pool-Größe
16
Ncpu Ucpu W C Nthreads
8 1.0 0 1 8
8 1.0 1 1 16
8 1.0 10 1 80
8 1.0 430 9 390.2
CPU bound
Latency bound
Beispiele
17. Folgerungen
17
Wenn W >> C wird der Pool sehr groß
⇒
W/C sollte minimiert werden
Frage: Kann man statt warten etwas anderes tun?
“Latency hiding”
22. Hello World mit Koroutinen
22
● Bindeglied zu synchronem Code
● Neuer CoroutineScope
● Wartet auf alle Koroutinen
runBlocking { }
23. Hello World mit Koroutinen
23
● Koroutine-Builder
● Startet Koroutine asynchron
launch { }
24. Hello World mit Koroutinen
24
● Analog Thread.sleep(), aber nicht-blockierend
● “suspension point”
delay()
25. Unterbrechbare Funktionen ...
25
● Einziges neues Keyword für Koroutinen
● Unterbrechbare Funktionen oder Lambdas
○ Nur von anderen unterbrechbaren Funktionen oder
Koroutinen aufrufbar
Werden mit suspend gekennzeichnet
31. Wer veranlasst die Unterbrechung ?
Unterschiedliche Arten des Multitaskings
31
Thread
● Präemptives Multitasking
● Von außen vom OS-Scheduler unterbrochen
● Unterbrechung kann zu jeder Zeit passieren
Koroutine
● Kooperatives Multitasking
● Benutzt Suspension-Points, um zum Dispatcher
zurückzukehren
32. Jede Phase ist ein Zustand in einem endlichen Automat
Koroutinen
32
https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md
Kotlin Evolution and Enhancement Process
“Suspension Points” definieren “Phasen”
Phase 0
Phase 1
Phase 2
40. CoroutineScope
40
● Hat CoroutineContext
● Platz für eigene Extensionsfunktionen
● Hierarchische Organisation
○ Keine Leaks
○ Exceptions- und Cancelling
Structured Concurrency
Job
Job
Job
Job
53. Communicating sequential processes (CSP)
53
● Modell der Interaktion von Prozessen
● C.A.R. Hoare
○ (Quicksort, ”Erfinder” von null)
● Ursprünglich eher theoretisch
● Wiederbelebt von Go und den “Goroutinen”
● Basiert auf Events die über Kanäle geschickt werden
54. Anwendungsfall
54
● Wie können Erzeuger und Verbraucher synchronisiert werden?
● “Backpressure”, wenn Geschwindigkeit der Producer zu hoch!
Producer-Consumer / Erzeuger-Verbraucher
https://play.kotlinlang.org/hands-on/Introduction%20to%20Coroutines%20and%20Channels/08_Channels
70. Koroutinen mit Swing
Die App bleibt responsiv, weil die Koroutine den Swing-Thread nicht blockiert
70
71. Retrofit
Retrofit erzeugt einen Rest-Client aus Schnittstellen:
REST-Clients auch mit Koroutinen
71
https://square.github.io/retrofit/
https://play.kotlinlang.org/hands-on/Introduction%20to%20Coroutines%20and%20Channels/01_Introduction
73. Wann sollte ich Koroutinen einsetzen?
● Lohnt sich die Umstellung?
● Wartbarkeit des Codes
○ Sequentieller Code ist immer einfacher zu lesen
● Lohnt sich die nebenläufige Ausführung?
○ W/C
○ Threads und Koroutinen haben einen Overhead
● Granularität
○ Lassen sich einzelne Tasks zusammen ausführen?
Es lohnt sich nicht immer ...
73