Visual system Oculus et structurae pertinentes © David Kachlík 30.9.2015.
JVM Concurreny Models at code.talks, Hamburg, 30.9.2015
-
Upload
lutz-huehnken -
Category
Software
-
view
894 -
download
0
Transcript of 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
#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.
#codetalkshh 18
Green Threads (User Mode Threads, Fibers) Quasar
Agenten Clojure
Communicating Sequential Processes (CSP) Clojure
Event Bus vert.x
Aktoren Akka
Programmiermodelle
#codetalkshh
Fibersnew Fiber<V>() { @Override protected V run() throws SuspendExecution,
InterruptedException { // code hier }}.start();
19
#codetalkshh
Fibers
Vorteil: Imperative Programmierung, wie mit Threads
Nachteil: Imperative Programmierung, wie mit Threads
20
Reactive Slick
Warum ist asynchrone I/O so wichtig?
Threads als kleinste Einheit der Nebenläufigkeit
21
Wichtig: Dies ist eine Momentaufnahme, kein Ablauf
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
#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
#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
#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
#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
#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
#codetalkshh
Agenten
• Der Agent kapselt den Zustand
• Sende eine Funktion als Nachricht an den Agenten, diese wird asynchron ausgeführt
28
#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
#codetalkshh
Clojure Channels
(def echo-chan (chan)) (go (println (<! echo-chan))) (>!! echo-chan "ketchup") ; => true ; => ketchup
30
#codetalkshh
Clojure Channels
(def echo-buffer (chan 2)) (>!! echo-buffer "ketchup") ; => true (>!! echo-buffer "ketchup") ; => true (>!! echo-buffer "ketchup") ; blocks
31
#codetalkshh
Channels
• Sehr flexible Komposition • Implementieren Communicating Sequential
Processes (Tony Hoare 1978, https://en.wikipedia.org/wiki/Communicating_sequential_processes)
32
#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!"); } }
#codetalkshh
Event Bus (vert.x)
• „Single Thread Illusion“ • Lose Kopplung • Hybrides Thread-Modell • Bonus: Verteilung
35
#codetalkshh
Aktoren (Akka)
• „Single Thread Illusion“ • Messaging, incl. Routing etc. • Dispatcher • Bonus: Verteilung, Supervision
42
#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
#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
#codetalkshh
• Concurrency ist interessant
• Threads sind passé, Alternativen sind vorhanden
• Wenn ihr euch nur eine Alternative anseht, empfehle ich Akka
45
Fazit