for you and me - amazon s3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · the...

42
Dierk König Concurrent programming for you and me GeeCon 2012 Mittwoch, 16. Mai 12

Upload: others

Post on 21-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Dierk König

Concurrent programming

for you and me

GeeCon 2012

Mittwoch, 16. Mai 12

Page 2: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Welcome!Dierk KönigFellow @ Canoo Engineering AG, Basel (CH)

Rich Internet ApplicationsProducts, Projects, Consultingwww.canoo.com

Open-source committer Groovy, Grails, GPars

Mittwoch, 16. Mai 12

Page 3: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

3

Mittwoch, 16. Mai 12

Page 4: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

What you can expect

4

F

F

F

N = 1

N = 2

N = 4

Mittwoch, 16. Mai 12

Page 5: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

5

Amdahl‘s law

Mittwoch, 16. Mai 12

Page 6: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

5

F: nicht parallelisierbare Anteil eines Programms

N: Anzahl der Kerne / Prozessoren

Amdahl‘s law

Mittwoch, 16. Mai 12

Page 7: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

0

1.25

2.50

3.75

5.00

1 2 3 4 5 10 20 100

F = 0,2Sp

eedu

p

Number of cores

Mittwoch, 16. Mai 12

Page 8: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

Mittwoch, 16. Mai 12

Page 9: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

1-5

Mittwoch, 16. Mai 12

Page 10: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

1-520-30

Mittwoch, 16. Mai 12

Page 11: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

1-520-30

300-400Cycles

Mittwoch, 16. Mai 12

Page 12: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

1-520-30

300-400Cycles

Unshared Object

Mittwoch, 16. Mai 12

Page 13: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Caching, Memory & CoProzessor

CoreL1/2-Cache

L3-Cache

CoreL1/2-Cache

Memory

Prozessor

1-520-30

300-400Cycles

Unshared Object Shared Object

Mittwoch, 16. Mai 12

Page 14: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

The Java state of affairs

Starting new threads is easy.Some real goodies in java.util.concurrent.* & Java 7

Manual thread-coordination is difficult.Access to shared state is error-prone.

Scheduling issues for many threads with badconcurrency characteristics.Good use of pooling is not obvious.Concepts are rather „low level“.

8

Mittwoch, 16. Mai 12

Page 15: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

For the application programmer

The servlet model works!

Swing, JavaFx? Oh, well...

We should learn from what works.

9

Mittwoch, 16. Mai 12

Page 16: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

It‘s all about coordination

Fork/JoinMap/Filter/Reduce

Actor Agent

DataflowKanbanflow

Working on collections with fixed coordination

Explicit coordinationDelegated coordinationImplicit coordinationBalanced coordination

1.8.6

Mittwoch, 16. Mai 12

Page 17: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Fork/Join on collections

import static groovyx.gpars.GParsPool.withPool

def numbers = [1, 2, 3, 4, 5, 6]def squares = [1, 4, 9, 16, 25, 36]

withPool { assert squares == numbers.collectParallel { it * it }}

// in reality, find better chunks of work!

11

Mittwoch, 16. Mai 12

Page 18: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Fork/Join on collections

import static groovyx.gpars.GParsPool.withPool

def numbers = [1, 2, 3, 4, 5, 6]def squares = [1, 4, 9, 16, 25, 36]

withPool { assert squares == numbers.collectParallel { it * it }}

// in reality, find better chunks of work!

11

makeConcurrent()Variation

Mittwoch, 16. Mai 12

Page 19: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

More such methods

any { ... } collect { ... } count(filter) each { ... } eachWithIndex{ ... } every { ... } find { ... } findAll { ... } findAny { ... } fold { ... } fold(seed) { ... } grep(filter) groupBy { ... } max { ... } max() min { ... } min() split { ... } sum()

12

Mittwoch, 16. Mai 12

Page 20: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Map/Filter/Reduce on collectionsimport static groovyx.gpars.GParsPool.withPool

withPool { assert 55 == [0, 1, 2, 3, 4].parallel .map { it + 1 } .map { it ** 2 } .reduce { a, b -> a + b }}

13

