Top Banner
#codetalkshh New Concurrency Models on the JVM Lutz Hühnken http://www.huehnken.de | @lutzhuehnken
46

JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

Jan 14, 2017

Download

Software

Lutz Hühnken
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

New Concurrency Models on the JVM

Lutz Hühnken http://www.huehnken.de | @lutzhuehnken

Page 2: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Warum ist Concurrency überhaupt interessant?

2

Page 3: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Hardware ist parallel.

3

Page 4: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Wir finden sequentiell einfach.

4

Page 5: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 5

Page 6: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 6

Page 7: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 7

Page 8: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 8

Page 9: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Wie sieht es denn heute aus? Threads.

9

Page 10: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Problem 1: Effizienz

10

Page 11: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 11

Page 12: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 12

1 2 3 … 10.000

Page 13: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 13

Source: John Rose, Java VM Architect, JFokus, Stockholm, February 2015

Page 14: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Lösung Effizienz:

• Sub-Thread-Level Concurrency • Asynchrone I/O

• Das ist allen den folgenden Ansätzen gemeinsam!

• Das ist allen „Reactive Systems“ gemeinsam!

14

Page 15: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Problem 2: Programmiermodell

15

Page 16: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 16

They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism. Threads, as a model of computation, are wildly nondeterministic, and the job of the programmer becomes one of pruning that nondeterminism.

Page 17: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Was interessiert uns denn?

Zustand

Komposition

Interoperation

17

Page 18: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 18

Green Threads (User Mode Threads, Fibers) Quasar

Agenten Clojure

Communicating Sequential Processes (CSP) Clojure

Event Bus vert.x

Aktoren Akka

Programmiermodelle

Page 19: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Fibersnew Fiber<V>() { @Override protected V run() throws SuspendExecution,

InterruptedException { // code hier }}.start();

19

Page 20: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Fibers

Vorteil: Imperative Programmierung, wie mit Threads

Nachteil: Imperative Programmierung, wie mit Threads

20

Page 21: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

Reactive Slick

Warum ist asynchrone I/O so wichtig?

Threads als kleinste Einheit der Nebenläufigkeit

21

Wichtig: Dies ist eine Momentaufnahme, kein Ablauf

Page 22: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

Reactive Slick

Warum ist asynchrone I/O so wichtig?

Threads als Vehikel für kleinere Einheiten (z.B. Fibers)

22

Wichtig: Dies ist eine Momentaufnahme, kein Ablauf

Page 23: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Einschub: Callback Hellfs.readdir(source, function(err, files) { if (err) { console.log('Error finding files: ' + err) } else { files.forEach(function(filename, fileIndex) { console.log(filename) gm(source + filename).size(function(err, values) { if (err) { console.log('Error identifying file size: ' + err) } else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.forEach(function(width, widthIndex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + 'x' + height) this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) { if (err) console.log('Error writing file: ' + err) }) }.bind(this)) } }) }) } })

23

Page 24: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Callback Hell Alternativen

• Channels (sehen wir noch) • Events oder Messages (sehen wir noch) • Lesbare Syntax für „onComplete“ (z.B.

Scala flatMap / for expression)

24

Page 25: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Fibersclass FooAsync extends FiberAsync<String, FooException> implements FooCompletion { @Override public void success(String result) { asyncCompleted(result); }

@Override public void failure(FooException exception) { asyncFailed(exception); }}

wird zu

String op() { new FooAsync() { protected void requestAsync() { Foo.asyncOp(this); } }.run();}

25

Page 26: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Fibers

•Effizienz ja •Programmiermodell unverändert •Aber: Eine Menge interessanter Tricks

(Instrumentation, Continuations, Thread Interop)

•Low-level Grundlage für andere Konstrukte •Drop-In Ersatz für Threads

26

Page 27: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Agenten

(def x (agent 0)) (defn increment [c n] (+ c n)) (send x increment 5) ; @x -> 5 (send x increment 10) ; @x -> 15

27

Page 28: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Agenten

• Der Agent kapselt den Zustand

• Sende eine Funktion als Nachricht an den Agenten, diese wird asynchron ausgeführt

28

Page 29: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Agenten

• Attraktivität: Funktionale Programmierung!

(Unveränderliche Werte als Normalfall, veränderlicher Zustand als Ausnahme)

• Keine Lösung für Komposition, daher ☞ Channels

29

Page 30: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Clojure Channels

(def echo-chan (chan)) (go (println (<! echo-chan))) (>!! echo-chan "ketchup") ; => true ; => ketchup

30

Page 31: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Clojure Channels

(def echo-buffer (chan 2)) (>!! echo-buffer "ketchup") ; => true (>!! echo-buffer "ketchup") ; => true (>!! echo-buffer "ketchup") ; blocks

31

Page 32: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Channels

• Sehr flexible Komposition • Implementieren Communicating Sequential

Processes (Tony Hoare 1978, https://en.wikipedia.org/wiki/Communicating_sequential_processes)

32

Page 33: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Event Bus (vert.x)

33

public class Receiver extends AbstractVerticle {

@Override public void start() throws Exception {

EventBus eb = vertx.eventBus();

eb.consumer("ping-address", message -> {

System.out.println("Received message: " + message.body()); // Now send back reply message.reply("pong!"); });

System.out.println("Receiver ready!"); } }

Page 34: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Event Bus (vert.x)

34

Image from Jonas Bandi @jbandi

Page 35: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Event Bus (vert.x)

• „Single Thread Illusion“ • Lose Kopplung • Hybrides Thread-Modell • Bonus: Verteilung

35

Page 36: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

36

Page 37: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

37

Page 38: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

38

Page 39: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

39

Page 40: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

40

Page 41: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

41

Page 42: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

Aktoren (Akka)

• „Single Thread Illusion“ • Messaging, incl. Routing etc. • Dispatcher • Bonus: Verteilung, Supervision

42

Page 43: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh 43

Green Threads (User Mode Threads, Fibers) Quasar

Agenten Clojure Agents

Communicating Sequential Processes (CSP) Clojure Channels

Event Bus vert.x

Aktoren Akka

Programmiermodelle

Page 44: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

• Quasar hat auch eine Implementierung von Channels, und sogar Aktoren

• Mit Akka kann man auch einen Event Bus implementieren, und auch Agenten

• Es gibt eine Welt außerhalb der JVM (Go Channels, Erlang…)

• …

44

Der Vollständigkeit halber

Page 45: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

#codetalkshh

• Concurrency ist interessant

• Threads sind passé, Alternativen sind vorhanden

• Wenn ihr euch nur eine Alternative anseht, empfehle ich Akka

45

Fazit

Page 46: JVM Concurreny Models at code.talks, Hamburg, 30.9.2015

Vielen Dank

Nicht vergessen: Bewertung abgeben (in der App)

Lutz Hühnken http://www.huehnken.de | @lutzhuehnken