approachable concurrency for the jvm with gpars...the java state of affairs starting new threads is...
TRANSCRIPT
![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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/1.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/24.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022040608/5ec6ea9a89b9675d89519196/html5/thumbnails/25.jpg)
Discussion
credits: Paul King
Mittwoch, 12. Oktober 2011