Mittwoch, 16. Mai 12

Page 21: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Fork/Join vs Map/Filter/Reduce

14

!

Mittwoch, 16. Mai 12

Page 22: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Fork/Join vs Map/Filter/Reduce

14

!

fixed coordination

Mittwoch, 16. Mai 12

Page 23: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Explicit coordination with Actors

import static groovyx.gpars.actor.Actors.*

def printer = reactor { println it }def decryptor = reactor { reply it.reverse() }

actor { decryptor.send 'lellarap si yvoorG' react { printer.send 'Decrypted message: ' + it decryptor.stop() printer.stop() }}.join()

15

Mittwoch, 16. Mai 12

Page 24: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Actors

Process one message at a time.

Dispatch on the message type, which fits nicely with dynamic languages.

Are often used in composition,which can lead to further problems down the road.

16

Personal note:

Actors are overrated

Mittwoch, 16. Mai 12

Page 25: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Delegate to an Agent

import groovyx.gpars.agent.Agent

def safe = new Agent<List>( ['GPars'] )

safe.send { it.add 'is safe!' }safe.send { updateValue it * 2 }

println safe.val

17

Mittwoch, 16. Mai 12

Page 26: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Agents

Analogous to Clojure agents (atoms, refs, ...)

Implementations differ much in efficiency.

18

Mittwoch, 16. Mai 12

Page 27: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

DataFlow for implicit coordination

import groovyx.gpars.dataflow.Dataflowsimport static groovyx.gpars.dataflow.Dataflow.task

final flow = new Dataflows()task { flow.result = flow.x + flow.y }!task { flow.x = 10 }! ! !task { flow.y = 5 }! ! !

assert 15 == flow.result!

19

Mittwoch, 16. Mai 12

Page 28: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Dataflow

Flavors: variables, streams, operators, tasks, flows

Write-Once, Read-Many (non-blocking)

Feel free to use millions of them

Fast, efficient, safe, and testable!

20

Model the flow of data,

not the control flow!

Mittwoch, 16. Mai 12

Page 29: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

KanbanFlow in code

@Grab('org.codehaus.gpars:gpars:1.0-beta-1')import groovyx.gpars.dataflow.* import static ProcessingNode.node

def producer = node { below -> below << 1 }def consumer = node { above -> println above.take() }

new KanbanFlow().with { link producer to consumer start() // run for a while stop()}

21

Mittwoch, 16. Mai 12

Page 30: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow scenarios

22

Producer Consumer

Mittwoch, 16. Mai 12

Page 31: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow chaining

23

Producer Filter Consumer

Mittwoch, 16. Mai 12

Page 32: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow cycles

24

Generator Filter Collector

Mittwoch, 16. Mai 12

Page 33: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow load balancer

25

Master Slave

Slave

Slave

Mittwoch, 16. Mai 12

Page 34: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow broadcast

26

Producer Consumer

Consumer

Consumer

Mittwoch, 16. Mai 12

Page 35: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Kanbanflow combinator

27

CombinatorProducer

Producer

Producer

Mittwoch, 16. Mai 12

Page 36: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Efficient Producer-Consumer

KanbanFlow pattern by /me

http://people.canoo.com/mittie/kanbanflow.html

Simple idea, amazing results

Resource efficient, composable, testable

28

Non-blocking writes,

Deadlock-free by design

Mittwoch, 16. Mai 12

Page 37: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Takeaways

29

1 Learn the concepts

Experiment with GPars

Validate

2

3

Mittwoch, 16. Mai 12

Page 38: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Further reading

• Groovy in Action, 2nd Editionchapter 17

• gpars.codehaus.org

Mittwoch, 16. Mai 12

Page 39: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

31

Mittwoch, 16. Mai 12

Page 40: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

31

Mittwoch, 16. Mai 12

Page 41: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Discussion

credits: Paul King

Mittwoch, 16. Mai 12

Page 42: for you and me - Amazon S3s3-eu-west-1.amazonaws.com/presentations2012/8... · 2012-05-23 · The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.*

Discussion

credits: Paul King

[email protected]@mittie

Mittwoch, 16. Mai 12