GPars For Beginners

Matt Passell
Matt PassellSoftware Consultant at Grove Hill Software LLC
For Beginners
     A Groovy approach to concurrent programming




Matt Passell                               May 25, 2011
About Me
•   Longtime Java developer (since '97)
•   Using Groovy on and off since early 2008
•   Concurrent programming enthusiast
•   Started software consultancy in 2007
•   More Groovy + Less Java = More Happy
Early History
●
  October 2008: Václav Pech creates
  GParallelizer
●
  September 2009: project renamed
  to GPars, moved to Codehaus, and
  Groovy luminaries join the team -
    (Dierk König, Paul King, Alex Tkachman, Russel
    Winder)
GPars Has a Logo Contest




    http://gpars.codehaus.org/Logo+Contest
History
●
    October 2008: Václav Pech creates GParallelizer
●
    September 2009: project renamed to GPars,
    moved to Codehaus, and Groovy luminaries join
    the team (Dierk König, Paul King, Alex Tkachman, Russel Winder)
●
    December 2009: GPars gets a logo
●
    May 2010: 0.10 Release
●
    December 2010: 0.11 Release
●
    May 2011: 0.12 Beta 1
What GPars Provides
●
    Code-level Helpers
●
    Architecture-level Concepts
●
    Protecting Shared Mutable State
What GPars Provides
●
    Code-level Helpers
    ●
        Fork/Join
    ●
        Map/Reduce (Parallel Collections)
    ●
        Asynchronous Processing*




              *not covered in this presentation
What GPars Provides
●
    Architecture-level Concepts
    ●
        Dataflow Concurrency
    ●
        Actors
    ●
        Communicating Sequential Processes*




                 *not covered in this presentation
What GPars Provides
●
    Protecting Shared Mutable State
    ●
        Agents
    ●
        Software Transactional Memory*




             *not covered in this presentation
Recurring Patterns
●
    Favor immutability
●
    Stateless code blocks (similar to
    Servlets)
Fork/Join & Map/Reduce

    Fork   Join         Fork     Join




     Map          Map          Reduce
Fork/Join
def list = [1, 2, 3, 4]
GParsPool.withPool {
    assert [2, 4, 6, 8] == list.collectParallel { it * 2 }
    def animals = ['dog', 'ant', 'cat', 'whale']
    println(animals.makeTransparent().
      collect {it.toUpperCase()}.
      groupBy {it.contains 'A'})
}


ParallelEnhancer.enhanceInstance(list)
assert [2, 4, 6, 8] == list.collectParallel { it * 2 }




                            DemoParallelizer & DemoParallelEnhancer.groovy
Fork/Join
def list = [1, 2, 3, 4]
GParsPool.withPool {
    assert [2, 4, 6, 8] == list.collectParallel { it * 2 }
    def animals = ['dog', 'ant', 'cat', 'whale']
    println(animals.makeTransparent().
      collect {it.toUpperCase()}.
      groupBy {it.contains 'A'})
}


ParallelEnhancer.enhanceInstance(list)
assert [2, 4, 6, 8] == list.collectParallel { it * 2 }




                            DemoParallelizer & DemoParallelEnhancer.groovy
Parallel Collections (Map/Reduce)
GParsPool.withPool {
     assert 20 == [1, 2, 3, 4, 5].parallel.
      filter {it % 2 == 0}.
      map {it ** 2}.
      reduce {a, b -> a + b} //sum the squares of even numbers


     def urls = ['http://www.jroller.com', 'http://www.dzone.com',
                'http://www.infoq.com']
     println 'Sum of chars in all pages: ' +
      urls.parallel.map { it.toURL().text.size() }.sum()


//The equivalent using Fork/Join
//    println urls.collectParallel { it.toURL().text.size() }.sum()
}



                                                      DemoMapReduce.groovy
Dataflow Concurrency
●
    Modeled as a flow of data rather
    than a flow of execution
●
    Tree of dependent values
Like this?
How about this?
My Last Try
              A




    B                 C




D         E       D
When you put it that way...
             A




      B              C




E                D
In Code
def flow = new DataFlows()
task { flow.a = flow.b + flow.c }
task { flow.b = flow.d + flow.e }
task { flow.c = flow.d }
task { flow.d = 17 }
task { flow.e = 12 }
assert 46 == flow.a
println "flow.a = ${flow.a}"
println "flow.b = ${flow.b}"
Actors
final def doubler = Actors.reactor {
    2 * it
}

Actor actor = Actors.actor {
    (1..10).each {doubler << it}
    int i = 0
    loop {
        i += 1
        if (i > 10) stop()
        else {
            react {message ->
                println "Double of $i = $message"
            }
        }
    }
}

actor.join()
doubler.stop()
doubler.join()



                                               DemoReactor2.groovy
Actors
final def doubler = Actors.reactor {
    2 * it
}

Actor actor = Actors.actor {
    (1..10).each {doubler << it}
    int i = 0
    loop {
        i += 1
        if (i > 10) stop()
        else {
            react {message ->
                println "Double of $i = $message"
            }
        }
    }
}

actor.join()
doubler.stop()
doubler.join()



                                               DemoReactor2.groovy
Agents
def jugMembers = new Agent<List<String>>(['Me']) //add Me

jugMembers.send {it.add 'James'} //add James


//add Joe using the left-shift operator
final Thread t1 = Thread.start { jugMembers << { it.add 'Joe' } }

final Thread t2 = Thread.start {
    jugMembers {it.add 'Dave'} //use the implicit call() method
    jugMembers {it.add 'Alice'} //use the implicit call() method
}


[t1, t2]*.join()
println jugMembers.val
jugMembers.valAsync {println "Current members: $it"}
jugMembers.await()

                                                            DemoAgent.groovy
Resources
●
    ReGinA Chapter 17 -
    http://manning.com/koenig2/
●
    GPars User Guide -
    http://gpars.org/0.11/guide/index.html
●
    GPars User Mailing List -
    http://xircles.codehaus.org/lists/user@gpars.codehaus.org
●
    Dierk König's presentation (Concurrent programming for you and me) -
    http://skillsmatter.com/podcast/groovy-grails/concurrent-programming-for-you-and-me

●
    Alex Miller's DevWorks article -
    http://www.ibm.com/developerworks/java/library/j-gpars/index.html
●
    Václav Pech's blog -
    http://www.jroller.com/vaclav/
Credits
●
    GPars logo contest entries: various, late 2009. Author: various -
    http://gpars.codehaus.org/Logo+Contest
●
    Giant Fork: A metal four-pronged fork, Jun 2010. Author: dismal_denizen -
    http://www.openclipart.org/detail/65749
●
    Tree image: Linde von Linn, Jun 2006. Author: Stefan Wernli -
    http://commons.wikimedia.org/wiki/File:Linde_von_linn.jpg
●
    George Clooney image: Nov 2006. Author: James White/Corbis Outline -
    http://bit.ly/iCSVal
●
    Meryl Streep image: 2009. Author: unknown -
    http://gosublogger.com/wp-content/uploads/2009/02/meryl-streep.jpg
Q&A


    http://bit.ly/gparsboston
mpassell@grovehillsoftware.com
http://blog.grovehillsoftware.com
        @softwaregrove
1 of 26

Recommended

Jggug 2010 330 Grails 1.3 観察 by
Jggug 2010 330 Grails 1.3 観察Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Tsuyoshi Yamamoto
1K views45 slides
Gearman, from the worker's perspective by
Gearman, from the worker's perspectiveGearman, from the worker's perspective
Gearman, from the worker's perspectiveBrian Aker
2.1K views28 slides
Gearmam, from the_worker's_perspective copy by
Gearmam, from the_worker's_perspective copyGearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copyBrian Aker
730 views38 slides
多治見IT勉強会 Groovy Grails by
多治見IT勉強会 Groovy Grails多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy GrailsTsuyoshi Yamamoto
959 views43 slides
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン by
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
名古屋SGGAE/J勉強会 Grails、GaelykでハンズオンTsuyoshi Yamamoto
1.8K views48 slides
Javascript ES6 generators by
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generatorsRamesh Nair
4.4K views22 slides

More Related Content

What's hot

EcmaScript 6 - The future is here by
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
4.8K views75 slides
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・- by
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
1.4K views58 slides
Psycopg2 - Connect to PostgreSQL using Python Script by
Psycopg2 - Connect to PostgreSQL using Python ScriptPsycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python ScriptSurvey Department
5.5K views28 slides
ES6 in Real Life by
ES6 in Real LifeES6 in Real Life
ES6 in Real LifeDomenic Denicola
5.5K views21 slides
Map kit light by
Map kit lightMap kit light
Map kit lightCocoaHeads France
2.2K views100 slides
Swift Sequences & Collections by
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & CollectionsCocoaHeads France
5.7K views49 slides

What's hot(20)

Grails 1.2 探検隊 -新たな聖杯をもとめて・・・- by Tsuyoshi Yamamoto
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Tsuyoshi Yamamoto1.4K views
Psycopg2 - Connect to PostgreSQL using Python Script by Survey Department
Psycopg2 - Connect to PostgreSQL using Python ScriptPsycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department5.5K views
G*なクラウド 雲のかなたに ショートバージョン by Tsuyoshi Yamamoto
G*なクラウド 雲のかなたに ショートバージョンG*なクラウド 雲のかなたに ショートバージョン
G*なクラウド 雲のかなたに ショートバージョン
Programming with Python and PostgreSQL by Peter Eisentraut
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut52.2K views
"PostgreSQL and Python" Lightning Talk @EuroPython2014 by Henning Jacobs
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Henning Jacobs1.2K views
Python postgre sql a wonderful wedding by Stéphane Wirtel
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
Stéphane Wirtel1.9K views
The Ring programming language version 1.5.4 book - Part 40 of 185 by Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185
Talk KVO with rac by Philippe Converset by CocoaHeads France
Talk KVO with rac by Philippe ConversetTalk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe Converset
CocoaHeads France6.7K views
Minimizing Decision Fatigue to Improve Team Productivity by Derek Lee Boire
Minimizing Decision Fatigue to Improve Team ProductivityMinimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team Productivity
Derek Lee Boire1.9K views
Real world scala by lunfu zhong
Real world scalaReal world scala
Real world scala
lunfu zhong1.5K views
Cycle.js: Functional and Reactive by Eugene Zharkov
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and Reactive
Eugene Zharkov1.2K views

Similar to GPars For Beginners

Groovy On Trading Desk (2010) by
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
2K views40 slides
Javascript & Ajax Basics by
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
1.4K views18 slides
JavaScript Growing Up by
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
14.1K views57 slides
Gpars workshop by
Gpars workshopGpars workshop
Gpars workshopVaclav Pech
1.2K views54 slides
Refactoring to Macros with Clojure by
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
3.5K views51 slides
Functional programming using underscorejs by
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs偉格 高
1.3K views88 slides

Similar to GPars For Beginners(20)

Javascript & Ajax Basics by Richard Paul
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul1.4K views
JavaScript Growing Up by David Padbury
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury14.1K views
Gpars workshop by Vaclav Pech
Gpars workshopGpars workshop
Gpars workshop
Vaclav Pech1.2K views
Refactoring to Macros with Clojure by Dmitry Buzdin
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin3.5K views
Functional programming using underscorejs by 偉格 高
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs
偉格 高1.3K views
Groovy by Zen Urban
GroovyGroovy
Groovy
Zen Urban1.1K views
Node Boot Camp by Troy Miles
Node Boot CampNode Boot Camp
Node Boot Camp
Troy Miles871 views
Groovy and Grails talk by desistartups
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talk
desistartups577 views
ClojureScript loves React, DomCode May 26 2015 by Michiel Borkent
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent411 views
Exploring Clojurescript by Luke Donnet
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
Luke Donnet83 views
Implementing a many-to-many Relationship with Slick by Hermann Hueck
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
Hermann Hueck4.6K views
How AngularDart & Firebase did an App together by Jana Moudrá
How AngularDart & Firebase did an App togetherHow AngularDart & Firebase did an App together
How AngularDart & Firebase did an App together
Jana Moudrá1.9K views
Tasks: you gotta know how to run them by Filipe Ximenes
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run them
Filipe Ximenes221 views
Scala is java8.next() by daewon jeong
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong3.8K views

Recently uploaded

Spesifikasi Lengkap ASUS Vivobook Go 14 by
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14Dot Semarang
35 views1 slide
Web Dev - 1 PPT.pdf by
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdfgdsczhcet
55 views45 slides
Piloting & Scaling Successfully With Microsoft Viva by
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft VivaRichard Harbridge
10 views160 slides
PharoJS - Zürich Smalltalk Group Meetup November 2023 by
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023Noury Bouraqadi
120 views17 slides
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensorssugiuralab
15 views15 slides
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...Bernd Ruecker
26 views69 slides

Recently uploaded(20)

Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab15 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker26 views
AMAZON PRODUCT RESEARCH.pdf by JerikkLaureta
AMAZON PRODUCT RESEARCH.pdfAMAZON PRODUCT RESEARCH.pdf
AMAZON PRODUCT RESEARCH.pdf
JerikkLaureta15 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex19 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
DALI Basics Course 2023 by Ivory Egg
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023
Ivory Egg14 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn19 views
Lilypad @ Labweek, Istanbul, 2023.pdf by Ally339821
Lilypad @ Labweek, Istanbul, 2023.pdfLilypad @ Labweek, Istanbul, 2023.pdf
Lilypad @ Labweek, Istanbul, 2023.pdf
Ally3398219 views

GPars For Beginners

  • 1. For Beginners A Groovy approach to concurrent programming Matt Passell May 25, 2011
  • 2. About Me • Longtime Java developer (since '97) • Using Groovy on and off since early 2008 • Concurrent programming enthusiast • Started software consultancy in 2007 • More Groovy + Less Java = More Happy
  • 3. Early History ● October 2008: Václav Pech creates GParallelizer ● September 2009: project renamed to GPars, moved to Codehaus, and Groovy luminaries join the team - (Dierk König, Paul King, Alex Tkachman, Russel Winder)
  • 4. GPars Has a Logo Contest http://gpars.codehaus.org/Logo+Contest
  • 5. History ● October 2008: Václav Pech creates GParallelizer ● September 2009: project renamed to GPars, moved to Codehaus, and Groovy luminaries join the team (Dierk König, Paul King, Alex Tkachman, Russel Winder) ● December 2009: GPars gets a logo ● May 2010: 0.10 Release ● December 2010: 0.11 Release ● May 2011: 0.12 Beta 1
  • 6. What GPars Provides ● Code-level Helpers ● Architecture-level Concepts ● Protecting Shared Mutable State
  • 7. What GPars Provides ● Code-level Helpers ● Fork/Join ● Map/Reduce (Parallel Collections) ● Asynchronous Processing* *not covered in this presentation
  • 8. What GPars Provides ● Architecture-level Concepts ● Dataflow Concurrency ● Actors ● Communicating Sequential Processes* *not covered in this presentation
  • 9. What GPars Provides ● Protecting Shared Mutable State ● Agents ● Software Transactional Memory* *not covered in this presentation
  • 10. Recurring Patterns ● Favor immutability ● Stateless code blocks (similar to Servlets)
  • 11. Fork/Join & Map/Reduce Fork Join Fork Join Map Map Reduce
  • 12. Fork/Join def list = [1, 2, 3, 4] GParsPool.withPool { assert [2, 4, 6, 8] == list.collectParallel { it * 2 } def animals = ['dog', 'ant', 'cat', 'whale'] println(animals.makeTransparent(). collect {it.toUpperCase()}. groupBy {it.contains 'A'}) } ParallelEnhancer.enhanceInstance(list) assert [2, 4, 6, 8] == list.collectParallel { it * 2 } DemoParallelizer & DemoParallelEnhancer.groovy
  • 13. Fork/Join def list = [1, 2, 3, 4] GParsPool.withPool { assert [2, 4, 6, 8] == list.collectParallel { it * 2 } def animals = ['dog', 'ant', 'cat', 'whale'] println(animals.makeTransparent(). collect {it.toUpperCase()}. groupBy {it.contains 'A'}) } ParallelEnhancer.enhanceInstance(list) assert [2, 4, 6, 8] == list.collectParallel { it * 2 } DemoParallelizer & DemoParallelEnhancer.groovy
  • 14. Parallel Collections (Map/Reduce) GParsPool.withPool { assert 20 == [1, 2, 3, 4, 5].parallel. filter {it % 2 == 0}. map {it ** 2}. reduce {a, b -> a + b} //sum the squares of even numbers def urls = ['http://www.jroller.com', 'http://www.dzone.com', 'http://www.infoq.com'] println 'Sum of chars in all pages: ' + urls.parallel.map { it.toURL().text.size() }.sum() //The equivalent using Fork/Join // println urls.collectParallel { it.toURL().text.size() }.sum() } DemoMapReduce.groovy
  • 15. Dataflow Concurrency ● Modeled as a flow of data rather than a flow of execution ● Tree of dependent values
  • 18. My Last Try A B C D E D
  • 19. When you put it that way... A B C E D
  • 20. In Code def flow = new DataFlows() task { flow.a = flow.b + flow.c } task { flow.b = flow.d + flow.e } task { flow.c = flow.d } task { flow.d = 17 } task { flow.e = 12 } assert 46 == flow.a println "flow.a = ${flow.a}" println "flow.b = ${flow.b}"
  • 21. Actors final def doubler = Actors.reactor { 2 * it } Actor actor = Actors.actor { (1..10).each {doubler << it} int i = 0 loop { i += 1 if (i > 10) stop() else { react {message -> println "Double of $i = $message" } } } } actor.join() doubler.stop() doubler.join() DemoReactor2.groovy
  • 22. Actors final def doubler = Actors.reactor { 2 * it } Actor actor = Actors.actor { (1..10).each {doubler << it} int i = 0 loop { i += 1 if (i > 10) stop() else { react {message -> println "Double of $i = $message" } } } } actor.join() doubler.stop() doubler.join() DemoReactor2.groovy
  • 23. Agents def jugMembers = new Agent<List<String>>(['Me']) //add Me jugMembers.send {it.add 'James'} //add James //add Joe using the left-shift operator final Thread t1 = Thread.start { jugMembers << { it.add 'Joe' } } final Thread t2 = Thread.start { jugMembers {it.add 'Dave'} //use the implicit call() method jugMembers {it.add 'Alice'} //use the implicit call() method } [t1, t2]*.join() println jugMembers.val jugMembers.valAsync {println "Current members: $it"} jugMembers.await() DemoAgent.groovy
  • 24. Resources ● ReGinA Chapter 17 - http://manning.com/koenig2/ ● GPars User Guide - http://gpars.org/0.11/guide/index.html ● GPars User Mailing List - http://xircles.codehaus.org/lists/user@gpars.codehaus.org ● Dierk König's presentation (Concurrent programming for you and me) - http://skillsmatter.com/podcast/groovy-grails/concurrent-programming-for-you-and-me ● Alex Miller's DevWorks article - http://www.ibm.com/developerworks/java/library/j-gpars/index.html ● Václav Pech's blog - http://www.jroller.com/vaclav/
  • 25. Credits ● GPars logo contest entries: various, late 2009. Author: various - http://gpars.codehaus.org/Logo+Contest ● Giant Fork: A metal four-pronged fork, Jun 2010. Author: dismal_denizen - http://www.openclipart.org/detail/65749 ● Tree image: Linde von Linn, Jun 2006. Author: Stefan Wernli - http://commons.wikimedia.org/wiki/File:Linde_von_linn.jpg ● George Clooney image: Nov 2006. Author: James White/Corbis Outline - http://bit.ly/iCSVal ● Meryl Streep image: 2009. Author: unknown - http://gosublogger.com/wp-content/uploads/2009/02/meryl-streep.jpg
  • 26. Q&A http://bit.ly/gparsboston mpassell@grovehillsoftware.com http://blog.grovehillsoftware.com @softwaregrove