Slide 1

Slide 1 text

GPars Promises to keep Václav Pech

Slide 2

Slide 2 text

About me ['Passionate programmer', 'Concurrency enthusiast', 'GPars lead', 'Developer/technology evangelist @ JetBrains' ].eachParallel {say it} http://www.jroller.com/vaclav http://twitter.com/vaclav_pech

Slide 3

Slide 3 text

We're all parallel now Use them or leave them!

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

# of cores

Slide 6

Slide 6 text

# of cores Today Soon

Slide 7

Slide 7 text

Dealing with threads sucks! public class Counter { private static long count = 0; public Counter() { count++; } }

Slide 8

Slide 8 text

Dealing with threads sucks! public class Counter { private static long count = 0; public Counter() { synchronized (this) { count++; } } }

Slide 9

Slide 9 text

Dealing with threads sucks! public class Counter { private static long count = 0; public Counter() { synchronized (this.getClass()) { count++; } } }

Slide 10

Slide 10 text

Dealing with threads sucks! public class ClickCounter implements ActionListener { public ClickCounter(JButton button) { button.addActionListener(this); } public void actionPerformed(final ActionEvent e) { ... } }

Slide 11

Slide 11 text

images.eachParallel {it.process()} documents.sumParallel() candidates.maxParallel {it.salary}.marry() Parallel Collections

Slide 12

Slide 12 text

Parallel Collections progLanguages.parallel.filter {it.concurrent} .max {it.javaInteroperability} .map {it.logo} ==

Slide 13

Slide 13 text

Fork/Join  Solve hierarchical problems  Divide and conquer  Merge sort, Quick sort  Tree traversal  File scan / search  … [a, b, c, d, e, f, g, h] [a, b, c, d] [e, f, g, h] [a, b] [c, d] [e, f] [g, h]

Slide 14

Slide 14 text

Thread Pool Tasks Worker threads Queue

Slide 15

Slide 15 text

Thread Pool Contention!

Slide 16

Slide 16 text

Fork/Join Thread Pool

Slide 17

Slide 17 text

Fork/Join Thread Pool Work stealing

Slide 18

Slide 18 text

Languages are either concurrent or obsolete.

Slide 19

Slide 19 text

Java 5 Asynchronous calculations

Slide 20

Slide 20 text

Java 7 Asynchronous calculations Fork/Join

Slide 21

Slide 21 text

Java 8 Asynchronous calculations Fork/Join Parallel collections

Slide 22

Slide 22 text

Scala Asynchronous calculations Fork/Join Parallel collections Actors

Slide 23

Slide 23 text

Clojure Asynchronous calculations Fork/Join Parallel collections Actors Agents, Stm

Slide 24

Slide 24 text

Oz Asynchronous calculations Fork/Join Parallel collections Actors Agents, Stm Dataflow

Slide 25

Slide 25 text

Google's Go Asynchronous calculations Fork/Join Parallel collections Actors Agents, Stm Dataflow CSP

Slide 26

Slide 26 text

Asynchronous calculations Fork/Join Parallel collections Actors Agents, Stm Dataflow CSP

Slide 27

Slide 27 text

Asynchronous calculations Fork/Join Parallel collections Actors Agents, Stm Dataflow CSP Agenda

Slide 28

Slide 28 text

Actors Processes with a mail-box Share no data Communicate by sending messages Use a thread-pool

Slide 29

Slide 29 text

Stateless Actors (pure Java) class MyActor extends DynamicDispatchActor { Account account = ... public void onMessage(String msg) { String encripted = encrypt(msg); reply(encripted); } public void onMessage(Integer number) { reply(2 * number); } public void onMessage(Money cash) { System.out.println("Received a donation " + cash); account.deposit(cash); } }

Slide 30

Slide 30 text

Active Objects @ActiveObject class MyCounter { private int counter = 0 @ActiveMethod def incrementBy(int value) { println "Received an integer: $value" this.counter += value } }

Slide 31

Slide 31 text

Composing async functions int hash1 = hash(download('http://www.gpars.org')) int hash2 = hash(loadFile('/gpars/website/index.html')) boolean result = compare(hash1, hash2) println result

Slide 32

Slide 32 text

Composing async functions @AsyncFun hash = oldHash @AsyncFun compare = oldCompare @AsyncFun download = oldDownload @AsyncFun loadFile = oldLoadFile def hash1 = hash(download('http://www.gpars.org')) def hash2 = hash(loadFile('/gpars/website/index.html')) def result = compare(hash1, hash2) println result.get()

Slide 33

Slide 33 text

Composing async functions @AsyncFun hash = oldHash @AsyncFun(blocking = true) compare = oldCompare @AsyncFun download = oldDownload @AsyncFun loadFile = oldLoadFile def hash1 = hash(download('http://www.gpars.org')) def hash2 = hash(loadFile('/gpars/website/index.html')) boolean result = compare(hash1, hash2) println result

Slide 34

Slide 34 text

int hash(String text) {…} Promise hash(Promise | String text)

Slide 35

Slide 35 text

int hash(String text) {…} Promise hash(Promise | String text) compare( hash( download() ), hash( loadFile() ) )

Slide 36

Slide 36 text

int hash(String text) {…} Promise hash(Promise | String text) { 1.Return a Promise for the result 2.Wait (non-blocking) for the text param 3.Call the original hash() 4.Bind the result }

Slide 37

Slide 37 text

Composing async functions Combine functions as usual Parallelism is detected automatically

Slide 38

Slide 38 text

Dataflow Concurrency  No race-conditions  No live-locks  Deterministic deadlocks Completely deterministic programs BEAUTIFUL code (Jonas Bonér)

Slide 39

Slide 39 text

Dataflow Variables / Promises main task2 task3 x y z task1

Slide 40

Slide 40 text

Dataflows def df = new Dataflows() task { df.z = df.x + df.y } task { df.x = 10 } task { println ”I am task 3” df.y = 5 } assert 15 == df.z

Slide 41

Slide 41 text

operator(inputs: [headers, bodies, footers], outputs: [articles, summaries]) {header, body, footer -> def article = buildArticle(header, body, footer) bindOutput(0, article) bindOutput(1, buildSummary(article)) } * + <> Dataflow Operators

Slide 42

Slide 42 text

Url resolver Url resolver Downloader Groovy scanner Scala scanner Url resolver Url resolver Reporter Speculator Evaluator Splitter Confirm Cache updates Approvals Dataflow Operators

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

'coz concurrency is Groovy Find more at: http://gpars.codehaus.org http://www.jroller.com/vaclav http://twitter.com/vaclav_pech GPars