SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
 
Psycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python ScriptPsycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python ScriptSurvey Department
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & CollectionsCocoaHeads France
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲームNoritada Shimizu
 
G*なクラウド 雲のかなたに ショートバージョン
G*なクラウド 雲のかなたに ショートバージョンG*なクラウド 雲のかなたに ショートバージョン
G*なクラウド 雲のかなたに ショートバージョンTsuyoshi Yamamoto
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQLPeter Eisentraut
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014Henning Jacobs
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful weddingStéphane Wirtel
 
The Ring programming language version 1.5.4 book - Part 40 of 185
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 185Mahmoud Samir Fayed
 
Talk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe ConversetTalk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe ConversetCocoaHeads France
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard LibraryNelson Glauber Leal
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access RunbookTaha Shakeel
 
Minimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team ProductivityMinimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team ProductivityDerek Lee Boire
 
Writing Clean Code in Swift
Writing Clean Code in SwiftWriting Clean Code in Swift
Writing Clean Code in SwiftDerek Lee Boire
 
Real world scala
Real world scalaReal world scala
Real world scalalunfu zhong
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveEugene Zharkov
 

Mais procurados (20)

EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
Psycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python ScriptPsycopg2 - Connect to PostgreSQL using Python Script
Psycopg2 - Connect to PostgreSQL using Python Script
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
Map kit light
Map kit lightMap kit light
Map kit light
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & Collections
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
G*なクラウド 雲のかなたに ショートバージョン
G*なクラウド 雲のかなたに ショートバージョンG*なクラウド 雲のかなたに ショートバージョン
G*なクラウド 雲のかなたに ショートバージョン
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
 
The Ring programming language version 1.5.4 book - Part 40 of 185
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
Talk KVO with rac by Philippe ConversetTalk KVO with rac by Philippe Converset
Talk KVO with rac by Philippe Converset
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access Runbook
 
Minimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team ProductivityMinimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team Productivity
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Writing Clean Code in Swift
Writing Clean Code in SwiftWriting Clean Code in Swift
Writing Clean Code in Swift
 
Real world scala
Real world scalaReal world scala
Real world scala
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and Reactive
 

Semelhante a GPars For Beginners

Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Functional programming using underscorejs
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs偉格 高
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot CampTroy Miles
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talkdesistartups
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickHermann Hueck
 
How AngularDart & Firebase did an App together
How AngularDart & Firebase did an App togetherHow AngularDart & Firebase did an App together
How AngularDart & Firebase did an App togetherJana Moudrá
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015Igor Laborie
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run themFilipe Ximenes
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 

Semelhante a GPars For Beginners (20)

Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Gpars workshop
Gpars workshopGpars workshop
Gpars workshop
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Functional programming using underscorejs
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs
 
Groovy
GroovyGroovy
Groovy
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talk
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
 
How AngularDart & Firebase did an App together
How AngularDart & Firebase did an App togetherHow AngularDart & Firebase did an App together
How AngularDart & Firebase did an App together
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run them
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Último (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

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