![Page 1: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/1.jpg)
Concurrency
Unleash your processor(s)Václav Pech
![Page 2: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/2.jpg)
About me
Passionate programmerConcurrency enthusiast
GPars @ Codehaus leadGroovy contributorTechnology evangelist @ JetBrains
http://www.jroller.com/vaclav http://twitter.com/vaclav_pech
![Page 3: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/3.jpg)
We’re all in the parallel computing business!
![Page 4: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/4.jpg)
![Page 5: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/5.jpg)
# of cores
![Page 6: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/6.jpg)
# of cores
Today
Soon
![Page 7: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/7.jpg)
Dealing with threads sucks!
public class Counter { private static long count = 0;
public Counter() {
count++;
} }
![Page 8: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/8.jpg)
Dealing with threads sucks!
public class Counter { private static long count = 0;
public Counter() { synchronized (this) { count++; } } }
![Page 9: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/9.jpg)
Dealing with threads sucks!
public class Counter { private static long count = 0;
public Counter() { synchronized (this.getClass()) { count++; } } }
![Page 10: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/10.jpg)
Dealing with threads sucks!
public class ClickCounter implements ActionListener { public ClickCounter(JButton button) { button.addActionListener(this); }
public void actionPerformed(final ActionEvent e) { ... } }
![Page 11: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/11.jpg)
Stone age of parallel SW
Dead-locksLive-locksRace conditionsStarvation
Shared Mutable State
![Page 12: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/12.jpg)
Multithreaded programs today work mostly by accident!
![Page 13: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/13.jpg)
Map/Reduce
Fork/Join
Actors
STM
Dataflow
Agent
Can we do better?
![Page 14: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/14.jpg)
Asynchronous invocation
Future f = threadPool.submit(calculation);…System.out.println(“Result: “ + f.get());
![Page 15: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/15.jpg)
Thread Pool
Tasks
Worker threads
Queue
![Page 16: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/16.jpg)
Thread Pool
Contention!
![Page 17: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/17.jpg)
Fork/Join Thread Pool
![Page 18: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/18.jpg)
Fork/Join Thread Pool
Work stealing
![Page 19: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/19.jpg)
Fork/Join
Solve hierarchical problems
Divide and conquer Merge sort, Quick sort Tree traversal File scan / search …
[a, b, c, d, e, f, g, h]
[a, b, c, d] [e, f, g, h]
[a, b] [c, d] [e, f] [g, h]
![Page 20: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/20.jpg)
Fork/Join (GPars)
runForkJoin(new File(“./src”)) {currentDir -> long count = 0; currentDir.eachFile { if (it.isDirectory()) { forkOffChild it } else { count++ } } return count + childrenResults.sum(0) }
Waits for children without blocking the thread!
![Page 21: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/21.jpg)
Collections (Groovy/GPars)
images.eachParallel {it.process()}
documents.sumParallel()
candidates.maxParallel {it.salary}.marry()
![Page 22: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/22.jpg)
Parallel Arrays (jsr-166y)
ParallelArray namesOfWomen = people.withFilter(aWoman).withMapping(retrieveName).all();
Ops.Predicate aWoman = new Ops.Predicate() { public boolean op(Person friend) {return !friend.isMale();}};
Ops.Op retrieveName = new Ops.Op() { public Object op(Person friend) {return friend.getName();}};
![Page 23: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/23.jpg)
Dataflow Concurrency
No race-conditions No live-locks Deterministic deadlocks
Completely deterministic programs
BEAUTIFUL code (Jonas Bonér)
![Page 24: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/24.jpg)
Milestone
Asynchronous calculationsFork/JoinParallel collection processingDataflow variables/streams
![Page 25: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/25.jpg)
Message Passing
Actors Processes with mailboxes
Communicating Sequential Processes (CSP) Sequential processes synchronously talking through channels
Dataflow Operators Event-triggered computations connected by channels into a graph
![Page 26: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/26.jpg)
Actors
Isolated Communicating
Immutable messages Active
Pooled shared threads Activities
Create a new actorSend a messageReceive a message
ActorActorActorActorActorActorActor
TTTThread pool
![Page 27: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/27.jpg)
Actors use
GateKeeper
Form
HTTP
FingerPrints
AddressCheck
EmailCheckProcess
FraudDetect
Response
SOAP
SMTP
![Page 28: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/28.jpg)
Actors patternsEnricher
Router
Translator
Endpoint
Splitter
Aggregator
Filter
Resequencer
Checker
![Page 29: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/29.jpg)
Sending messages
buddy.send 10.eur
buddy << new Book(title:’Groovy Recipes’, author:’Scott Davis’)
def canChat = buddy.sendAndWait ‘Got time?’
buddy.sendAndContinue ‘Need money!’, {cash-> pocket.add cash}
![Page 30: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/30.jpg)
Stateless Actors (pure Java)class MyActor extends DynamicDispatchActor { private Account account = ... public void onMessage(String msg) { String encripted = encrypt(msg); reply(encripted); } public void onMessage(Integer number) { reply(2 * number); }
public void onMessage(Money cash) { System.out.println("Received a donation " + cash); account.deposit(cash); }}
![Page 31: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/31.jpg)
Stateful Actors
class MyActor extends DefaultActor { void act() { def buddy = new YourActor() buddy << ‘Hi man, how\’re things?’ def response = receive() }}
![Page 32: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/32.jpg)
Implicit State in Actors
val me = actor { react {msg1 -> switch (msg1) { case Work: reply “I don't work so early” ; stop(); case Breakfast: msg1.eat() react {msg2 → switch (msg2) { case Work: reply “OK, time to work”; msg2.do() case Lunch: ...} } } }
![Page 33: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/33.jpg)
Continuation Style
loop { … react { … react {/* schedule the block and exit */ … } //Never reached } //Never reached}//Never reached
![Page 34: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/34.jpg)
Java actor frameworks
JetlangKilimActorFoundryActoromAkkaGPars (Yes!)…
![Page 35: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/35.jpg)
Shared Mutable State
Misused most of the time
When really needed, use Agents Software Transactional Memory Locks
![Page 36: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/36.jpg)
No more threads and locks
images.eachParallel { //concurrency agnostic code here}
def myActor = actor { //concurrency agnostic code here}
atomic { /*concurrency agnostic code here*/ }…
![Page 37: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/37.jpg)
Summary
Parallelism is not hard, multi-threading is
Jon Kerridge, Napier University
![Page 38: Concurrency - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_UnleashYourPro… · BEAUTIFUL code (Jonas Bonér) Milestone Asynchronous calculations Fork/Join Parallel](https://reader035.vdocuments.us/reader035/viewer/2022062607/60279bc516c8314f1c1bebd7/html5/thumbnails/38.jpg)
Questions?
Find more at:http://gpars.codehaus.org
http://www.jroller.com/vaclav
http://twitter.com/vaclav_pech