APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is...

26
APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS Dierk König Canoo Mittwoch, 12. Oktober 2011

Transcript of APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is...

Page 1: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS

Dierk KönigCanoo

Mittwoch, 12. Oktober 2011

Page 2: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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

Rich Internet ApplicationsProducts, Projects, Consultingwww.canoo.com

Open-source committer Groovy, Grails, GPars

Mittwoch, 12. Oktober 2011

Page 3: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Groovy & GPars mission

3

1 Built for Java developers

Mend with Java

Make concurrency simpler

2

3

Mittwoch, 12. Oktober 2011

Page 4: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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“.

4

Mittwoch, 12. Oktober 2011

Page 5: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

It‘s all about coordination

Fork/JoinMap/Reduce

Actor SafeDataflow

Working on collections with fixed coordination

Explicit coordinationDelegated coordinationImplicit coordination

1.8.2

Mittwoch, 12. Oktober 2011

Page 6: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

It‘s all about coordination

Fork/JoinMap/Reduce

Actor SafeDataflow

Asynchronizer

STM

more

Working on collections with fixed coordination

Explicit coordinationDelegated coordinationImplicit coordination

1.8.2

Mittwoch, 12. Oktober 2011

Page 7: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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 }}

6

Mittwoch, 12. Oktober 2011

Page 8: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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 }}

6

makeTransparent()Variation

Mittwoch, 12. Oktober 2011

Page 9: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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()

7

Mittwoch, 12. Oktober 2011

Page 10: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Map/Reduce on collections

import 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 }}

8

Mittwoch, 12. Oktober 2011

Page 11: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Fork/Join vs Map/Reduce

9

!

Mittwoch, 12. Oktober 2011

Page 12: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Fork/Join vs Map/Reduce

9

!

fixed coordination

Mittwoch, 12. Oktober 2011

Page 13: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Explicit coordination with Actors

import static groovyx.gpars.actor.Actors.*

def printer = reactor { println it }def decryptor = reactor { message -> if (message in String) reply message.reverse() else stop()}actor { decryptor.send 'lellarap si yvoorG' react { printer.send 'Decrypted message: ' + it decryptor.send false }}.join()

10

Mittwoch, 12. Oktober 2011

Page 14: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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.

11

Personal note:

Actors are overrated

Mittwoch, 12. Oktober 2011

Page 15: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Delegate to an Agent

import groovyx.gpars.agent.Agent

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

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

println name.val

12

Mittwoch, 12. Oktober 2011

Page 16: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Safe

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

Implementations differ much in efficiency.

13

Mittwoch, 12. Oktober 2011

Page 17: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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!

14

Mittwoch, 12. Oktober 2011

Page 18: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

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!

15

Model the flow of data,

not the control flow!

Mittwoch, 12. Oktober 2011

Page 19: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

KanbanFlow in code

import static ProcessingNode.nodeimport groovyx.gpars.kanban.KanbanFlow

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

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

16

Mittwoch, 12. Oktober 2011

Page 20: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Efficient Producer-Consumer

KanbanFlow pattern by /me

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

Simple idea, amazing results

Resource efficient, composable, testable

17

Non-blocking writes,

Deadlock-free by design

Mittwoch, 12. Oktober 2011

Page 21: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Takeaways

18

1 Experiment with GPars!

Great for learning concepts!

Get involved!

2

3

Mittwoch, 12. Oktober 2011

Page 22: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Further reading

• Groovy in Action groovy.canoo.com/gina Manning, 2007, Foreword by James GoslingKönig with Glover, Laforge, King, Skeet

• groovy.codehaus.orggpars.codehaus.org

Mittwoch, 12. Oktober 2011

Page 23: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

20

Mittwoch, 12. Oktober 2011

Page 24: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

20

Mittwoch, 12. Oktober 2011

Page 25: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Discussion

credits: Paul King

Mittwoch, 12. Oktober 2011

Page 26: APPROACHABLE CONCURRENCY FOR THE JVM WITH GPARS...The Java state of affairs Starting new threads is easy. Some real goodies in java.util.concurrent.* & Java 7 Manual thread-coordination

Discussion

credits: Paul King

[email protected]@mittie

Mittwoch, 12. Oktober 2011