Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
GPars
Concurrency in Java and Groovy
Contact Info
Ken Kousen
ken.kousen@kousenit.com
@kenkousen
http://www.kousenit.com (home page)
http://kousenit.wordpress.c...
Publications
O'Reilly video courses:
Groovy Programming Fundamentals
Practical Groovy Programming
Mastering Groovy Program...
GPars, CRPars,
Where'd you get those PPars?
Groovy Parallel Systems
http://gpars.codehaus.org
Oh, wait. CodeHaus is gone.
https://github.com/GPars is a good
place to ...
Features
JSR-166y Parallel Arrays
Multi-threaded collection processing
Asynchronous functions
Extend executor services
Features
Dataflow concurrency
Reactive programming
Actors
Agents
Groovy JDK
Groovy JDK
Adds methods to Java classes
static Thread.start(Closure closure)
BlockingQueue.leftShift(Object obj)
Threads vs Tasks
Coding with threads is difficult
Better to think in terms of tasks
Concurrent sequential code
Threading u...
Goals
Starting and stopping concurrent tasks
Coordinating concurrent tasks
Controlling access to shared mutable state
Concurrent Collections
Assume each item is independent
Groovy methods take closures
Consider each closure a task
Concurrent Collections
GParsPool.withPool {
collection.collectParallel {
// do parallel processing
}
}
Concurrent Collections
withPool
Default pool size is cores + 1
Can specify size argument
Uses fork/join pool of Java lib
GParsPoolUtil
GParsPoolUtil
Shows all the "parallel" methods
Concurrent methods
findAny rather than find
find → in order, returns first
fold rather than inject
inject → processes in o...
map / filter / reduce
Available on parallel collections
Use parallel property
Chain together
map / filter / reduce
assert 55 == [0, 1, 2, 3, 4].parallel
.map { it + 1 }
.map { it * it }
.reduce { acc, val -> acc + v...
map / filter / reduce
More opportunities for parallelization
Each element proceeds at its own rate
map / filter / reduce
Figure 18.1 from
Groovy in Action,
2nd edition
map / filter / reduce
Parallel vs Sequential
map → collect
filter → findAll
reduce → inject, fold
sum, max, min as usual
map / filter / reduce
Use parallel property to make parallel
Use collection property to convert back
Concurrent Collections
Pre-defined task coordination
Well-known flow of data
Flow of data predictable and
easy to coordina...
DataFlows
Coordinate tasks that wait for others
def flow = new Dataflows()
task { flow.result = flow.x + flow.y }
task { f...
DataFlows
Flow variables can only be assigned once
Best used with immutable variables
DataFlows
Not only wait for variables
Also calculate whenever vars available
DataFlows
DataflowVariable
DataflowStream
DataflowStreamReadAdapter
DataflowStreamWriteAdapter
DataflowOperator
DataflowQu...
Actors
Coordinate tasks explicitly
Concept comes from Erlang
Actors
At most one thread processing body
at any one time
State can be safely modified w/o sync
Actors
Messages passed between them
Messages should be immutable
Sender should not modify after sending
Actors
Stateless
DynamicDispatchActor
ReactiveActor
Stateful
DefaultActor
Actors
Send messages
send, leftShift
Receive messages
react, reply
Create new actors
actor
Actors
Inside actor, call react
consumes next message from inbox
waits if no message available
Actors
Use loop to continue to reply
def decryptor = Actors.actor {
loop {
react {String message ->
if ('stopService' == m...
Agents
Wraps references to mutable state
Thread safe
Non-blocking
Shared, mutable state
(like a shopping cart)
Agents
Clients can only send commands
functions, closures
Commands are serialized and
processed one-by-one
Agents
GPars provides an Agent class
wraps a ref to mutable state
accepts closures as messages
Others
STM → Software Transactional Memory
Active Objects → OO Agents
CSP → Concurrent Sequential Processing
Conclusions
Very powerful library
Concurrent collections
Dataflows
Actors
Agents
Much more...
GPars
Upcoming SlideShare
Loading in …5
×

GPars

318 views

Published on

Recorded at SpringOne2GX 2015
Presenter: Ken Kousen
Core Groovy Track

The Groovy Parallel Streams framework is a Groovy library for performing both Java and Groovy tasks concurrently. It offers many high-level concurrency abstractions, from Actors to Dataflow operators to Agents and Parallel Collections.

After reviewing the basic concepts, demonstrations will include communicating sequential processes, promises, dataflow concurrency, and concurrent collection processing. As multi-core and multi-processor systems become pervasive, the need for algorithms that take advantage of them becomes more and more critical. GPars is a powerful library with a straightforward syntax for managing concurrency.

Published in: Technology
  • Be the first to comment

GPars

  1. 1. GPars Concurrency in Java and Groovy
  2. 2. Contact Info Ken Kousen ken.kousen@kousenit.com @kenkousen http://www.kousenit.com (home page) http://kousenit.wordpress.com (blog) https://github.com/kousen (repositories)
  3. 3. Publications O'Reilly video courses: Groovy Programming Fundamentals Practical Groovy Programming Mastering Groovy Programming Learning Android Practical Android Gradle Fundamentals (link soon) Gradle for Android
  4. 4. GPars, CRPars, Where'd you get those PPars?
  5. 5. Groovy Parallel Systems http://gpars.codehaus.org Oh, wait. CodeHaus is gone. https://github.com/GPars is a good place to start
  6. 6. Features JSR-166y Parallel Arrays Multi-threaded collection processing Asynchronous functions Extend executor services
  7. 7. Features Dataflow concurrency Reactive programming Actors Agents
  8. 8. Groovy JDK Groovy JDK Adds methods to Java classes static Thread.start(Closure closure) BlockingQueue.leftShift(Object obj)
  9. 9. Threads vs Tasks Coding with threads is difficult Better to think in terms of tasks Concurrent sequential code Threading under the hood
  10. 10. Goals Starting and stopping concurrent tasks Coordinating concurrent tasks Controlling access to shared mutable state
  11. 11. Concurrent Collections Assume each item is independent Groovy methods take closures Consider each closure a task
  12. 12. Concurrent Collections GParsPool.withPool { collection.collectParallel { // do parallel processing } }
  13. 13. Concurrent Collections withPool Default pool size is cores + 1 Can specify size argument Uses fork/join pool of Java lib
  14. 14. GParsPoolUtil GParsPoolUtil Shows all the "parallel" methods
  15. 15. Concurrent methods findAny rather than find find → in order, returns first fold rather than inject inject → processes in order
  16. 16. map / filter / reduce Available on parallel collections Use parallel property Chain together
  17. 17. map / filter / reduce assert 55 == [0, 1, 2, 3, 4].parallel .map { it + 1 } .map { it * it } .reduce { acc, val -> acc + val }
  18. 18. map / filter / reduce More opportunities for parallelization Each element proceeds at its own rate
  19. 19. map / filter / reduce Figure 18.1 from Groovy in Action, 2nd edition
  20. 20. map / filter / reduce Parallel vs Sequential map → collect filter → findAll reduce → inject, fold sum, max, min as usual
  21. 21. map / filter / reduce Use parallel property to make parallel Use collection property to convert back
  22. 22. Concurrent Collections Pre-defined task coordination Well-known flow of data Flow of data predictable and easy to coordinate
  23. 23. DataFlows Coordinate tasks that wait for others def flow = new Dataflows() task { flow.result = flow.x + flow.y } task { flow.x = 10 } task { flow.y = 5 } assert 15 == flow.result
  24. 24. DataFlows Flow variables can only be assigned once Best used with immutable variables
  25. 25. DataFlows Not only wait for variables Also calculate whenever vars available
  26. 26. DataFlows DataflowVariable DataflowStream DataflowStreamReadAdapter DataflowStreamWriteAdapter DataflowOperator DataflowQueue
  27. 27. Actors Coordinate tasks explicitly Concept comes from Erlang
  28. 28. Actors At most one thread processing body at any one time State can be safely modified w/o sync
  29. 29. Actors Messages passed between them Messages should be immutable Sender should not modify after sending
  30. 30. Actors Stateless DynamicDispatchActor ReactiveActor Stateful DefaultActor
  31. 31. Actors Send messages send, leftShift Receive messages react, reply Create new actors actor
  32. 32. Actors Inside actor, call react consumes next message from inbox waits if no message available
  33. 33. Actors Use loop to continue to reply def decryptor = Actors.actor { loop { react {String message -> if ('stopService' == message) { println 'Stopping decryptor' stop() } else reply message.reverse() } } }
  34. 34. Agents Wraps references to mutable state Thread safe Non-blocking Shared, mutable state (like a shopping cart)
  35. 35. Agents Clients can only send commands functions, closures Commands are serialized and processed one-by-one
  36. 36. Agents GPars provides an Agent class wraps a ref to mutable state accepts closures as messages
  37. 37. Others STM → Software Transactional Memory Active Objects → OO Agents CSP → Concurrent Sequential Processing
  38. 38. Conclusions Very powerful library Concurrent collections Dataflows Actors Agents Much more...

×