<ul>Concurrency pick the low-hanging fruit </ul><ul>Václav Pech </ul>
<ul>About me </ul>Passionate programmer Concurrency enthusiast GPars @ Codehaus lead Groovy contributor Technology evangel...
<ul>We’re all in the parallel computing business! </ul>
 
# of cores Today Soon
<ul>Dealing with threads sucks! </ul>public class  Counter {  private static long  count =  0 ; public  Counter() {  count...
<ul>Dealing with threads sucks! </ul>public class  Counter {  private static long  count =  0 ; public  Counter() {  synch...
<ul>Dealing with threads sucks! </ul>public class  Counter {  private static long  count =  0 ; public  Counter() {  synch...
<ul>Dealing with threads sucks! </ul>public class  ClickCounter  implements  ActionListener {  public  ClickCounter(JButto...
<ul>Dealing with threads sucks! </ul>Dead-locks, Live-locks, Race conditions, Starvation, ...
<ul>Can we do better? </ul><ul>Map/Reduce </ul><ul>Fork/Join </ul><ul>Actors </ul><ul>STM </ul><ul>Dataflow </ul><ul>Agent...
<ul>Asynchronous invocation </ul><ul>Future f =  threadPool.submit (calculation); … System.out.println( “Result: “  + f.ge...
<ul>Thread Pool </ul>Tasks Worker threads Queue
<ul>Thread Pool </ul>Contention!
<ul>Fork/Join Thread Pool </ul>
<ul>Fork/Join Thread Pool </ul>Work stealing
<ul>Fork/Join </ul><ul><li>Solve hierarchical problems </li></ul><ul><ul><ul><li>Divide and conquer </li></ul><li>Merge so...
Tree traversal
File scan / search
… </li></ul></ul><ul>[a, b, c, d, e, f, g, h] </ul><ul>[a, b, c, d] </ul><ul>[e, f, g, h] </ul><ul>[a, b] </ul><ul>[c, d] ...
<ul>Collections (Groovy/GPars) </ul><ul>images. eachParallel   {it.process()} documents. sumParallel () candidates. maxPar...
<ul>Parallel Arrays (jsr-166y) </ul><ul>ParallelArray namesOfWomen = people .withFilter( aWoman ).withMapping( retrieveNam...
<ul>Dataflow Concurrency </ul><ul><li>No race-conditions
No live-locks
Deterministic deadlocks  </li></ul><ul><ul><li>Completely deterministic programs  </li></ul></ul><ul>BEAUTIFUL code </ul><...
<ul>Dataflow Variables / Promises </ul><ul>main </ul><ul>task2 </ul><ul>task3 </ul><ul>x </ul><ul>y </ul><ul>z </ul><ul>ta...
<ul>DataFlows (GPars) </ul><ul>def  df  = new  DataFlows () task {  df .z =  df .x +  df .y } task {  df .x = 10 } task { ...
df .y = 5
}
assert 15 ==  df .z </li></ul>
Composing async functions int  hash1 = hash(download( 'http://www.gpars.org' ))  int  hash2 = hash(loadFile( '/gpars/websi...
Composing async functions def  hash = oldHash.asyncFun() def  compare = oldCompare.asyncFun() def  download = oldDownload....
Composing async functions int hash(String  text ) {…} Promise<int>  hash( Promise<String>  |  String   text )
Composing async functions int hash(String  text ) {…} Promise<int>  hash( Promise<String>  |  String   text ) { <ul><li>Re...
Upcoming SlideShare
Loading in...5
×

Pick up the low-hanging concurrency fruit

1,291

Published on

My presentation on concurrency at GeeCON 2011

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,291
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Pick up the low-hanging concurrency fruit

  1. 1. <ul>Concurrency pick the low-hanging fruit </ul><ul>Václav Pech </ul>
  2. 2. <ul>About me </ul>Passionate programmer Concurrency enthusiast GPars @ Codehaus lead Groovy contributor Technology evangelist @ JetBrains JetBrains Academy member http://www.jroller.com/vaclav <ul><ul><li>http://twitter.com/vaclav_pech </li></ul></ul>
  3. 3. <ul>We’re all in the parallel computing business! </ul>
  4. 5. # of cores Today Soon
  5. 6. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { count++; } }
  6. 7. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { synchronized ( this ) { count++; } } }
  7. 8. <ul>Dealing with threads sucks! </ul>public class Counter { private static long count = 0 ; public Counter() { synchronized ( this .getClass()) { count++; } } }
  8. 9. <ul>Dealing with threads sucks! </ul>public class ClickCounter implements ActionListener { public ClickCounter(JButton button) { button.addActionListener( this ); } public void actionPerformed( final ActionEvent e) { ... } }
  9. 10. <ul>Dealing with threads sucks! </ul>Dead-locks, Live-locks, Race conditions, Starvation, ...
  10. 11. <ul>Can we do better? </ul><ul>Map/Reduce </ul><ul>Fork/Join </ul><ul>Actors </ul><ul>STM </ul><ul>Dataflow </ul><ul>Agent </ul>
  11. 12. <ul>Asynchronous invocation </ul><ul>Future f = threadPool.submit (calculation); … System.out.println( “Result: “ + f.get()); </ul><ul>main </ul><ul>calculation </ul><ul>get() </ul>
  12. 13. <ul>Thread Pool </ul>Tasks Worker threads Queue
  13. 14. <ul>Thread Pool </ul>Contention!
  14. 15. <ul>Fork/Join Thread Pool </ul>
  15. 16. <ul>Fork/Join Thread Pool </ul>Work stealing
  16. 17. <ul>Fork/Join </ul><ul><li>Solve hierarchical problems </li></ul><ul><ul><ul><li>Divide and conquer </li></ul><li>Merge sort, Quick sort
  17. 18. Tree traversal
  18. 19. File scan / search
  19. 20. … </li></ul></ul><ul>[a, b, c, d, e, f, g, h] </ul><ul>[a, b, c, d] </ul><ul>[e, f, g, h] </ul><ul>[a, b] </ul><ul>[c, d] </ul><ul>[e, f] </ul><ul>[g, h] </ul>
  20. 21. <ul>Collections (Groovy/GPars) </ul><ul>images. eachParallel {it.process()} documents. sumParallel () candidates. maxParallel {it.salary}.marry() </ul>
  21. 22. <ul>Parallel Arrays (jsr-166y) </ul><ul>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();} }; </ul>
  22. 23. <ul>Dataflow Concurrency </ul><ul><li>No race-conditions
  23. 24. No live-locks
  24. 25. Deterministic deadlocks </li></ul><ul><ul><li>Completely deterministic programs </li></ul></ul><ul>BEAUTIFUL code </ul><ul><ul><li>(Jonas Bonér) </li></ul></ul>
  25. 26. <ul>Dataflow Variables / Promises </ul><ul>main </ul><ul>task2 </ul><ul>task3 </ul><ul>x </ul><ul>y </ul><ul>z </ul><ul>task1 </ul>
  26. 27. <ul>DataFlows (GPars) </ul><ul>def df = new DataFlows () task { df .z = df .x + df .y } task { df .x = 10 } task { <li>println ”I am task 3”
  27. 28. df .y = 5
  28. 29. }
  29. 30. assert 15 == df .z </li></ul>
  30. 31. Composing async functions int hash1 = hash(download( 'http://www.gpars.org' )) int hash2 = hash(loadFile( '/gpars/website/index.html' )) boolean result = compare(hash1, hash2) println result
  31. 32. Composing async functions def hash = oldHash.asyncFun() def compare = oldCompare.asyncFun() def download = oldDownload.asyncFun() def loadFile = oldLoadFile.asyncFun() def hash1 = hash(download( 'http://www.gpars.org' )) def hash2 = hash(loadFile( '/gpars/website/index.html' )) def result = compare(hash1, hash2) println result .get()
  32. 33. Composing async functions int hash(String text ) {…} Promise<int> hash( Promise<String> | String text )
  33. 34. Composing async functions int hash(String text ) {…} Promise<int> hash( Promise<String> | String text ) { <ul><li>Return a Promise for the result
  34. 35. Wait (non-blocking) for the text param
  35. 36. Call the original hash()
  36. 37. Bind the result </li></ul>}
  37. 38. Composing async functions Combine functions as usual Parallelism is detected automatically
  38. 39. Milestone <ul><li>Asynchronous calculations
  39. 40. Fork/Join
  40. 41. Parallel collection processing
  41. 42. Dataflow variables/streams </li></ul>
  42. 43. <ul>Actors </ul><ul><li>Isolated
  43. 44. Communicating </li></ul><ul><ul><li>Immutable messages </li></ul></ul><ul><li>Active </li></ul><ul><ul><li>Pooled shared threads </li></ul></ul><ul><li>Activities </li></ul><ul><ul><li>Create a new actor
  44. 45. Send a message
  45. 46. Receive a message </li></ul></ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>Actor </ul><ul>T </ul><ul>T </ul><ul>T </ul><ul>Thread pool </ul>
  46. 47. <ul>Stateless Actors (pure Java) </ul><ul>class MyActor extends DynamicDispatchActor { <li>private Account account = ...
  47. 48. public void onMessage(String msg ) {
  48. 49. String encripted = encrypt( msg );
  49. 50. reply( encripted );
  50. 51. }
  51. 52. public void onMessage(Integer number ) {
  52. 53. reply(2 * number );
  53. 54. }
  54. 55. public void onMessage(Money cash ) {
  55. 56. System.out.println( &quot;Received a donation &quot; + cash );
  56. 57. account .deposit( cash );
  57. 58. }
  58. 59. } </li></ul>
  59. 60. Active Objects @ActiveObject class MyCounter { private int counter = 0 @ActiveMethod def incrementBy( int value) { println &quot;Received an integer: $value &quot; this .counter += value } }
  60. 61. <ul>No more threads and locks </ul><ul>images. eachParallel { //concurrency agnostic code here } def myActor = actor { //concurrency agnostic code here } atomic { /*concurrency agnostic code here*/ } … </ul>
  61. 62. <ul>Summary </ul><ul><li>Parallelism is not hard, multi-threading is
  62. 63. Jon Kerridge, Napier University </li></ul>
  63. 64. <ul><li>Questions?
  64. 65. Find more at: </li><ul><li>http://gpars.codehaus.org
  65. 66. http://www.jroller.com/vaclav
  66. 67. http://twitter.com/vaclav_pech </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×